#include "head.h"
int main(int argc, const char *argv[])
{
pid_t cpid= fork();//创建子进程
if(cpid>0)//父进程
{
int status;
printf("getpid=%d cpid=%d\n",getpid(),cpid);
while(1)
{
pid_t x_pid = waitpid(-1,NULL,WNOHANG);
printf("status=%d x_pid=%d\n",status,x_pid);
sleep(1);
}
}
else if(cpid == 0)//子进程
{
int i=0;
while( i<3)
{
printf("-------------\n");
sleep(1);
i++;
}
printf("child process is exit\n");
exit(0);
}
else
{
perror("fork");
return -1;
}
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/49d963e9752b6f4e63f6997b8380ec32.png)