-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3-test.c
executable file
·51 lines (45 loc) · 1.49 KB
/
p3-test.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
#ifdef CS333_P3
// A starting point for writing your own p3 test program(s).
// Notes
// 1. The parent never gets to the wait() call, so killing any child will cause that
// child to move to zombie indefinitely.
// 2. When running as a background process, the parent of the main process
// will be init. This is how background processes work. Read sh.c for details.
// 3. Useful test: run in background then kill a child. Child will be in zombie. Now
// kill parent. Should get "zombie!" equal to the number of zombies.
// ps or ^p should now show init as the parent of all child processes.
// init will then reap the zombies.
// 4. Can also be used to show that the RUNNABLE list is still round robin.
#include "types.h"
#include "user.h"
#include "param.h"
#include "pdx.h"
int
main(int argc, char *argv[])
{
int rc, i = 0, childCount = 20;
if (argc > 1) {
childCount = atoi(argv[1]);
}
if (!childCount) {
printf(1, "No children to create, so %s is exiting.\n", argv[0]);
exit();
}
printf(1, "Starting %d child processes that will run forever\n", childCount);
do {
rc = fork();
if (rc < 0) {
printf(2, "Fork failed!\n");
exit();
}
if (rc == 0) { // child process
while(1) i++; // infinite
exit(); // not reachable.
}
childCount--;
} while(childCount);
printf(1, "All child processes created\n");
while(1) i++; // loop forever and don't call wait. Good for zombie check
exit(); // not reachable
}
#endif // CS333_P3