-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignal-vs-sigaction.c
66 lines (58 loc) · 1.46 KB
/
signal-vs-sigaction.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Compile with and without -DPOSIX_SIGNALS and try to run like this
* on {FreeBSD,Linux} vs Solaris to see the difference in behavior:
*
* 0. compile 2 programs
* cc -o sig1 signal-vs-sigaction.c
* cc -DPOSIX_SIGNALS -o sig2 signal-vs-sigaction.c
* 1. run the first program
* ./sig1 &
* 2. send the 2 signals in quick succession (in the 10 seconds window)
* kill -INT `pgrep sig1`
* kill -INT `pgrep sig1`
* 3. repeat steps #1, #2 for program sig2
* 4. observe the behavior (check if the programs are still around)
*
* vlada@devnull.cz, 2009
*/
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
/*
* Using unsafe printf() for simplicity.
*/
void
sig_handler(int s)
{
printf("Got signal! Sleeping.\n");
sleep(10);
printf("returning from signal handler\n");
}
int
main(void)
{
#ifdef POSIX_SIGNALS
struct sigaction s_action;
#endif
/*
* You may experiment with other signals but note that some shell might
* automatically block some of them; bash(1) sometimes block SIGHUP in
* the new process, for example.
*/
printf("Setting signal handler: ");
#ifdef POSIX_SIGNALS
printf("sigaction\n");
(void) sigemptyset(&s_action.sa_mask);
s_action.sa_handler = sig_handler;
s_action.sa_flags = 0;
(void) sigaction(SIGINT, &s_action, (struct sigaction *)NULL);
#else
printf("signal\n");
signal(SIGINT, sig_handler);
#endif
printf("Waiting for signal\n");
while (1)
pause();
return (0);
}