1. 使用代码模拟实现僵尸进程, 孤儿进程的场景
孤儿进程的实现,子进程变成孤儿进程后被1号进程所收养。
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 int main()
6 {
7 pid_t pid;
8 pid = fork();
9 if (-1 == pid)
10 exit(1);
11 else if (0 == pid) //child
12 {
13 sleep(30);
14 printf("child pid is %d, parent pid is %d\n", getpid(), getppid());
15 }
16 else // parent
17 {
18 printf("parent pid is %d\n", getpid());
19 return 0;
20 }
21 }
僵尸进程的实现
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 int main()
6 {
7 pid_t pid;
8 pid = fork();
9 if (-1 == pid)
10 exit(1);
11 else if (0 == pid) //child
12 {
13 printf("child pid is %d, parent pid is %d\n", getpid(), getppid());
14 }
15 else // parent
16 {
17 sleep(30);
18 printf("parent pid is %d\n", getpid());
19 }
20 return 0;
21 }
~
<defunct>表示为僵尸进程