1job = queue.popleft()2run(job, SLICE)3if job.left > 0:4 queue.append(job)SLICE1 msDONETAILHEAD →CPUAAAAABBBBBCCCCCABCABCABCABCABC0 ms5 ms10 ms15 msABC
Back: the scheduler's loop, the timer and the finished trayMiddle: the ready queue and the CPU; front: the two timelinesJobs A, B, C (a pip per ms of work left) and the timer's interruptAt the back: the scheduler's loop, the timer that ends each slice, and the tray of finished jobs. In the middle: the ready queue and the CPU. At the front: the two timelines. Three 5 ms jobs share one CPU in 1 ms slices. Each turn the head job runs, the timer interrupts, and the job goes to the back of the queue: every job starts within 2 ms, but they finish at 13, 14 and 15 ms instead of 5, 10 and 15. OSTEP tells this example in seconds (5-second jobs, a 1-second slice). The lesson uses milliseconds, closer to real slices; the arithmetic is the same.

The problem

  1. Three jobs, A, B and C, arrive together at time 0. Each needs 5 ms of CPU time. They wait in the ready queue, the line of jobs ready to run. The CPU can run only one at a time.

One rotation

Choosing the slice

What happens

;; step 1/11 · jobs
job   arrives   needs
A     0 ms      5 ms of CPU
B     0 ms      5 ms of CPU
C     0 ms      5 ms of CPU

ready queue   head -> A  B  C <- tail
CPU           one job at a time

response time    = first run - arrival
turnaround time  = finish    - arrival

OSTEP tells this example in seconds (5-second jobs, a 1-second slice). The lesson uses milliseconds, closer to real slices; the arithmetic is the same. The worked example treats a context switch as free, as OSTEP does. Real switches take time, and they also cost the cache, TLB and branch-predictor state the job had built up.