Re: Possible problem with ACCEPT/CONNECT in

From: Vandoorselaere Yoann (yoann@mandrakesoft.com)
Date: Fri Apr 07 2000 - 04:35:14 EST


"Peter Zaitsev" <pz@spylog.ru> writes:

> #define HOST "localhost"
You're using localhost, which is the loopback interface,
AFAIK, there is actually a know slowdown problem with loopback
on recents 2.3 kernels, so maybe the slowdown come from here.

> for(t=0;t<PROCESSES;t++)
> if (fork()==0)

Hummm, so you fork() 200 time, which isn't a really good thing,
ever heard of non blocking connect() then select() on a set of fd ?

> {
> // Here we have the process forked.
> for(c=0;c<CONNECTS;c++)
> {
[...]
> res=connect(i,&serv_addr,sizeof(serv_addr));
> if (res==-1)

Then you start connecting 10000 time for each fork()...
I can understand the time it make to connect...

you're making a total of 2 000 000 of connections, what do you expect ?
with 200 simultaneous connection attempt at the same time, on the loopback
interface ( so there isn't much latency between you're connect() )...

Your benchmark ( which i think is very similar to a m$ windows one )
do not mean anything, you're actually benchmarking BS.

Your client program itself make the machine slow because it is badly
written, and you're benchmarking client & server on the same machine !
What did you expect ?

Please do not blame the kernel for being a bad programmer.

* Now for the server part :

[...]

> if(listen(sockfd,100)<0)printf("error listen\n");
> for(i=0;i<PROCESSES;i++)
> if (fork()==0)
> for(;;){
> bzero(&clnt_addr,sizeof(clnt_addr));
> caddrlen=sizeof(clnt_addr);
> sockfd1=accept(sockfd,(struct sockaddr*)&clnt_addr,&caddrlen);
> if(sockfd1<0) continue;
> close(sockfd1);
> }
> }

Please,
before blowing the list up with such thing, know what you do !
You're currently starting one accept by process.

It could be a good idea for you to learn how to correctly code a
server process...
It should be like that :

while ( 1 ) {
      clnt_sock = accept(sock, (struct sockaddr *) addr, &len);
      if ( clnt_sock < 0 ) {
           perror("accept");
           return -1;
      }
      
      ret = fork();
      if ( ret < 0 ) {
           perror("fork");
           return -1;
      } else if ( ret == 0 ) {
             /*
              * Child
              */
             
             /* Close duplicated server FD which we do not in the client */
             close(sock);

             /* Do whatever */
             ret = HandleConnection(clnt_sock);
             
             /* close the client socket */
             close(clnt_sock);

             /* Exit this server child */
             exit(ret);
      }
      /* We do not need the client socket in the parent process */
      close(clnt_sock);
}

-- 
                   -- Yoann,  http://prelude.sourceforge.net
     It is well known that M$ products don't call free() after a malloc().
     The Unix community wish them good luck for their future developments.

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Fri Apr 07 2000 - 21:00:18 EST