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