进程
更新于 2023-12-04
孤儿进程
- 父进程先于子进程结束
- 子进程被init进程接管
测试
c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
#include <sys/prctl.h>
int main()
{
int pid = fork();
if(pid < 0) {
printf("fail to fork\n");
exit(0);
}else if(pid == 0){ //子进程
printf("it is child %d\n", getpid()); //打印PID
prctl(PR_SET_NAME, "test_child"); //设计进程名
while(1){} //死循环
printf("child is done!\n");
} else { //父进程
printf("it is father %d\n", getpid()); //打印PID
prctl(PR_SET_NAME, "test_father"); //设置进程名
printf("father is done\n"); //结束
}
return 0;
}shell
# 下列命令查看
pstree -pt
# 进程信息如下:
# 当父进程结束后,子进程后系统init进程接管
#systemd(1)─┬─ModemManager(858)─┬─{gdbus}(879)
# │ └─{gmain}(874)
# ├─test_child(1470)僵尸进程
- 子进程结束后
- 父进程未调用
wait或waitpid接受子进程结束信息 - 当父进程结束后,僵尸子进程会被系统一并回收销毁
测试
c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
int main()
{
int pid = fork();
if(pid < 0) {
printf("fail to fork\n");
exit(0);
}else if(pid == 0){ //子进程
printf("it is child %d\n", getpid()); //打印PID
printf("child is done!\n"); //子进程结束
} else { //父进程
printf("it is father %d\n", getpid()); //打印PID
sleep(10);
printf("father is done\n"); //父亲结束
}
return 0;
}shell
# 下列命令查看
ps aux | grep child-id
#Z+ 13:27 0:00 [test_child] <defunct>