Solved

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

Posted on 2009-03-29
3
483 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

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Make the most of your online learning experience.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes

626 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