<div dir="ltr"><div><div><div><div><div><div>&quot;The problem was that before the child process was able to &quot;accept(...)&quot; 
the connection the parent process forks another process as the 
connection is still in the queue.&quot;<br><br></div>it is better to do the &quot;accept&quot; in the main process<br></div>here is my logic:<br><br></div><div>//main process<br></div><div>while {<br></div>   accept(....)<br>

</div>   //a new client coming:<br></div>   fork a child process handle this connection<br></div>   continue; //main process waiting for another connection<br><div><div><div><div><div><div>}<br><div><br></div><div>-Hao<br>

</div><div><br></div></div></div></div></div></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Nov 4, 2013 at 2:48 PM, lbustama <span dir="ltr">&lt;<a href="mailto:lbustama@stevens.edu" target="_blank">lbustama@stevens.edu</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">By the way here is part of the code. Just in case I&#39;m doing something wrong:<br>
<br>
listen(sock, 5);<br>
184     do {<br>
185         FD_ZERO(&amp;ready);<br>
186         FD_SET(sock, &amp;ready);<br>
187         to.tv_sec = 5;<br>
188         to.tv_usec = 0;<br>
189         if (select(sock + 1, &amp;ready, 0, 0, &amp;to) &lt; 0) {<br>
190             perror(&quot;select&quot;);<br>
191             continue;<br>
192         }<br>
193         if (FD_ISSET(sock, &amp;ready)) {<br>
194<br>
195             pid_t pid;<br>
196             if((pid = fork()) &lt; 0)<br>
197             {<br>
199                 perror(&quot;fork error&quot;);<br>
200             }<br>
201             if(pid == 0) /*child*/<br>
202             {<br>
203                 /* variables for client information */<br>
204                 socklen_t client_length;<br>
205                 struct sockaddr_in client;<br>
206                 unsigned int client_port;<br>
207<br>
208                 client_length = sizeof(client);<br>
209                 msgsock = accept(sock, (struct sockaddr *)&amp;client,<br>
210                         &amp;client_length);<br>
                    ....<br>
230                 exit(EXIT_SUCCESS);<br>
231             }<br>
232             else<br>
233             {<br>
234                 printf(&quot;forked pid: %d\n&quot;,pid);<br>
235             }<br>
236         }<br>
243     } while (TRUE);<br>
<br>
Thanks,<br>
<br>
Luis<div class="HOEnZb"><div class="h5"><br>
<br>
On 11/04/2013 2:43 PM, lbustama wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
When a new connection comes in to the server I fork a new process to<br>
take care of the new connection.<br>
<br>
I was having an issue after forking the new process.<br>
The problem was that before the child process was able to<br>
&quot;accept(...)&quot; the connection the parent process forks another process<br>
as the connection is still in the queue.<br>
<br>
macbookpro:HW3$ ./sws &amp;<br>
[2] 6832<br>
<br>
macbookpro:HW3$ Server listening on IP address 0.0.0.0 and port# 8080<br>
forked pid: 6834<br>
127.0.0.1#62858: Connected:<br>
forked pid: 6835<br>
<br>
macbookpro:HW3 Luis$ !net<br>
netstat -na | grep 8080<br>
tcp4       0      0  127.0.0.1.8080         127.0.0.1.62858        ESTABLISHED<br>
tcp4       0      0  127.0.0.1.62858        127.0.0.1.8080         ESTABLISHED<br>
tcp4       0      0  *.8080                 *.*                    LISTEN<br>
macbookpro:HW3 Luis$<br>
<br>
^^^ 1 active connection<br>
<br>
macbookpro:HW3$ ps -o pid,ppid,comm | grep sws<br>
 6832  6556 ./sws<br>
 6834  6832 ./sws<br>
 6835  6832 ./sws<br>
<br>
<br>
<br>
^^^ 2 new processes for 1 request<br>
<br>
<br>
The way I got around this was to put the parent process to sleep for 1<br>
second, which will allow the child to grab the new connection.<br>
<br>
I was wondering if there is a better way to do this? what if your web<br>
server receives several connections per second. Only 1 connection is<br>
handled every second, which is obviously not good.<br>
<br>
Thanks,<br>
<br>
Luis<br>
</blockquote>
______________________________<u></u>_________________<br>
cs631apue mailing list<br>
<a href="mailto:cs631apue@lists.stevens.edu" target="_blank">cs631apue@lists.stevens.edu</a><br>
<a href="https://lists.stevens.edu/mailman/listinfo/cs631apue" target="_blank">https://lists.stevens.edu/<u></u>mailman/listinfo/cs631apue</a><br>
</div></div></blockquote></div><br></div>