#include <unistd.h> #include<stdio.h> #include<stdlib.h> #include <sys/types.h> #include <sys/wait.h> int main() { pid_t a=fork(),b; if (a<0) perror("fork error"); //子进程 让它5s后自动退出 else if (a==0) { printf("child process will be terminated in 5 sec/n"); sleep(5); exit(0); } //每秒检查一次子进程是否退出 do{ b=waitpid(a,NULL,WNOHANG); if(b==0) { printf("the child is not exit the child PID is %d/n",getpid()); sleep(1); } }while(b==0); if(b==a) printf("the child is exit now,the parent PID is %d/n",getppid()); else printf("something wrong occur"); return 0; }