-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQ1Fork.c
More file actions
47 lines (39 loc) · 674 Bytes
/
Q1Fork.c
File metadata and controls
47 lines (39 loc) · 674 Bytes
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pidp, pidc;
int i,j;
static int cnt;
printf("\n I am the parent process with PID %d \n", getpid());
pidp = getpid();
fflush(stdout);
pidc = fork();
printf("%d", cnt);
fflush(stdout);
++cnt;
j=cnt;
for (i = 1; i < 4; ++i) {
if (pidc == 0) {
printf("\n I am a child with PID %d and my parent ID is %d \n",
getpid(), pidp);
printf("%d", j);
fflush(stdout);
break;
}
if (pidc < 0) {
printf("fork error\n");
exit(1);
} else {
sleep(2);
}
pidc = fork();
fflush(stdout);
j=0;
++cnt;
j=cnt;
}
}