Update copyright notices with scripts/update-copyrights.
[glibc.git] / manual / examples / sigusr.c
blobacc7237fc3ded38129b69726308ac11946a7ba92
1 /* Using kill for Communication
2 Copyright (C) 1991-2013 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
18 /*@group*/
19 #include <signal.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 /*@end group*/
25 /* When a @code{SIGUSR1} signal arrives, set this variable. */
26 volatile sig_atomic_t usr_interrupt = 0;
28 void
29 synch_signal (int sig)
31 usr_interrupt = 1;
34 /* The child process executes this function. */
35 void
36 child_function (void)
38 /* Perform initialization. */
39 printf ("I'm here!!! My pid is %d.\n", (int) getpid ());
41 /* Let parent know you're done. */
42 kill (getppid (), SIGUSR1);
44 /* Continue with execution. */
45 puts ("Bye, now....");
46 exit (0);
49 int
50 main (void)
52 struct sigaction usr_action;
53 sigset_t block_mask;
54 pid_t child_id;
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, &usr_action, NULL);
63 /* Create the child process. */
64 child_id = fork ();
65 if (child_id == 0)
66 child_function (); /* Does not return. */
68 /*@group*/
69 /* Busy wait for the child to send a signal. */
70 while (!usr_interrupt)
72 /*@end group*/
74 /* Now continue execution. */
75 puts ("That's all, folks!");
77 return 0;