分类: C/C++
2007-11-24 18:58:16
#include #include #include #include #include sig_atomic_t child_exit_status; void clean_up_child_process (int signal_number) { /* Clean up the child process. */ int status; wait (&status); /* Store its exit status in a global variable. */ child_exit_status = status; } int main () { pid_t pid; /* Handle SIGCHLD by calling clean_up_child_process. */ struct sigaction sigchld_action; memset (&sigchld_action, 0, sizeof (sigchld_action)); sigchld_action.sa_handler = &clean_up_child_process; sigaction (SIGCHLD, &sigchld_action, NULL); /* Now do things, including forking a child process. */ /* ... */ pid = fork(); if(pid == 0) { printf("In Child:\n"); sleep(5); } else { printf("In Parent:\n"); sleep(10); printf("Child Return Status: %d\n", child_exit_status); } return 0; } |