创建一个孤儿进程
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
pid_t pid = fork();
if(pid > 0)
{
printf("this is parent: %d ,child:%d\n",pid,getpid());
_exit(0);
}
else if(0 == pid)
{
int i=2;
while(1)
{
printf("this is child ppid:%d, child pid:%d\n",getppid(),getpid());
if(i == 0)
_exit(0);
i--;
sleep(1);
}
printf("子程序退出\n");
}else
{
perror("fork");
return -1;
}
return 0;
}
创建一个僵尸进程
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
pid_t pid = fork();
if(pid > 0) //父进程
{
while(1)
{
sleep(1);
printf("parent pid:%d child:%d\n",pid,getpid());
}
}else if(0 == pid) //子进程
{
printf("child ppid:%d pid:%d\n",getppid(),getpid());
sleep(2);
}else
{
perror("fork");
return -1;
}
return 0;
}
![]()


被折叠的 条评论
为什么被折叠?



