在子进程结束后清理子进程实际上意味着什么? 在高级Linux编程一书中,有一节通过处理SIGCHLD信号来异步清理子进程。子进程终止后,此信号将发送到父进程。
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 ()
{
/* 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.
/* ... */
*/
return 0;
}
捕获信号后,除了将退出状态存储在全局变量中之外,信号处理程序不执行任何操作。那么在这种情况下清理子进程是什么意思呢?