Unix – Why doesn’t child process continue running after receiving signal

forkprocesssignalsunix

The following is my code. Parent forks a child. Child pause until parent sends a signal to it, then it continues running.
My question is why doesn't child process continue
running after parent sending signal to him. Did I miss or misunderstand something?

#include<stdio.h>
#include<unistd.h>
#include<signal.h>


void 
sigusr1( int pidno )
{
  printf("Catched\n");
}

int 
main()
{
  pid_t pid;

  signal( SIGUSR1, sigusr1 );
  if( (pid = fork()) == 0 ){
    pause();
  printf("Child\n"); 
  }

  kill( pid , SIGUSR1 ); //parent sends signal to child 
  pause();
}

Best Answer

Here's what happens in the parent:

  1. Fork a child.
  2. Send SIGUSR1 to the child.
  3. Wait for a signal.

Here's what happens in the child:

  1. Wait for a signal.
  2. Print Child.
  3. Call kill(0, SIGUSR1) (0 being the value of pid in the child). Calling kill with a process ID of 0 sends the signal to every process in the process group of the process that calls kill.
  4. Wait for a signal.

There are several possible behaviors for your program, depending on the order in which the parent and the child system calls are executed. Depending on the exact version of your operating system, on the fine-tuning of various kernel parameters, on how loaded your system is, and on random chance, you may or may not observe different behavior if you run the program several times or under a debugger.

If the parent starts faster than the child, you may see this:

  1. Parent sends SIGUSR1 to the child.
  2. Child receives SIGUSR1 and prints Catched.
  3. Child calls pause.
  4. Parent calls pause.

With this execution order, both the parent and the child end up waiting forever (it's a deadlock).

If the child starts faster than the parent, you may see this:

  1. Child calls pause.
  2. Parent sends SIGUSR1 to the child.
  3. Parent calls pause.
  4. Child is unblocked and prints Catched.
  5. Child prints Child.
  6. Child sends SIGUSR1 to the process group.
  7. Child prints Catched.
  8. Child calls pause.
  9. Parent is unblocked and prints Catched.
  10. Parent exits.

I don't think there's a way for the child to exit: it calls pause twice, and while it can receive up to two signals, one of these is sent from itself (the one from kill(0,SIGUSR1)) and that one is delivered synchronously, not during the execution of pause.

This program is probably not what you meant to write, but since you don't describe the expected behavior, it's impossible to tell what you did mean to write. I do note that you do not follow the usual structure of a program that forks:

pid = fork();
if (pid < 0) {
    /*error handling*/
} else if (pid == 0) {
    /*child code*/
    exit(...); /*Usually, what follows the if is specific to the parent.*/
}