首页 > > > 有两个这样的进程:僵尸进程&孤儿进程,蓝瘦香菇
那时刚写文章时候,当时记录的学习笔记,现在看来,之前记录的有一个错误的地方,当时也没察觉到。



[root@iz2ze76ybn73dvwmdij06zz ~]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 5月20 ? 00:00:33 /usr/lib/systemd/systemd --system --deserialize 21
root 2 0 0 5月20 ? 00:00:00 [kthreadd]
root 3 2 0 5月20 ? 00:00:06 [ksoftirqd/0]
root 5 2 0 5月20 ? 00:00:00 [kworker/0:0H]
root 7 2 0 5月20 ? 00:00:02 [migration/0]
root 8 2 0 5月20 ? 00:00:00 [rcu_bh]
root 9 2 0 5月20 ? 00:30:40 [rcu_sched]
root 10 2 0 5月20 ? 00:00:17 [watchdog/0]
root 11 2 0 5月20 ? 00:00:16 [watchdog/1]
root 12 2 0 5月20 ? 00:00:02 [migration/1]
root 13 2 0 5月20 ? 00:00:03 [ksoftirqd/1]
root 15 2 0 5月20 ? 00:00:00 [kworker/1:0H]
root 17 2 0 5月20 ? 00:00:00 [kdevtmpfs]
root 18 2 0 5月20 ? 00:00:00 [netns]
root 19 2 0 5月20 ? 00:00:01 [khungtaskd]
root 20 2 0 5月20 ? 00:00:00 [writeback]
root 21 2 0 5月20 ? 00:00:00 [kintegrityd]
root 22 2 0 5月20 ? 00:00:00 [bioset]
root 23 2 0 5月20 ? 00:00:00 [kblockd]
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char const *argv[])
{
pid_t p1 = fork();
printf("%d\n",p1);
if(p1 > 0)
{
printf("父进程 pid = %d, p1 = %d\n", getpid(), p1);
}
else
{
printf("子进程 pid = %d , ppid = %d, p1 = %d\n", getpid(), getppid(), p1);
}
return 0;
}
[root@iz2ze76ybn73dvwmdij06zz ~]# ./fork2
10213
父进程 pid = 10212, p1 = 10213
子进程 pid = 10213 , ppid = 10212, p1 = 0

[root@iz2ze76ybn73dvwmdij06zz ~]# cat guer.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
int main()
{
pid_t pid = fork();
if (pid < 0) {
perror("fork error;");
exit(1);
} else if (pid == 0) {
sleep(5);
printf ("子进程 : [ pid] = %d , 父进程 [ppid] = %d\n",getpid(),getppid());
exit(0);
} else if (pid > 0) {
printf("我是父线程,我先退出一步~\n");
exit(0);
}
return 0;
}

[root@iz2ze76ybn73dvwmdij06zz ~]# cat zombie.c
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
int main()
{
pid_t pid;
pid = fork();
if (pid < 0)
{
perror("fork error:");
exit(1);
}
else if (pid == 0)
{
printf("我是子进程,我要先退出一步了.\n");
printf("子进程 id : %d\n" ,getpid());
exit(0);
} else {
printf("我是父进程,我先睡2秒\n");
printf("父进程 id : %d\n" ,getpid());
sleep(2);
while(2); //来个死循环,不退出的那种
}
return 0;
}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69902700/viewspace-3154567/ ,如需转载,请注明出处,否则将追究法律责任。