C/Zombies

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with '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 fr…')
 
Line 3: Line 3:
 
Assume <code>sock</code> is the file descriptor that you wish to read from...
 
Assume <code>sock</code> is the file descriptor that you wish to read from...
 
<code>
 
<code>
/* 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) {
  int pid, status, read;
+
    int pid, status, read;
  struct timeval timeout;
+
    struct timeval timeout;
  fd_set toset;
+
    fd_set toset;
  
  /* check if any children have become zombies! */
+
    /* check if any children have become zombies! */
  pid = waitpid(-1,&status,WNOHANG | WUNTRACED);
+
    pid = waitpid(-1,&status,WNOHANG | WUNTRACED);
  if (pid > 0) {
+
    if (pid > 0) {
    children--;
+
      children--;
    printf("*** Child Died ***  PID: %-6d    Status: %-3d    Children: %-2d\n",pid,WEXITSTATUS(status),children);
+
      printf("*** Child Died ***  PID: %-6d    Status: %-3d    Children: %-2d\n",pid,WEXITSTATUS(status),children);
  }
+
    }
  /* if there are no children left, then we dont need to be here anymore... */
+
    /* if there are no children left, then we dont need to be here anymore... */
  if (!children) break;
+
    if (!children) break;
  
  /* timeout on the socket every 0.5sec */
+
    /* timeout on the socket every 0.5sec */
  memset(&timeout,0,sizeof(timeout));
+
    memset(&timeout,0,sizeof(timeout));
  timeout.tv_usec = 500000;
+
    timeout.tv_usec = 500000;
  
  FD_ZERO(&toset);
+
    FD_ZERO(&toset);
  FD_SET(sock,&toset);
+
    FD_SET(sock,&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,&toset,NULL,NULL,&timeout)) == -1) {
    perror("select()");
+
      perror("select()");
    break;
+
      break;
  } else if (read != 0) {
+
    } else if (read != 0) {
    /* something can be read! */
+
      /* something can be read! */
    break;
+
      break;
 +
    }
 
   }
 
   }
}
 
 
</code>
 
</code>

Revision as of 12:29, 3 March 2010

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

Assume sock is the file descriptor that you wish to read from...

 /* 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, then we dont need to be here anymore... */
   if (!children) break;
   /* 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;
   }
 }

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox