Update.
[glibc.git] / manual / examples / sigusr.c
blob11e3ceee8f18ebbd3274a3e1fd52e6b7a9ab5aff
1 /*@group*/
2 #include <signal.h>
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 /*@end group*/
8 /* When a @code{SIGUSR1} signal arrives, set this variable. */
9 volatile sig_atomic_t usr_interrupt = 0;
11 void
12 synch_signal (int sig)
14 usr_interrupt = 1;
17 /* The child process executes this function. */
18 void
19 child_function (void)
21 /* Perform initialization. */
22 printf ("I'm here!!! My pid is %d.\n", (int) getpid ());
24 /* Let parent know you're done. */
25 kill (getppid (), SIGUSR1);
27 /* Continue with execution. */
28 puts ("Bye, now....");
29 exit (0);
32 int
33 main (void)
35 struct sigaction usr_action;
36 sigset_t block_mask;
37 pid_t child_id;
39 /* Establish the signal handler. */
40 sigfillset (&block_mask);
41 usr_action.sa_handler = synch_signal;
42 usr_action.sa_mask = block_mask;
43 usr_action.sa_flags = 0;
44 sigaction (SIGUSR1, &usr_action, NULL);
46 /* Create the child process. */
47 child_id = fork ();
48 if (child_id == 0)
49 child_function (); /* Does not return. */
51 /*@group*/
52 /* Busy wait for the child to send a signal. */
53 while (!usr_interrupt)
55 /*@end group*/
57 /* Now continue execution. */
58 puts ("That's all, folks!");
60 return 0;