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