僵尸进程和孤儿进程

http://blog.youkuaiyun.com/kennyrose/article/details/7532758

孤儿进程

在下面的代码执行时,

$ ps -ef |grep test3

harryhare   26918 26335  0 23:08 pts/14   00:00:00 ./test3
harryhare   26919 26918  0 23:08 pts/14   00:00:00 ./test3
harryhare   27055 26430  0 23:10 pts/15   00:00:00 grep --color=auto test

$ pstree

systemd─┬─YDLive───{YDLive}
........此处省略一堆没用的.....
        ├─sshd─┬─2*[sshd───sshd───bash]
        │      ├─sshd───sshd───bash───test3───test3
        │      ├─sshd───sshd───bash───pstree
        │      └─sshd───sshd
        ├─systemd───(sd-pam)
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd

$ kill 26918 

# 杀死父进程后,child 的父进程切换为systemd

$  ps -ef |grep test3

harryhare   26919     1  0 23:08 pts/14   00:00:00 ./test3
harryhare   28137 26430  0 23:22 pts/15   00:00:00 grep --color=auto test3


$ pstree 

systemd─┬─YDLive───{YDLive}
........此处省略一堆没用的.....
        ├─sshd─┬─3*[sshd───sshd───bash]
        │      └─sshd───sshd───bash───pstree
        ├─systemd───(sd-pam)
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─test3
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

void  child(){
    printf("child...\n");
    pid_t pid=getpid();
    printf("child: pid = %d\n", pid);
    sleep(1000);
}

void parent(){
    printf("parent...\n");
    pid_t pid=getpid();
    printf("parent: pid = %d\n", pid);
    sleep(1000);
}

void error(){
    perror("fork:");
    exit( -1);
}
int main(){
    int pid;
    pid=fork();
    if(pid==0){
        child();
    }else if(pid>0){
        printf("child process id %d\n",pid);
        parent();
    }else{
        error();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值