开发者

fork() within a fork()

开发者 https://www.devze.com 2022-12-22 01:39 出处:网络
Is there any way to differentiate the child processes created by different fork() functions within a progra开发者_如何转开发m.

Is there any way to differentiate the child processes created by different fork() functions within a progra开发者_如何转开发m.

global variable i;

SIGCHLD handler function()
{
  i--;
}

handle()
{
  fork() --> FORK2
}

main()
{
  while(1)
  {
     if(i<5)
     {
        i++;
        if( (fpid=fork())==0) --> FORK1
           handle()
        else (fpid>0)
           .....
     }
  }
}

Is there any way I can differentiate between child processes created by FORK1 and FORK2 ?? because I am trying to decrement the value of global variable 'i' in SIGCHLD handler function and it should be decremented only for the processes created by FORK1 ..

I tried to use an array and save the process id of the child processes created by FORK1 and this is done by the parent process. I will decrement the value of 'i' only if the process id of dead child is within the array ...

But I faced a problem with the following scenario

child1, parent1, child1 killed, child2, child2 killed, parent2

Incase of child1 since it is killed after parent1 the array is updated properly.

But what in the case of child2 which gets killed before its pid value get updated by parent2 in the array? Inside SIGCHLD signal handler function since child2 PID value is not in the array the 'i' value is not getting decremented accordingly ..

So is there any better solution for this problem ??


Since you're already saving a list of PIDs for which you want to decrement the value of i in the SIGCHLD handler, the part that may be tripping you up is receiving an additional SIGCHLD while updating the list.

You can mask/block additional SIGCHLD signals during execution of the signal handler using sigprocmask(). This should provide you sufficient time to update your array/list of process IDs without worrying about receiving another SIGCHLD. The blocked mask must be returned to its original value when the signal handling routine has finished.

Try adding something similar to the following to your SIGCHLD handler:

sigset_t mask;
sigset_t orig_mask;

sigemptyset (&mask);
sigaddset (&mask, SIGCHLD);

/* temporarily mask/block SIGCHLD signals and save original mask */
if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
    perror ("sigprocmask - %s",  strerror(errno));
}

/* process SIGCHLD via waitpid() and update PID list as necessary */
...

/* restore original mask */ 
if (sigprocmask(SIG_SETMASK, &orig_mask, NULL) < 0) {
    perror ("sigprocmask - %s", strerror(errno));
}

Update

After re-examining your question, I see that you have a race conditon in which a child process terminates before the parent process can add its PID to the list.

One possible solution is still to use sigprocmask() to block the SIGCHLD signal during the critical section. In this case, you will need to block the SIGCHLD signal before your call to fork() and unblock it in the parent code after the PID has been added to the list. If the child dies, the signal handler will be called after you unblock the signal and this should guarantee that the PID is in the list.


use :

getppid() function . 


If I understand well, you might be interested in waitpid() system call. It allows you to wait for a specific child (given a pid). So if you keep track of the pid created by fork1 and fork2, you should be able to decrement your i variable only if a child from fork1 died.

0

精彩评论

暂无评论...
验证码 换一张
取 消