当一个子进程结束后,Linux 给父进程发送SIGCHLD 信号,这个信号的默认处理方式是什么都不做。通过这一特点我们可以用获得子进程的退出信息,而且在用 wait(&status) 获得信息的时候,父进程还可以不用阻塞继续运行 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<sys/types.h> #include<unistd.h> #include<signal.h> void clean_up_child_process(int signal_number) { int status; wait(&status); printf("/nchild process exit with : %d/n",status); } int main() { pid_t id; int i=0,j=0; struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_handler = clean_up_child_process; sigaction(SIGCHLD, &action, NULL); id = fork(); if(id > 0) { while(i++ < 15) { printf("father is running -----/n"); sleep(1); } } else { while(j++ < 10) { printf("/tchild runing .../n"); sleep(1); } exit(0); } return 0; }