Debian Codegraph @

listings/chapter-3/sigchld.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 #include <signal.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11
12 sig_atomic_t child_exit_status;
13
14 void clean_up_child_process (int signal_number)
15 {
16 /* Clean up the child process. */
17 int status;
18 wait (&status);
19 /* Store its exit status in a global variable. */
20 child_exit_status = status;
21 }
22
23 int main ()
24 {
25 /* Handle SIGCHLD by calling clean_up_child_process. */
26 struct sigaction sigchld_action;
27 memset (&sigchld_action, 0, sizeof (sigchld_action));
28 sigchld_action.sa_handler = &clean_up_child_process;
29 sigaction (SIGCHLD, &sigchld_action, NULL);
30
31 /* Now do things, including forking a child process. */
32 /* ... */
33
34 return 0;
35 }