Teacher answer key

Scheduling, Cores and Events

Gather four kinds of evidence: prediction of relevant state; behavior across varied cases including boundaries; explanation using trace/state evidence; and a reasoned correction of one mismatch. Mark each as emerging, with support or independent using paper or your school’s existing approved process. A challenge pass is machine-behavior evidence, not proof of authorship or complete mastery. Accept equivalent correct programs; the public key is a model, not the only possible answer.

Public teaching material. Solutions are examples; equivalent correct programs may also pass the actual checks.

25. Cooperative Scheduling

Take turns between tasks and distinguish policy from CPU instructions.

Open this lesson →

1. Predict and trace

Before running the Try program, predict its output and trace the first three executed instructions. Track the relevant registers, flags or memory as needed. Then step the program to compare.

LOAD 1
CALL taskA
CALL taskB
HALT
taskA: PRINT
SLEEP 500
RETURN
taskB: LOAD 101
PRINT
RETURN

Queue input: None

Initialize memory: All bytes initially zero

Try prediction key

Output: 1, 101

InstructionPC beforePC afterR0 beforeR0 afterRelevant stateOutput
LOAD0101{"registers":[1,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]}None
CALL1411{"registers":[1,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":254,"stack":[],"callStack":[2],"loopStack":[],"memoryReads":[],"memoryWrites":[]}None
PRINT4511{"registers":[1,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":254,"stack":[],"callStack":[2],"loopStack":[],"memoryReads":[],"memoryWrites":[]}1

2. Build and check

Read a number of rounds from 0 through 3. Start task A’s counter at 0 and task B’s at 100. Each round, call A to increment and print its counter, then B to increment and print its counter.

Required instruction types: INPUT, LOOP, CALL, RETURN

Example challenge solution

INPUT
COPY R0 R3
LOAD 0 R1
LOAD 100 R2
LOOP R3
CALL taskA
CALL taskB
RETURN
HALT
taskA: LOAD R1
ADD 1
COPY R0 R1
PRINT
SLEEP 0
RETURN
taskB: LOAD R2
ADD 1
COPY R0 R2
PRINT
SLEEP 0
RETURN

Actual checker fixtures

Case 1
Input
0
Initial memory
All bytes initially zero
Expected output
None
Case 2
Input
1
Initial memory
All bytes initially zero
Expected output
1, 101
Case 3
Input
3
Initial memory
All bytes initially zero
Expected output
1, 101, 2, 102, 3, 103

3. Explain the machine

Is CALL/RETURN turn-taking the same as hardware preemption?

Reasoning and teaching note

No. It is a cooperative dispatcher model; actual scheduler policy/quantum belongs to the explicitly labeled systems simulation.