previous | start | next

Ch 8 Review Problem 2

Describe the control flow of this program and the possible output it can produce.

(Assume fork and signal calls succeed. This is not checked for brevity of the code example.)

    1   
    2   pid_t pid;
    3   
    4   void handler1(int sig) {
    5     printf("zip");
    6     fflush(stdout); /* Flushes the printed string to stdout */
    7     kill(pid, SIGUSR1);
    8   }
    9   void handler2(int sig) {
   10     printf("zap");
   11     exit(0);
   12   }
   13   int main() {
   14     signal(SIGUSR1, handler1);
   15     if ((pid = fork()) == 0) {
   16       signal(SIGUSR1, handler2);
   17       kill(getppid(), SIGUSR1);
   18       while(1) {};
   19     }
   20     else {
   21       pid_t p; int status;
   22       if ((p = wait(&status)) > 0) {
   23         printf("zoom");
   24       }
   25     }
   26     return 0;
   27   }


previous | start | next