linux-sysprog · beginner · ~15 min
Reason about exponential process growth.
If every existing process calls fork() once per round, the population doubles each round. Compute the total after n rounds.
Implement long processes_after(int n) that returns the number of processes after n rounds, starting from a single process.
n: number of fork rounds (n >= 0).The total process count, which is 2^n.
processes_after(0) -> 1
processes_after(1) -> 2
processes_after(3) -> 8
n == 0: the original process, so 1.n: number of fork rounds (>= 0).
Total processes after n rounds (2^n) as a long.
Start from 1 process; each round doubles the count.
long processes_after(int n) {
/* TODO */
return 1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.