I tried this program from Advance Programming on a Unix environment.
#include<stdio.h> #include<signal.h> static void handler(int sig){ if(sig == SIGUSR1) printf("handled user1 signal"); else if(sig == SIGUSR2) printf("handles user2 signal"); else printf("unkown signal"); } int main(){ if(signal(SIGUSR1, handler) == SIG_ERR) printf("can't handle signal SIGUSR1"); if(signal(SIGUSR2, handler) == SIG_ERR) printf("can't handle signal SIGUSR2"); for(;;) pause(); return 0; }
I am using Ubuntu 11.10. I compile the program using gcc and then run a.out, as described in the book.
.
$ / a.out & [1] + 1345
$ kill -USR1 1345
But the seal is not displayed. The program continues to run in backgound, and I have to kill it.
Other things I've tried:
Tried to process SIGINT to see if the running program calls in the background. There is still no way out.
We downloaded the latest version of FreeBSD and tried the same program on it, but with the same problem.
I set the printf statement before installing the signal handler:
int main(){ printf("printf is working...");
when the comment exit () is commented out, there is no output. When I uncomment it, the output is printed.
Please tell me what am I doing wrong in this?
PS: Do not suggest using sigaction (). I learn Unix programming without creating a practical application.
source share