#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <sys/wait.h> int main() { //子进程的pid int c_pid; int pid; if ((pid = fork())) { //父进程 c_pid = pid; printf("The child process is %d\n", c_pid); //阻塞等待子进程 int status; if ((pid = wait(&status)) != -1 && pid == c_pid) { //成功回收子进程 printf("The child exit with %d\n", WEXITSTATUS(status)); fflush(stdin); } else { printf("wait() fail.\n"); } printf("Now , The child has been exit , and I will sleep.\n"); sleep(20); exit(0); } else { //子进程 printf("I 'm a child.\n"); sleep(5); exit(0); } }
异步回收fork出的子进程(僵尸进程)(2)
内容版权声明:除非注明,否则皆为本站原创文章。