目录
创建一个孤儿进程
#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("parent:%d child %d\n",getpid(),pid);
sleep(1);
}
else if(0 == pid)
{
while(1)
{
printf("parent:%d child:%d\n",getppid(),getpid());
sleep(1);
}
}
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)
{
printf("parent:%d child %d\n",getpid(),pid);
while(1)
{
printf("parent:%d child:%d\n",getppid(),getpid());
sleep(1);
}
}
else if(0 == pid)
{
printf("parent:%d child:%d\n",getppid(),getpid());
sleep(1);
}
else
{
perror("fork");
return -1;
}
return 0;
}