<div dir="ltr"><div><div><div><div><div><div>"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."<br><br></div>it is better to do the "accept" 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"><<a href="mailto:lbustama@stevens.edu" target="_blank">lbustama@stevens.edu</a>></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'm doing something wrong:<br>
<br>
listen(sock, 5);<br>
184 do {<br>
185 FD_ZERO(&ready);<br>
186 FD_SET(sock, &ready);<br>
187 to.tv_sec = 5;<br>
188 to.tv_usec = 0;<br>
189 if (select(sock + 1, &ready, 0, 0, &to) < 0) {<br>
190 perror("select");<br>
191 continue;<br>
192 }<br>
193 if (FD_ISSET(sock, &ready)) {<br>
194<br>
195 pid_t pid;<br>
196 if((pid = fork()) < 0)<br>
197 {<br>
199 perror("fork error");<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 *)&client,<br>
210 &client_length);<br>
....<br>
230 exit(EXIT_SUCCESS);<br>
231 }<br>
232 else<br>
233 {<br>
234 printf("forked pid: %d\n",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>
"accept(...)" the connection the parent process forks another process<br>
as the connection is still in the queue.<br>
<br>
macbookpro:HW3$ ./sws &<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>