A user interrupts a command and expects the terminal to remain usable. Another task may finish in the background while the user continues typing. The shell needs to remember which jobs are running and which are ready for cleanup.
Track background jobs and their states. Handle sigint without ending the loop, and remove only finished jobs when reap is requested. These are simulated events, not operating-system signals or processes.
Input: One event per line: sigint, spawn-bg <name>, reap, echo [words...], or exit.
Output: sigint prints ^C. reap prints REAPED: N. echo prints its words. Starting a job is silent. exit prints CLEAN_EXIT only when tracked jobs remain.
Input:
sigint echo ok exit
Output:
^C ok
Explanation: sigint prints ^C, but echo still runs afterward. An interruption does not end this shell loop.
Input:
spawn-bg true reap exit
Output:
REAPED: 1
Explanation: true is immediately marked finished in this model. reap removes that one finished record.
Input:
spawn-bg true spawn-bg true reap exit
Output:
REAPED: 2
Explanation: Two finished jobs are waiting, so one cleanup removes both and reports 2.
Input:
sigint sigint echo done exit
Output:
^C ^C done
Explanation: Repeated interruptions do not disable later commands. The loop still reaches echo done.
Input:
spawn-bg true exit
Output:
CLEAN_EXIT
Explanation: The finished job has not been reaped, so it is still tracked. exit reports CLEAN_EXIT before stopping.
Paste this into Custom Input and run your solution.
spawn-bg true reap reap exit
Why does the second cleanup return zero?
Expected output:
REAPED: 1 REAPED: 0
The first reap removes the finished record. Cleanup changes state; it does not repeatedly count historical completions. No jobs remain when exit is reached.
Can each job carry a finished flag? Which flags should cleanup remove?
Lifecycle tracking explains why a shell must collect completed work. This is a teaching state machine, not an implementation of OS signal delivery or waitpid.
Pass one command's output into another and distinguish overwrite from append.
^C ok