|
1 | | -#include <signal.h> |
2 | | -#include <stdio.h> |
3 | | -#include <stdlib.h> |
4 | | -#include <unistd.h> |
5 | | - |
6 | | -#include "Common.h" |
7 | | - |
8 | | -pid_t child_pid; |
9 | | - |
10 | | -// Compilation: |
11 | | -// gcc "01 - Signals_SIGINT_SIGTERM_nonkillable_parent.c" -o nonkillable_parent |
12 | | - |
13 | | -// Task: |
14 | | -// Create a program which creates a child process and this child process can be killed by SIGTERM or SIGINT. |
15 | | -// However, parent should not be killed by first SIGTERM/SIGINT. It may be killed in further signals. |
16 | | - |
17 | | -void cant_stop_me_handler(int sig) |
18 | | -{ |
19 | | - if (child_pid) |
20 | | - { |
21 | | - raise(-9); // or kill(-9, child_pid); |
22 | | - printf("SIGCHLD Child: killed %d.\n", child_pid); |
23 | | - } |
24 | | - else |
25 | | - { |
26 | | - printf("SIGCHLD Parent: Ha-ha, can't kill me this time :)! (try again)\n"); |
27 | | - } |
28 | | -} |
29 | | - |
30 | | -void cant_stop_me_handler_int(int sig) |
31 | | -{ |
32 | | - if (child_pid) |
33 | | - { |
34 | | - raise(-9); // or kill(-9, child_pid); |
35 | | - printf("SIGINT Child: killed %d.\n", child_pid); |
36 | | - } |
37 | | - else |
38 | | - { |
39 | | - printf("SIGINT Parent: Ha-ha, can't kill me this time :)! (try again)\n"); |
40 | | - } |
41 | | -} |
42 | | - |
43 | | -int main() |
44 | | -{ |
45 | | - pid_t main_pid = getpid(); |
46 | | - |
47 | | - printf("Start of process %d forever.\n", main_pid); |
48 | | - |
49 | | - // Do no stop by SIGTERM. |
50 | | - struct sigaction cant_stop_me_signal; |
51 | | - cant_stop_me_signal.sa_handler = cant_stop_me_handler; |
52 | | - sigemptyset(&cant_stop_me_signal.sa_mask); |
53 | | - cant_stop_me_signal.sa_flags = SA_RESETHAND; // Non-permanent handler. Handle only first time... |
54 | | - |
55 | | - if (sigaction(SIGTERM, &cant_stop_me_signal, 0) == -1) |
56 | | - { |
57 | | - perror("Error of calling sigaction in parent."); |
58 | | - exit(EXIT_FAILURE); |
59 | | - } |
60 | | - |
61 | | - // Do no stop by SIGINT. |
62 | | - struct sigaction cant_stop_me_signal1; |
63 | | - cant_stop_me_signal1.sa_handler = cant_stop_me_handler_int; |
64 | | - sigemptyset(&cant_stop_me_signal1.sa_mask); |
65 | | - cant_stop_me_signal1.sa_flags = SA_RESETHAND; // Non-permanent handler. Handle only first time... |
66 | | - |
67 | | - if (sigaction(SIGINT, &cant_stop_me_signal1, 0) == -1) |
68 | | - { |
69 | | - perror("Error of calling sigaction in parent."); |
70 | | - exit(EXIT_FAILURE); |
71 | | - } |
72 | | - |
73 | | - // Make child. |
74 | | - if((child_pid = fork())) |
75 | | - { |
76 | | - printf("PARENT: Start of child process %d forever. Press Ctrl+C OR write 'kill -9 %d' to terminate child.\n", child_pid, child_pid); |
77 | | - } |
78 | | - else |
79 | | - { |
80 | | - // Do anything in child. |
81 | | - } |
82 | | - |
83 | | - while(1) |
84 | | - { |
85 | | - // Forever... until someone press Ctrl+C or write 'kill -9 xxxx' |
86 | | - } |
87 | | - |
88 | | - printf("End of process %d\n", getpid()); |
89 | | - |
90 | | - exit(0); |
91 | | -} |
| 1 | +#include <signal.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <unistd.h> |
| 5 | + |
| 6 | +#include "Common.h" |
| 7 | + |
| 8 | +pid_t child_pid; |
| 9 | + |
| 10 | +// Compilation: |
| 11 | +// gcc "01 - Signals_SIGINT_SIGTERM_nonkillable_parent.c" -o nonkillable_parent |
| 12 | + |
| 13 | +// Task: |
| 14 | +// Create a program which creates a child process and this child process can be killed by SIGTERM or SIGINT. |
| 15 | +// However, parent should not be killed by first SIGTERM/SIGINT. It may be killed in further signals. |
| 16 | + |
| 17 | +void cant_stop_me_handler(int sig) |
| 18 | +{ |
| 19 | + if (child_pid) |
| 20 | + { |
| 21 | + raise(-9); // or kill(-9, child_pid); |
| 22 | + printf("SIGCHLD Child: killed %d.\n", child_pid); |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + printf("SIGCHLD Parent: Ha-ha, can't kill me this time :)! (try again)\n"); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +void cant_stop_me_handler_int(int sig) |
| 31 | +{ |
| 32 | + if (child_pid) |
| 33 | + { |
| 34 | + raise(-9); // or kill(-9, child_pid); |
| 35 | + printf("SIGINT Child: killed %d.\n", child_pid); |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + printf("SIGINT Parent: Ha-ha, can't kill me this time :)! (try again)\n"); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +int main() |
| 44 | +{ |
| 45 | + pid_t main_pid = getpid(); |
| 46 | + |
| 47 | + printf("Start of process %d forever.\n", main_pid); |
| 48 | + |
| 49 | + // Do no stop by SIGTERM. |
| 50 | + struct sigaction cant_stop_me_signal; |
| 51 | + cant_stop_me_signal.sa_handler = cant_stop_me_handler; |
| 52 | + sigemptyset(&cant_stop_me_signal.sa_mask); |
| 53 | + cant_stop_me_signal.sa_flags = SA_RESETHAND; // Non-permanent handler. Handle only first time... |
| 54 | + |
| 55 | + if (sigaction(SIGTERM, &cant_stop_me_signal, 0) == -1) |
| 56 | + { |
| 57 | + perror("Error of calling sigaction in parent."); |
| 58 | + exit(EXIT_FAILURE); |
| 59 | + } |
| 60 | + |
| 61 | + // Do no stop by SIGINT. |
| 62 | + struct sigaction cant_stop_me_signal1; |
| 63 | + cant_stop_me_signal1.sa_handler = cant_stop_me_handler_int; |
| 64 | + sigemptyset(&cant_stop_me_signal1.sa_mask); |
| 65 | + cant_stop_me_signal1.sa_flags = SA_RESETHAND; // Non-permanent handler. Handle only first time... |
| 66 | + |
| 67 | + if (sigaction(SIGINT, &cant_stop_me_signal1, 0) == -1) |
| 68 | + { |
| 69 | + perror("Error of calling sigaction in parent."); |
| 70 | + exit(EXIT_FAILURE); |
| 71 | + } |
| 72 | + |
| 73 | + // Make child. |
| 74 | + if((child_pid = fork())) |
| 75 | + { |
| 76 | + printf("PARENT: Start of child process %d forever. Press Ctrl+C OR write 'kill -9 %d' to terminate child.\n", child_pid, child_pid); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + // Do anything in child. |
| 81 | + } |
| 82 | + |
| 83 | + while(1) |
| 84 | + { |
| 85 | + // Forever... until someone press Ctrl+C or write 'kill -9 xxxx' |
| 86 | + } |
| 87 | + |
| 88 | + printf("End of process %d\n", getpid()); |
| 89 | + |
| 90 | + exit(EXIT_SUCCESS); |
| 91 | +} |
0 commit comments