Solved

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

Posted on 2009-03-29
3
457 Views
Last Modified: 2013-12-20
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

0
Comment
Question by:tamg_2009
3 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 63 total points
ID: 24015907
According to the code provided, it is the parent that should handle once  (since the 2nd line is commented in the signal handler code) the signal. The child will apply the default action.
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 62 total points
ID: 24072490
> .. and How many times the signal for my function would actually be handled
it should be handled once as long as not re-initialized again
depending on you os you have a runtime condition if the child gets executed before the parent's signal initialization which then results in on call only
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 116
split53 challenge 7 109
Excel file not created as expected 7 79
Why use this lambda? 12 39
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question