title: handling sigusr1 and sigusr2, works for only sigusr2
body: <p>This is (almost) a homework question. I have a sender and a receiver program. Sender takes pid of receiver and int t as command line parameters. It has a string which consists of As and Bs(e.g. "AABBBA") and sends this string to receiver via signals. For every A in the string sends a SIGUSR1 and for every B sends a SIGUSR2. And waits t milliseconds between every letter.</p>
  
  <p>Receiver, should print an A for every SIGUSR1 and a B for every SIGUSR2 arrived. I can't understand why but my receiver prints only Bs. That's the first problem. </p>
  
  <p>In the code above, i use two signal handlers, one for each. But i also want to know if it's possibile to use a single handler to handle both of them. I read something like that it's not ok. But i'm not sure if i got it true. I tried both of them but anyway it prints only Bs. Is it ok to use a handler like this?:</p>
  
  <pre><code> void handler(int signum)
   {
      if (signum == SIGUSR1) // arrived .
      {
        write(1,"A\n", 2);
      }
      else if(signum == SIGUSR2) //arrived _:
      {
        write(1,"B\n", 2);
      }
   }
  
   //in the main function...
  
   sa.sa_handler = handler_sg1;
   ec_neg1(sigaction(SIGUSR1, &amp;sa, NULL), "sigaction");
   ec_neg1(sigaction(SIGUSR2, &amp;sa, NULL), "sigaction");
  </code></pre>
  
  <p>sender.c :</p>
  
  <pre><code> #include &lt;stdio.h&gt;
   #include &lt;stdlib.h&gt;
   #include &lt;errno.h&gt;
   #include &lt;time.h&gt;
   #include &lt;signal.h&gt;
  
   #define ec_neg1(s,m) if((s) == -1) {perror(m); exit(errno);}
  
  
   int main(int argc, char *argv[])
   {
    pid_t pid;
    int sleepVal;
  
    struct timespec ts;
  
    if(argc == 3)
    {
      pid = atoi(argv[1]);
      sleepVal= atoi(argv[2]);
  
      ts.tv_sec = 0;
      ts.tv_nsec = sleepVal * 1000000L;
  
      printf("sender started...\n");
  
      char msg[100] = "AABBBABA";
      int i;
  
      for (i = 0; msg[i] != '\0'; ++i)
      {
            if(msg[i] == 'A')
                kill(pid, SIGUSR1);
            else if( msg[i] == 'B')
                kill(pid, SIGUSR2);
  
            //Wait for sleepVal  milliseconds after every signal
            ec_neg1(nanosleep(&amp;ts, NULL),"nanosleep failed")
      }
  
      printf("done.\n");
  
    }
  
    return 0;
   } 
  </code></pre>
  
  <p>receiver.c :</p>
  
  <pre><code> #include &lt;stdio.h&gt;
   #include &lt;stdlib.h&gt;
   #include &lt;string.h&gt;
   #include &lt;errno.h&gt;
   #include &lt;unistd.h&gt;
   #include &lt;signal.h&gt;
  
   #define ec_neg1(s,m) if((s) == -1) {perror(m); exit(errno);}
  
  
  
   static void handler_sg1(int signum)
   { 
     write(1,"A\n", 2);
   }
  
   static void handler_sg2(int signum)
   { 
     write(1,"B\n", 2);
   }
  
   int main(int argc, char *argv[])
   {
  
     struct sigaction sa;
  
     memset(&amp;sa, 0, sizeof(sa));
     sa.sa_handler = handler_sg1;
     ec_neg1(sigaction(SIGUSR1, &amp;sa, NULL), "sigaction");
     sa.sa_handler = handler_sg2;
     ec_neg1(sigaction(SIGUSR2, &amp;sa, NULL), "sigaction");
  
     printf("receiver started...\n");
  
     while(1)
       ;
  
  
    return 0;
   }
  </code></pre>