Solved

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

Posted on 2009-03-29
3
425 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
Comment Utility
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
Comment Utility
> .. 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 44
Problem to event 3 48
base64 decode encode 12 96
Arduino EDI - Programming language 3 60
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about problems I had with the new location API and permissions in Marshmallow
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 …
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now