user3717434
user3717434

Reputation: 225

handling sigusr1 and sigusr2, works for only sigusr2

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.

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.

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?:

 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, &sa, NULL), "sigaction");
 ec_neg1(sigaction(SIGUSR2, &sa, NULL), "sigaction");

sender.c :

 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <time.h>
 #include <signal.h>

 #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(&ts, NULL),"nanosleep failed")
    }

    printf("done.\n");

  }

  return 0;
 } 

receiver.c :

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
 #include <signal.h>

 #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(&sa, 0, sizeof(sa));
   sa.sa_handler = handler_sg1;
   ec_neg1(sigaction(SIGUSR1, &sa, NULL), "sigaction");
   sa.sa_handler = handler_sg2;
   ec_neg1(sigaction(SIGUSR2, &sa, NULL), "sigaction");

   printf("receiver started...\n");

   while(1)
     ;


  return 0;
 }

Upvotes: 1

Views: 1787

Answers (1)

user1525888
user1525888

Reputation: 23

Ok. If you just assign the sleepVal to tv_sec and have tv_nsec assigned to 0 work ?

Upvotes: 0

Related Questions