simple forking server -> problem (C)

Peter L. Peres plp-ysDPMY98cNQDDBjDh4tngg at public.gmane.org
Tue Nov 30 23:53:16 UTC 2004


I have a strange problem. I have a simple forking server that has a 
structure like:

sigaction(SIGCHLD...catcher)
socket()
bind()
listen()
while(1) {
 	t = accept()
 	fork() // child
 		read()
 		write()
 		exit()
}

catcher() ...

problem: the first client connects and everything works. The second 
concurrent client connects (while the first one is connected) and it is 
blocked on accept(). gdb attached to the parent process shows the blocking 
is on the accept() call. If the first client closes its socket the first 
child exits and now the blocking accept opens and the second client forks 
and works. I.e. I cannot have more than one child (actually socket) of the 
parent process (socket) concurrently. Huh ? I tried setting various socket 
options using linger, reuseaddr, etc. I have extensive logging turned on 
and this is precisely what happens.

I even modified the master socket to be nonblocking and async and have 
logging turned on. Even with nonblocking accept and the second client 
connected accroding to netstat, accept returns EAGAIN while the second 
client is blocking. HUH ?! This is something that happens at the 
kernel/firewall level imho. I used several interfaces for testing (lo, 
eth0, default etc).

The code is known to work (it worked a few years ago). 
The only thing I haven't tried was to use a long version of fork, and to 
link statically in the hope that whatever mutex is in accept() will decide 
to work. Btw when the second client connects netstat shows it's connected, 
accept() blocks however.

Any ideas as to what might cause this strange behavior ? Maybe I forgot 
something but I don't think so. This is code that used to work. The 
compiler is gcc-3.3.3 and the kernel is 2.4 (debian based). Is there a 
setting on how many children a process can fork or spawn ? I tried to run 
the server as root, but no change.

Any ideas are welcome.

tia,
Peter
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list