The kernel keeps a process table. fork adds a row, exit turns a process into a zombie until its parent calls wait, and children whose parent exits are adopted by init.
What to build
Run a list of fork, exit and wait operations and print what is left in the process table.
Requirements
- The table starts with one process,
init, with pid 1 and parent 0. It is running and never exits. New pids are 2, 3, 4, ... in the order of theforkcalls. fork p: processpcreates a child with the next pid, state running, parentp.exit p(p is not 1):pbecomes a zombie; all its children (running or zombie) get parent 1; zombie children now owned by init are removed at once; and if the parent ofpis 1,pis removed at once too.wait p: removes every zombie child ofpand printsP reaped K, where P ispand K is the number removed.- An operation whose process does not exist, or is a zombie, is an error: print
error: op K(K counts operations from 1) and change nothing.exit 1is also an error.
Input
The first line has m (1 ≤ m ≤ 40). Each of the next m lines is fork p, exit p or wait p.
Output
The reaped and error lines in order, then one line pid ppid state for each process still in the table, in order of pid. The state is running or zombie.
Constraints
At most 40 processes are created, so pids stay below 50.
Example
Input:
6
fork 1
fork 2
exit 3
wait 2
fork 1
exit 2
Output:
2 reaped 1
1 0 running
4 1 running
Why it works
A process that has exited cannot disappear until its parent has had a chance to read its exit status, so it stays as a zombie in the table. Init plays the parent of last resort: it adopts every orphan and removes its zombies straight away, so orphans never pile up.
Examples
6 fork 1 fork 2 exit 3 wait 2 fork 1 exit 2
2 reaped 1 1 0 running 4 1 running
3 exit 1 fork 5 wait 1
error: op 1 error: op 2 1 reaped 0 1 0 running
Hints
Hint 1
Keep arrays alive[], ppid[] and zombie[] indexed by pid. A pid that was reaped has alive[pid] = 0.
Hint 2
On exit(p): set zombie[p]; for every live child c of p set ppid[c] = 1 and, if c is a zombie, remove it. Then, if ppid[p] is 1, remove p as well.
Hint 3
wait(p) removes the zombie children of p and prints how many; remember to print error lines for events that name a process that does not exist or is not running.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>
int main(void) {
int m, p, alive[60] = {0}, ppid[60], zombie[60] = {0}, next = 2;
char word[16];
alive[1] = 1;
ppid[1] = 0;
scanf("%d", &m);
for (int k = 1; k <= m; k++) {
scanf("%15s %d", word, &p);
if (p < 1 || p >= next || !alive[p] || zombie[p]) {
printf("error: op %d\n", k);
continue;
}
if (strcmp(word, "fork") == 0) {
alive[next] = 1;
ppid[next] = p;
next++;
} else if (strcmp(word, "exit") == 0) {
if (p == 1) {
printf("error: op %d\n", k);
continue;
}
zombie[p] = 1;
for (int c = 2; c < next; c++) {
if (alive[c] && ppid[c] == p) {
ppid[c] = 1;
if (zombie[c]) alive[c] = 0;
}
}
if (ppid[p] == 1) alive[p] = 0;
} else {
int reaped = 0;
for (int c = 2; c < next; c++) {
if (alive[c] && ppid[c] == p && zombie[c]) {
alive[c] = 0;
reaped++;
}
}
printf("%d reaped %d\n", p, reaped);
}
}
for (int pid = 1; pid < next; pid++)
if (alive[pid]) printf("%d %d %s\n", pid, ppid[pid], zombie[pid] ? "zombie" : "running");
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step