previous | start | next

Signal Example 1

A parent registers a signal handler for SIGINT, the forks a child; waits for the child, then reads an integer.

The child registers a different signal handler for SIGINT and reads an integer.

What happens if a SIGINT is generated while the child is waiting to read its integer? Is the SIGINT delivered to both parent and child?

    1   
    2   int main()
    3   {
    4     pid_t session, shell_pid, pgrpid, mypid;
    5     int n;
    6   
    7     signal(SIGINT, handlerP);
    8     shell_pid = getppid();
    9     mypid = fork();
   10     if ( mypid == 0 ) {
   11   
   12       signal(SIGINT, handlerC);
   13       session = getsid(0);
   14       mypid = getpid();
   15       pgrpid = getpgid(0);
   16       printf("Child: session = %u, shell = %u, pgrpid = %u, mypid =
   17       %u\n",
   18              session, shell_pid, pgrpid, mypid);
   19       scanf("%d", &n);
   20       printf("Child got %d\n", n);
   21       exit(0);
   22     }
   23     waitpid(-1, 0, 0);
   24     scanf("%d", &n);
   25     printf("Parent got %d\n", n);
   26   
   27     session = getsid(0);
   28     mypid = getpid();
   29     pgrpid = getpgid(0);
   30     printf("Parent: session = %u, shell = %u, pgrpid = %u, mypid = %u\n",
   31              session, shell_pid, pgrpid, mypid);
   32     return 0;
   33   }


previous | start | next