Take the 2-minute tour ×

i am asked to find all the possible outputs in this question:

#define N 4
int val = 9;
void handler(sig) {
   val += 3;
int main() {
  pid_t pid;
  int i;
        val -= 3;

I do not know what the line signal(SIGCHLD, handler) does. I only found the following:

SIGABRT - abnormal termination.
SIGFPE - floating point exception.
SIGILL - invalid instruction.
SIGINT - interactive attention request sent to the program.
SIGSEGV - invalid memory access.
SIGTERM - termination request sent to the program.

what does SIGCHLD do? Can you also explain the for loop in this question as well?

and what necessary libraries do I need to compile and run this code?

share|improve this question
more signals: man 7 signal –  Jonas Wielicki Dec 9 '12 at 23:17
still more signals : signal -l –  wildplasser Dec 9 '12 at 23:22
Despite Google not paying tax, this link might be useful, 'SIGCHLD man page'.. BTW the only time I was in Starbucks was to have a slash –  Ed Heal Dec 9 '12 at 23:47
First: RTFM is not going to help the OP. My opinion. Second: to really understand this means --read something like Stevens 'Advanced Programming in the UNIX Environment' -- which I highly recommend. –  jim mcnamara Dec 10 '12 at 2:14

2 Answers 2

up vote 0 down vote accepted

Odd program.

What I think it i trying to shows you is what SIGCHLD causes and how a variable in a child process being changed by the child, has no effect in the parent.

When you fork() you create a child process. Sound familiar? The parent process ( the one that called fork() ) receives the signal. The handler is executed. When you run the program you should see out of 21.

This is because val is incremented by three every time the signal handler is executed 9 + ( 3*4)=21. The code creates four children.

The child process

val -= 3; exit(0);

decrements val. But because this happens in another process, not the original child, it does not touch the "original" val variable because it has its own local copy.

share|improve this answer

When you fork a child process from a parent process, the SIGCHLD is set but the handler function is NOT executed. After the child exits, it trips the SIGCHLD and thus causes the code in the handler function to execute...

share|improve this answer

Your Answer


