1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Rep Power

    Computer Science Study Guide Question

    So this is a question we got on our midterm study guide:

    A friend wants to write a program with a parent and a child process that allows the parent to count events in the child. The idea is that the child sends a signal (SIGUSR2) to the parent at each event. The parent has a signal handler for SIGUSR2 that increments a counter each time the handler executes. The parent can then print the count after the child has terminated.

    Your friend tried to write a prototype below. The child sends 5 signals to the parent. Your friend tried it with the calls to sleep at lines 6 and 20 to simulate work by the parent and work by the child. He expected the counter value printed at line 27 to be 5.

    However, with the sleep times as below, he consistently got the value 1 printed, not 5.

    He asks you why he got 1, not 5. What is your explanation?

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    #include <sys/wait.h>
       int counter = 0;
       void handler(int sig) 
           sleep(1); /* Parent does some work in the handler */
      int main() 
          int i;
          signal(SIGUSR2, handler);
          if (fork() == 0) {  /* Child */
          for (i = 0; i < 5; i++) {
              kill(getppid(), SIGUSR2);
              printf("sent SIGUSR2 to parent\n");
              sleep(1); /* Child does some work */
          waitpid(-1, NULL, 0);
          printf("counter=%d\n", counter); 
    The code looked correct to me, and when I took the code and ran it, the output it gave me was 5. So I guess my question is is there something I'm not seeing or is this a typo? I emailed my professor a couple hours ago and have still heard nothing and I wanna make sure I know everything going into this midterm.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Rep Power
    Have you covered race conditions in the class? I'm not sure that's what the question refers to, but it might be.

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Rep Power
    > sleep(1); /* Parent does some work in the handler */
    Well first of all, you DON'T do this.

    A signal handler is in a restricted environment, where only certain functions can be called. See Async-signal-safe functions. Also in the same document, you'll note that normal signals are not queued. If a signal cannot be delivered (because the signal handler is still busy), then the signal is dropped.

    If the parent has got work to do, do it in main and not the signal handler. The signal handler should do the bare minimum necessary (say just increment a variable).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    (note that when I don't test or read the documentation I'm usually wrong. I didn't look this up...)

    I'd answer that the signal handler needs to reinstall itself after handling the signal.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5

IMN logo majestic logo threadwatch logo seochat tools logo