Exercise 1: A Three-Way Rotation (A -> B -> C -> A) — Possible Solution ==================================================================== THE TEST ------------------------------ pa = kernel.create_process(num_pages=1) pb = kernel.create_process(num_pages=1) pc = kernel.create_process(num_pages=1) cpu = CPU() pa.transition(ProcessState.RUNNING) cpu.current_pid = pa.pid cpu.registers['PC'] = 100 context_switch_fixed(cpu, pa, pb) # A -> B cpu.registers['PC'] = 200 context_switch_fixed(cpu, pb, pc) # B -> C cpu.registers['PC'] = 300 context_switch_fixed(cpu, pc, pa) # C -> A RESULT ------------------------------ process A resumed after a full A->B->C->A rotation, CPU shows: {'PC': 100, 'ACC': 0, 'R1': 0, 'R2': 0} A's own PC is correctly restored to 100 -- its exact value from before it was ever switched away, even though it sat dormant through TWO other processes' own turns (B running to PC=200, then C running to PC=300) before finally being resumed. WHY THIS GENERALIZES CLEANLY BEYOND TWO PROCESSES ------------------------------ context_switch_fixed()'s own logic never assumes there are only two processes in the system -- it only ever knows about the ONE old process and the ONE new process passed into a given call. Each call is a fully self-contained save-then-load operation: A -> B saves A's own state into pa.registers and loads B's saved state (all zeros, at that point) into the CPU; B -> C saves B's own now-current state (PC=200) into pb.registers and loads C's saved state; C -> A saves C's own state (PC=300) into pc.registers and loads A's saved state -- which is still exactly what it was left as after the very first switch, since nothing has touched pa.registers in between. WHY THE OTHER TWO PROCESSES' OWN PROGRESS ALSO SURVIVES ------------------------------ Although this exercise only checks A's own final state, the same mechanism guarantees pb.registers correctly holds PC=200 and pc.registers correctly holds PC=300 at this point too -- each process's own PCB is a genuinely independent storage location, never shared or reused between processes. A three-way rotation doesn't introduce anything qualitatively different from the two-way case; it simply chains more individually-correct switches together. WHY THIS WORKS AS AN ANSWER ------------------------------ Testing a rotation through a THIRD process, rather than just alternating between the original two, rules out the possibility that Finding 3's own correctness was specific to the two-process case -- confirming context_switch_fixed() is a genuinely general mechanism, not a solution narrowly tailored to exactly two processes taking turns.