-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathforktest.c
executable file
·62 lines (54 loc) · 1.73 KB
/
forktest.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
61
62
#include <stdio.h>
#include <sys/types.h> //--for ??
#include <unistd.h> //--for ??
#include <stdlib.h>
#include <fcntl.h>
#include <sys/wait.h>
int glob1=1;
int main(int argc,char *argv[])
{
int fd,pid,vpid,status,test;
int glob2=2;
printf("before fork glob1=%d\n",++glob1);
printf("before fork glob2=%d\n",++glob2);
printf("current pid=%d\n",getpid());
printf("current parent ppid=%d\n",getppid());
if((pid=fork())<0)
{
perror("fork");
exit(-1);
}
else if (pid==0)
{
printf("fork created successfully\n");
printf("current parent ppid=%d\n",getppid());
printf("fork child pid =%d\n",getpid());
printf("fork glob1=%d\n",++glob1);
printf("fork glob2=%d\n",++glob2);
}
else
{
if((vpid=vfork())<0)
{
perror("vfork");
}
else if(vpid==0)
{
glob1++;
glob2++;
printf("vfork succeed!\n");
printf("current ppid=%d\n",getppid());
printf("vfork glob1=%d\n",++glob1);
printf("vfork glob2=%d\n",++glob2);
}
else
{
wait(&vpid);
wait(&pid);
printf("after child pid created glob1=%d\n",glob1);
printf("after child pid created glob2=%d\n",glob2);
}
}
printf(" Finish pid %d\n",getpid());
exit(0);
}