previous | start | next

Race Problem - version 1

    1   
    2   pid_t pid;
    3   int counter = 0;
    4   
    5   void handler1(int sig)
    6   {
    7     counter++;
    8     printf("counter = %d\n", counter);
    9     fflush(stdout);
   10     kill(pid, SIGUSR1);
   11   }
   12   
   13   void handler2(int sig)
   14   {
   15     counter += 3;
   16     printf("counter = %d\n", counter);
   17     exit(0);
   18   }
   19   
   20   int main()
   21   {
   22   
   23     Signal(SIGUSR1, handler1);
   24   
   25     if ((pid = fork()) == 0) {
   26       Signal(SIGUSR1, handler2);
   27       kill(getppid(), SIGUSR1);
   28       while(1) {
   29       }
   30     } else {
   31       pid_t p; 
   32       int status;
   33   
   34       if ((p = wait(&status)) > 0) {
   35         counter += 2;
   36         printf("counter = %d\n", counter);
   37       }
   38     }
   39   
   40     return 0;
   41   }


previous | start | next