Back to index

glibc  2.9
sigusr.c
Go to the documentation of this file.
00001 /*@group*/
00002 #include <signal.h>
00003 #include <stdio.h>
00004 #include <sys/types.h>
00005 #include <unistd.h>
00006 /*@end group*/
00007 
00008 /* When a @code{SIGUSR1} signal arrives, set this variable.  */
00009 volatile sig_atomic_t usr_interrupt = 0;
00010 
00011 void 
00012 synch_signal (int sig)
00013 {
00014   usr_interrupt = 1;
00015 }
00016 
00017 /* The child process executes this function. */
00018 void 
00019 child_function (void)
00020 {
00021   /* Perform initialization. */
00022   printf ("I'm here!!!  My pid is %d.\n", (int) getpid ());
00023 
00024   /* Let parent know you're done. */
00025   kill (getppid (), SIGUSR1);
00026 
00027   /* Continue with execution. */
00028   puts ("Bye, now....");
00029   exit (0);
00030 }
00031 
00032 int
00033 main (void)
00034 {
00035   struct sigaction usr_action;
00036   sigset_t block_mask;
00037   pid_t child_id;
00038 
00039   /* Establish the signal handler. */
00040   sigfillset (&block_mask);
00041   usr_action.sa_handler = synch_signal;
00042   usr_action.sa_mask = block_mask;
00043   usr_action.sa_flags = 0;
00044   sigaction (SIGUSR1, &usr_action, NULL);
00045 
00046   /* Create the child process. */
00047   child_id = fork ();
00048   if (child_id == 0)
00049     child_function ();             /* Does not return.  */
00050 
00051 /*@group*/
00052   /* Busy wait for the child to send a signal. */
00053   while (!usr_interrupt)
00054     ;
00055 /*@end group*/
00056 
00057   /* Now continue execution. */
00058   puts ("That's all, folks!");
00059 
00060   return 0;
00061 }