prim: Introduces Python version
[lcapit-junk-code.git] / signal.c
blob84f9ec63c1ce7ab4422ffe42abe73534357f7fa0
1 /*
2 * Signal usage example
3 *
4 * Luiz Fernando N. Capitulino <lcapitulino@mandriva.com.br>
5 */
7 #include <stdio.h>
8 #include <signal.h>
9 #include <stdlib.h>
11 volatile sig_atomic_t quitflag; /* set by the handler */
13 static void sig_int(int signo)
15 if (signo == SIGINT)
16 printf("interrupt\n");
17 else if (signo == SIGQUIT)
18 quitflag = 1;
21 int main(void)
23 sigset_t newmask, oldmask, zeromask;
25 if (signal(SIGINT, sig_int) == SIG_ERR) {
26 perror("signal(SIGINT)");
27 exit(1);
30 if (signal(SIGQUIT, sig_int) == SIG_ERR) {
31 perror("signal(SIGQUIT)");
32 exit(1);
35 sigemptyset(&zeromask);
36 sigemptyset(&newmask);
37 sigaddset(&newmask, SIGQUIT);
40 * Block SIGQUIT and save current signal mask
42 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
43 perror("sigprocmask()");
44 exit(1);
47 while (!quitflag)
48 sigsuspend(&zeromask);
51 * SIGQUIT has been caught and is now blocked; do whatever
53 printf("SIGQUIT caught!\n");
56 * Reset signal mask which unblocks SIGQUIT
58 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
59 perror("sigprocmask()");
60 exit(1);
63 return 0;