1

I have the following C files: parent & child. Parent forks the child, and sends multiple SIGUSR1/SIGUSR2 to the child, the child receives them and prints the output.

receiver.c:

void start_signalset();

void wypisz(int signo) {
    if (signo == SIGUSR1) 
        printf("Received SIGNAL1\n");
    if (signo == SIGUSR2) 
        printf("Received SIGNAL2\n")
    start_signalset();
}

void start_signalset() {
    if(signal(SIGUSR2, wypisz) == SIG_ERR) {  exit(0);  }
    if(signal(SIGUSR1, wypisz) == SIG_ERR) {  exit(0);  }
}

int main(int argc, char ** argv) {
    start_signalset();
    while(is_active) {
        sigset_t pusty;
        sigemptyset(&pusty);
        sigsuspend(&pusty);
    }
    return 0;
}

parent.c:

int main(int argc, char ** argv) {

sigset_t nowy, stary;
sigaddset(&nowy, SIGUSR1);
sigaddset(&nowy, SIGUSR2);
sigprocmask(SIG_BLOCK, &nowy, &stary);

pid_t pid;
if((pid = fork()) == -1) {//error handling }    
else if(pid == 0)
    if(execlp("./receiver", "receiver", NULL) == -1)
        _exit(7);

kill(pid, SIGUSR1);
kill(pid, SIGUSR1);
kill(pid, SIGUSR1);
kill(pid, SIGUSR2);

return 0;
}

The problem is that in receiver I get only one SIGUSR1 and SIGUSR2, no matter how many kills I sent in the parent. So the sample output is:

Received SIGNAL2
Received SIGNAL1

Instead of 4 entries, I have only 2.

Any ideas why?

user2441297
  • 241
  • 2
  • 4
  • 13
  • 3
    Because a signal can never be pending more than once for a process. Subsequent signals received before the first is handled are ignored. – Dark Falcon Apr 22 '15 at 18:12
  • Signals arn't queued. see http://stackoverflow.com/questions/5285414/signal-queuing-in-c – nos Apr 22 '15 at 18:24

0 Answers0