linux C孤儿进程以及进程回收

  • 一例孤儿进程代码的演示
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
int main()
{
    pid_t pid;
    pid =fork();
    if(pid==0)
    {
        while(1)
        {
            printf("i'm child=%d, parent=%d\n",getpid(),getppid());
            sleep(1);
        }
    }
    else if(pid==-1)
    {
        perror("process failed!");
        exit(1);
    }
    else
    {
        printf("i'm the master process %d\n",getpid());
        sleep(10);
        printf("master process %d died\n",getpid());
    }
    return 0;
}

输出结果:

由此可见,进程13345的父进程结束后,变为孤儿进程,其父进程转为1365

猜你喜欢

转载自www.cnblogs.com/saintdingspage/p/12182503.html