-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.c
49 lines (38 loc) · 1.39 KB
/
signal.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
#include <stdio.h>
#include <uv.h>
#include <stdlib.h>
static int kHandleTimes = 1;
void signal_handler(uv_signal_t* handle, int signum) {
kHandleTimes--;
// Print that our signal was received
printf("Signal received: %d\n", signum);
if (kHandleTimes == 0) {
// Stop our signal listening
// at this point, the process will finish.
// See docs: https://docs.libuv.org/en/v1.x/signal.html#c.uv_signal_stop
uv_signal_stop(handle);
}
}
int main(int argc, char *argv[]) {
// Handle signal k times using argv.
if (argc > 1) {
kHandleTimes = atoi(argv[1]);
}
printf("Will handle SIGINT signal %d time(s)\n", kHandleTimes);
// Initialize our event loop
// See docs: https://docs.libuv.org/en/v1.x/loop.html#c.uv-loop-t-event-loop
uv_loop_t* loop = uv_default_loop();
// Create a variable for our signal handler
// See docs: https://docs.libuv.org/en/v1.x/signal.html#c.uv-signal-t
uv_signal_t signal;
// Initialize signal handler.
// See docs: https://docs.libuv.org/en/v1.x/signal.html#c.uv_signal_init
uv_signal_init(loop, &signal);
// In this case, we'll listening to SIGINT (ctrl + c) signal.
// When that signal is triggered, will run signal_handler callback.
// See docs: https://docs.libuv.org/en/v1.x/signal.html#c.uv_signal_start
uv_signal_start(&signal, signal_handler, SIGINT);
// Run our event loop
uv_run(loop, UV_RUN_DEFAULT);
return 0;
}