Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking the death of a child process

Tags:

How could I track down the death of a child process without making the parent process wait until the child process got killed?

I am trying a client-server scenario where the server accepts the connection from a client and forks a new process for each and every connection it accepts.

I am ignoring SIGCHLD signals to prevent zombie creation.

signal(SIGCHLD, SIG_IGN); while(1) {   accept();   clients++;   if(fork() ==0)   {      childfunction();      clients--;   }   else   {   } } 

The problem in the above scenario is that if the child process gets killed in the childfunction() function, the global variable clients is not getting decremented.

NOTE: I am looking for a solution without using SIGCHLD signal ... If possible

like image 355
codingfreak Avatar asked Mar 04 '10 08:03

codingfreak


People also ask

What happens when child process dies?

When a child process terminates, some information is returned to the parent process. When a child process terminates before the parent has called wait, the kernel retains some information about the process, such as its exit status, to enable its parent to call wait later.

How long does grief last after losing a child?

The initial severe and intense grief you feel will not be continuous. Periods of intense grief often come and go over 18 months or longer. Over time, your grief may come in waves that are gradually less intense and less frequent. But you will likely always have some feelings of sadness and loss.

What is status in Waitpid?

Only one status is returned per waitpid function call. If pid is equal to -1, status is requested for any child process. If status information is available for two or more processes, the order in which their status is reported is not specified. If pid is greater than 0, status is requested for a single process.

How does the death of a child affect the parent?

Specifically, parents who experienced the death of a child would be more likely than would nonbereaved parents to report depressive symptoms, poor psychological well-being, health problems, limited social participation, marital disruption, and limited occupational success.


1 Answers

Typically you write a handler for SIGCHLD which calls waitpid() on pid -1. You can use the return value from that to determine what pid died. For example:

void my_sigchld_handler(int sig) {     pid_t p;     int status;      while ((p=waitpid(-1, &status, WNOHANG)) != -1)     {        /* Handle the death of pid p */     } }  /* It's better to use sigaction() over signal().  You won't run into the  * issue where BSD signal() acts one way and Linux or SysV acts another. */  struct sigaction sa;  memset(&sa, 0, sizeof(sa)); sa.sa_handler = my_sigchld_handler;  sigaction(SIGCHLD, &sa, NULL); 

Alternatively you can call waitpid(pid, &status, 0) with the child's process ID specified, and synchronously wait for it to die. Or use WNOHANG to check its status without blocking.

like image 136
asveikau Avatar answered Sep 20 '22 18:09

asveikau