forked from gparmer/gwu-xv6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpstest3.c
38 lines (38 loc) · 984 Bytes
/
pstest3.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
#include "types.h"
#include "user.h"
#include "pstat.h"
/**
* Test 3: Parent forks two children, and they each fork two children, then the original parent calls ps.
*/
int
main(void)
{
int pid;
// Parent forks two children
for (int i = 0; i < 2; i++) {
pid = fork();
if (pid < 0) { // error
exit();
} else if (pid == 0) { // child
// Parent's child forks two children
for (int j = 0; j < 2; j++) {
pid = fork();
if (pid < 0) { // error
exit();
} else if (pid == 0) { // child
sleep(200);
exit();
}
}
wait();
wait();
exit();
}
}
sleep(100);
if (pid > 0) { // if we are the parent then print out the processes
ps();
}
while (wait() != -1); // wait until all processes have been cleaned up
exit();
}