sevenover 发表于 2015-01-21 22:02

父进程退出后子进程为什么没有被1号init收养

系统:$ uname -a
Linux yuhuashi-Linux 3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux测试代码:#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

int main (void)
{
        pid_t pid;

        fflush(NULL);
        pid = fork();
        if (pid < 0)
        {
                perror("fork()");
                exit(errno);
        }
        if (0 == pid)
        {
                printf("chlidren run\n");
                int i = 0;
                for (i = 0; i < 30; i++)
                {
                        printf("%d\n", i);
                        sleep(1);
                }
                exit(0);
        }
        sleep(5);
        printf("parend exit\n");
        exit(0);
        return 0;
}
父进程未退出时:$ ps -axj
PPID   PIDPGID   SID TTY      TPGID STAT   UID   TIME COMMAND
8474850585058505 pts/6   8692 Ss    1000   0:00 bash
8505869286928505 pts/6   8692 S+    1000   0:00 ./a.out --父进程
8692869386928505 pts/6   8692 S+    1000   0:00 ./a.out --子进程父进程退出后:$ ps -axj
PPID   PIDPGID   SID TTY      TPGID STAT   UID   TIME COMMAND
2153216621662166 ?         -1 Ss    1000   0:00 init --user
2166869386928505 pts/6   8505 S   1000   0:00 ./a.out子进程没有被1号init收养,被2153 init --user 收养了,这个init --user 是个神马东西?

zsszss0000 发表于 2015-01-21 23:34

pid==0的这个分支应该是子进程的,你这里是子进程先退出啊

sagarfan 发表于 2015-01-22 21:12

回复 1# sevenover


Ubuntu使用Upstart作为默认init系统.


Upstart can now run as a normal user process (whose PID is not 1) both as root or as a non-privileged user by passing the --user option. This feature is employed to supervise a user's desktop session (technically those sessions listed in /etc/upstart-xsessions). The Session Init is started /etc/X11/Xsession.d/99upstart which starts a Session Init for a user.

Upstart is now used to supervise a user's desktop session. To see details of the running Upstart session, either echo $UPSTART_SESSION (用这个命令可以查看本机--user的属性)to see the D-Bus address the Session Init process is listening to.

--user (Running a Session Init)
Runs Upstart as a Session Init.

   

sevenover 发表于 2015-01-22 23:42

回复 3# sagarfan


    echo $UPSTART_SESSION 查看到的ID正是 init --user 的 PID。

sagarfan 发表于 2015-01-23 00:12

回复 4# sevenover
恩恩,我之前也是遇到过同样的问题,是ubuntu的init系统的实现导致的,可能早期的ubuntu版本没有这个问题
而且在redhat也没有发现这个问题。


   

gaojl0728 发表于 2015-01-23 16:01

linux 下默认的行为是init 1来接收孤儿进程, 但这种行为可以修改,进程可以 通过设置PR_SET_CHILD_SUBREAPER来代替init成为新的reaper
upstart 应该就是修改了默认行为。

sevenover 发表于 2015-01-23 21:06

回复 5# sagarfan


    嗯对,我也在redhat6、7上均没有发现这个问题,只有Ubuntu有这种情况,看来果然跟Ubuntu的实现有关系,谢谢啦。

斗志之歌 发表于 2015-01-27 09:52

汪峰和章子怡在一起后,为了让章子怡相信他永远不会背叛,就去纹身店想在胸口前纹个章字,无奈纹身师冷笑地说“纹章就不会背叛老婆吗?http://www.bxwx.la/b/2/2726/

sunlw78 发表于 2015-01-27 10:30

这个程序能不能继续在讨论下。这个程序能正常结束吗,在子进程还没有输出完的时候,父进程也输出了。父子进程都有个exit啊。这个得怎么处理啊?

super皮波 发表于 2015-01-27 10:40

回复 9# sunlw78

两个都有exit,刷新两次缓冲区,其他的没什么讨论的了吧
   
页: [1] 2
查看完整版本: 父进程退出后子进程为什么没有被1号init收养