Solved

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

Posted on 2009-03-29
3
473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.
In this post we will learn different types of Android Layout and some basics of an Android App.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

710 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