1 | /* Using kill for Communication |
2 | Copyright (C) 1991-2022 Free Software Foundation, Inc. |
3 | |
4 | This program is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU General Public License |
6 | as published by the Free Software Foundation; either version 2 |
7 | of the License, or (at your option) any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, see <https://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | /*@group*/ |
19 | #include <signal.h> |
20 | #include <stdio.h> |
21 | #include <sys/types.h> |
22 | #include <unistd.h> |
23 | /*@end group*/ |
24 | |
25 | /* When a @code{SIGUSR1} signal arrives, set this variable. */ |
26 | volatile sig_atomic_t usr_interrupt = 0; |
27 | |
28 | void |
29 | synch_signal (int sig) |
30 | { |
31 | usr_interrupt = 1; |
32 | } |
33 | |
34 | /* The child process executes this function. */ |
35 | void |
36 | child_function (void) |
37 | { |
38 | /* Perform initialization. */ |
39 | printf (format: "I'm here!!! My pid is %d.\n" , (int) getpid ()); |
40 | |
41 | /* Let parent know you're done. */ |
42 | kill (pid: getppid (), SIGUSR1); |
43 | |
44 | /* Continue with execution. */ |
45 | puts (s: "Bye, now...." ); |
46 | exit (0); |
47 | } |
48 | |
49 | int |
50 | main (void) |
51 | { |
52 | struct sigaction usr_action; |
53 | sigset_t block_mask; |
54 | pid_t child_id; |
55 | |
56 | /* Establish the signal handler. */ |
57 | sigfillset (&block_mask); |
58 | usr_action.sa_handler = synch_signal; |
59 | usr_action.sa_mask = block_mask; |
60 | usr_action.sa_flags = 0; |
61 | sigaction (SIGUSR1, act: &usr_action, NULL); |
62 | |
63 | /* Create the child process. */ |
64 | child_id = fork (); |
65 | if (child_id == 0) |
66 | child_function (); /* Does not return. */ |
67 | |
68 | /*@group*/ |
69 | /* Busy wait for the child to send a signal. */ |
70 | while (!usr_interrupt) |
71 | ; |
72 | /*@end group*/ |
73 | |
74 | /* Now continue execution. */ |
75 | puts (s: "That's all, folks!" ); |
76 | |
77 | return 0; |
78 | } |
79 | |