[cs631apue] Forking too many processes.
hao wu
hwu9 at stevens.edu
Mon Nov 4 14:57:38 EST 2013
"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."
it is better to do the "accept" in the main process
here is my logic:
//main process
while {
accept(....)
//a new client coming:
fork a child process handle this connection
continue; //main process waiting for another connection
}
-Hao
On Mon, Nov 4, 2013 at 2:48 PM, lbustama <lbustama at stevens.edu> wrote:
> By the way here is part of the code. Just in case I'm doing something
> wrong:
>
> listen(sock, 5);
> 184 do {
> 185 FD_ZERO(&ready);
> 186 FD_SET(sock, &ready);
> 187 to.tv_sec = 5;
> 188 to.tv_usec = 0;
> 189 if (select(sock + 1, &ready, 0, 0, &to) < 0) {
> 190 perror("select");
> 191 continue;
> 192 }
> 193 if (FD_ISSET(sock, &ready)) {
> 194
> 195 pid_t pid;
> 196 if((pid = fork()) < 0)
> 197 {
> 199 perror("fork error");
> 200 }
> 201 if(pid == 0) /*child*/
> 202 {
> 203 /* variables for client information */
> 204 socklen_t client_length;
> 205 struct sockaddr_in client;
> 206 unsigned int client_port;
> 207
> 208 client_length = sizeof(client);
> 209 msgsock = accept(sock, (struct sockaddr *)&client,
> 210 &client_length);
> ....
> 230 exit(EXIT_SUCCESS);
> 231 }
> 232 else
> 233 {
> 234 printf("forked pid: %d\n",pid);
> 235 }
> 236 }
> 243 } while (TRUE);
>
> Thanks,
>
> Luis
>
>
> On 11/04/2013 2:43 PM, lbustama wrote:
>
>> 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
>>
> _______________________________________________
> cs631apue mailing list
> cs631apue at lists.stevens.edu
> https://lists.stevens.edu/mailman/listinfo/cs631apue
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.stevens.edu/pipermail/cs631apue/attachments/20131104/90661fda/attachment.htm>
More information about the cs631apue
mailing list