Hardware interview practice
Capture per-iteration values in fork...join_none
A loop spawns three join_none children. Correct the code so each child prints its own iteration value, then state what output ordering is and is not guaranteed.
Starting point
Question code
for (int i = 0; i < 3; i++) fork
// child print
join_none
wait fork;Reviewed example
Work through one case
Input
three iterations spawn three join_none childrenExpected output
one legal print order is 0, 2, 1The captured multiset is deterministic, but sibling scheduling is not, so any permutation of 0, 1, and 2 is legal.
What to cover
Requirements
- Print exactly one occurrence of each value 0, 1, and 2.
- Capture i in an automatic variable declared in the fork scope for that iteration.
- Wait for every spawned child before the parent finishes.
- Do not assert any particular ordering among sibling processes.
