曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 6611|回复: 0
打印 上一主题 下一主题

[进程] 孤儿进程

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2009-6-14 01:25:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如果一个子进程的父进程先于子进程结束,子进程就成为一个孤儿进程,它由 init 进程收养,成为 init  进程的子进程。下面程序说明:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
    pid_t pid;
   
    pid = fork();
    switch( pid ) {
        case 0:
          while(1) {
            printf("A background process, PID:%d
, ParentID: %d
",
                getpid(),getppid());
            sleep(3);
          }
        case -1:
            perror("Process creation failed
");
            exit(-1);
        default:
            printf("I am parent process, my pid is %d
", getpid());
            exit(0);
    }
    return 0;
}
       运行及输出
beyes@linux-beyes:~/C> ./fork3.exe
A background process, PID:15465
, ParentID: 15464
I am parent process, my pid is 15464
beyes@linux-beyes:~/C> A background process, PID:15465
, ParentID: 1
A background process, PID:15465
, ParentID: 1
A background process, PID:15465
, ParentID: 1
由第二行蓝色的提示符可以看到,父进程到这里已经退出。接下来子进程被 init 收养(parentID = 1)。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-4 00:00 , Processed in 0.087477 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表