Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-03-29
3
Medium Priority
?
488 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 252 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 248 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
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…

660 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