-
Notifications
You must be signed in to change notification settings - Fork 0
/
fork1.c
60 lines (59 loc) · 1.22 KB
/
fork1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <unistd.h>
#include <stdio.h>
#include<sys/types.h>
int main()
{
pid_t pid1;
printf("P1为父进程,PID为%d\n",getpid());
pid1 = fork();
if(pid1<0)
{
printf("创建失败\n");
return 0;
}
else if(pid1 == 0)
{
printf("P3是子进程,PID为%d\n",getpid());
return 0;
}
else
{
pid_t pid2;
pid2 = fork();
if(pid2<0)
{
printf("创建失败\n");
return 0;
}
else if(pid2 == 0)
{
printf("P2是子进程,PID为%d\n",getpid());
pid_t pid3;
pid3 = fork();
if(pid3<0)
{
printf("创建失败\n");
return 0;
}
else if(pid3 == 0)
{
printf("P4是子进程,PID为%d\n",getpid());
return 0;
}
sleep(1);
pid_t pid4;
pid4 = fork();
if(pid4<0)
{
printf("创建失败\n");
return 0;
}
else if(pid4 == 0)
{
printf("P5是子进程,PID为%d\n",getpid());
return 0;
}
}
}
return 0;
}