0

I'm trying to subscribe to a new signal handler in my current signal handler but nothing happens. No output from usr1b is printed in terminal, output from usr1a is printed in terminal.

Code:

#include<signal.h>
#include<stdio.h>
#include<unistd.h>

static int counter = 0;

void usr1b(int sig1) {
    printf("usr1b\n");
    counter++;
    printf("current count: %d", counter);
}

void usr1a(int sig) {
    printf("usr1a\n");
    if( signal(SIGUSR1, usr1b) == SIG_ERR) {
        printf("ERROR\n");   // subscribe usr1b to SIGUSR1
    }
}

int main(void) {
    if(signal(SIGINT, SIG_IGN)==SIG_ERR)
        printf("ERROR"); // ignore SIGINT

    if(signal(SIGUSR1, usr1a)==SIG_ERR)
        printf("ERROR");  // subscribe usr1a to SIGUSR1

    printf("Send some signal to process %d\n", getpid());

    while (1) {
        sleep(1);
    }
    return 0;
}
ChristianG
  • 11
  • 3
  • Have you sent another `SIGUSR1` to the process? Also, be aware that using `printf` in signal handlers is, strictly speaking, not allowed (but usually works). – Wintermute Mar 03 '15 at 10:55
  • Consider using [`sigaction` rather than `signal`](http://stackoverflow.com/a/232711/132382) – pilcrow Mar 04 '15 at 18:01

1 Answers1

0

Try this...

static void usr1b(int signo) {
     if (signo == SIGUSR1) {
        cnt++;
        printf("SIGUSR1 count: %d\n", cnt);
    }
}