linux-sysprog · beginner · ~15 min

Processes after N forks

Reason about exponential process growth.

Challenge

If every existing process calls fork() once per round, the population doubles each round. Compute the total after n rounds.

Task

Implement long processes_after(int n) that returns the number of processes after n rounds, starting from a single process.

Input

  • n: number of fork rounds (n >= 0).

Output

The total process count, which is 2^n.

Example

processes_after(0)   ->   1
processes_after(1)   ->   2
processes_after(3)   ->   8

Edge cases

  • n == 0: the original process, so 1.

Input format

n: number of fork rounds (>= 0).

Output format

Total processes after n rounds (2^n) as a long.

Constraints

Start from 1 process; each round doubles the count.

Starter code

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.