Link to home
Start Free TrialLog in
Avatar of tamg_2009
tamg_2009Flag for India

asked on

What is the behaviour of a double fork with a signal Handler in the Parent as below?

Hi,

I've been trying to judge the output of the following code. Over a double fork(), I have the signal handler for SIGCHLD initialized. As per my understanding, I believed that the signal SIGCHLD would be handled only once by the parent unless initialized again in the handler.

However, after executing the code handles the Signal only twice (Even if I re-initialized my signal in the handler) and inspite of having 3 childen. The above behaviour is with respect to Darwin OS on Mac. On Ubuntu Linux, the signal is handled by the code based on the timing of the sleep (commented out). Can someone please explain what is happening and How many times the signal for my function would actually be handled??
#include<stdio.h>
#include<unistd.h>
#include<sys/signal.h>
 
int status,pid,cpid;
static void signal_handler(int);
 
int main(void)
{
 pid=fork();
 if(fork()==0)
 {
//  sleep(5);
  printf("Return Status in CHILD : %d\tMy PID : %d\t My PPID : %d\n",status,getpid(),getppid());
 }
 else
 {
  signal(SIGCHLD,signal_handler);
  printf("Return Status in PARENT : %d\t My PID : %d\t My PPID : %d\n",status,getpid(),getppid());
  wait(&status);
 }
}
static void signal_handler(int signo)
{
 printf("SIGCHLD Received : %d\n",signo);
// signal(SIGCHLD,signal_handler);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial