[cs631apue] Forking too many processes.

lbustama lbustama at stevens.edu
Mon Nov 4 14:43:21 EST 2013


Hi,

When a new connection comes in to the server I fork a new process to 
take care of the new connection.

I was having an issue after forking the new process.
The problem was that before the child process was able to "accept(...)" 
the connection the parent process forks another process as the 
connection is still in the queue.

macbookpro:HW3$ ./sws &
[2] 6832

macbookpro:HW3$ Server listening on IP address 0.0.0.0 and port# 8080
forked pid: 6834
127.0.0.1#62858: Connected:
forked pid: 6835

macbookpro:HW3 Luis$ !net
netstat -na | grep 8080
tcp4       0      0  127.0.0.1.8080         127.0.0.1.62858        
ESTABLISHED
tcp4       0      0  127.0.0.1.62858        127.0.0.1.8080         
ESTABLISHED
tcp4       0      0  *.8080                 *.*                    
LISTEN
macbookpro:HW3 Luis$

^^^ 1 active connection

macbookpro:HW3$ ps -o pid,ppid,comm | grep sws
  6832  6556 ./sws
  6834  6832 ./sws
  6835  6832 ./sws



^^^ 2 new processes for 1 request


The way I got around this was to put the parent process to sleep for 1 
second, which will allow the child to grab the new connection.

I was wondering if there is a better way to do this? what if your web 
server receives several connections per second. Only 1 connection is 
handled every second, which is obviously not good.

Thanks,

Luis


More information about the cs631apue mailing list