linux-sysprog · beginner · ~15 min

Interpret fork's return

Decode the three-way meaning of fork's return value.

Challenge

fork() returns <0 on error, 0 in the child, and the child PID (>0) in the parent. Implement int fork_role(int ret) returning -1, 0, or 1 for error, child, parent.

Starter code

int fork_role(int ret) {
    /* TODO */
    return -1;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.