views:

336

answers:

3

Hi

Is there any way to differentiate the child processes created by different fork() functions within a program.

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 ??

A: 

use :

getppid() function . 
pavun_cool
You mean I should use getppid() in SIGCHILD signal handler function ... ?? In that case it is not of much useful ... ??
codingfreak
A: 

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.

Ben
Inorder to maintain a list for all the child processes created by fork1 I am using an array in parent process to update the array with the child process ID and in SIGCHLD handler function I am trying to decrement the value of i only if its PID is within the array.. But there is a problem .. if child process dies so fast that even the code in parent process doesnt execute then the array is not getting updated with proper values ..
codingfreak
+1  A: 

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.

jschmier
Actually I am not facing any problem in processing the array List in SIGCHLD signal handler function ... Initially updating the array with process ids of child processes is done by parent process. So in a scenario where execution is order is something like .... child1, parent1, child2, child3, parent2, parent3 ... If child2 is killed before parent2 code is executed then the ARRAY doesnt get updated with child2 PID as a result SIGCHLD signal handler will not decrement the i value as the pid is not present in the array
codingfreak
Now that I have a better understanding of your problem, I have updated my answer accordingly.
jschmier
You mean I should block the SIGCHLD signal before the fork() and unblock it once I am in parent process .. ??
codingfreak
That solved my problem with FLYING colors
codingfreak