Scheduler Simulator
Prerequisites: Context Switching and the Scheduler, Processes, Threads, and IPC, Kernel Bypass and Real-Time Scheduling Intro
The scheduler decides which thread runs on a core, and each change of thread is a context switch that starts the next thread on cold caches.
Simulate one CPU shared by threads such as the AAPL feed handler, a risk process, and background jobs, under two policies.
- Task: one thread, or a single-threaded process, that needs a fixed amount of CPU time.
- Decision point: when the running task's slice ends (its length is used up or the task finishes), or when the CPU is idle and a task arrives. The scheduler acts only then. Part 3 adds two more kinds.
- Context switch: the CPU starts a task other than the last one it ran. Idle time in between doesn't change that, and the first task to run is not a switch.
- Preemption: a task leaves the CPU unfinished, so its timeline segment ends before its completion time.
What to implement
simulate_scheduler(policy, time_slice, tasks)returns the lines described under Output.policyisRRorCFS.time_sliceis a positive integer: the quantum underRR, the minimum granularity underCFS.- Each entry of
tasksis one lineNAME arrival burst weight. - On stdin, the first line is
policy, the secondtime_slice, the third the number of tasks, then one task per line. - There are three parts, each building on the previous ones. One solution must pass all eleven tests.
Tasks
Time is measured in integer units starting at 0. The task lines are in non-decreasing order of arrival.
| Field | Meaning |
|---|---|
NAME | 1 to 8 uppercase letters or digits, unique |
arrival | When the task becomes runnable |
burst | Total CPU time the task needs |
weight | Matters only to CFS (Part 2); the value RT instead marks a real-time task (Part 3) |
Decision points
The CPU runs one task at a time. At a decision point at time t the scheduler does four things in order:
- Settle the task whose slice just ended. If its burst is used up, its completion time is
t; underCFSit is also charged (Part 2). - Admit every task with
arrival <= tthat has not been admitted yet, in input order. - Under
RR, put the task from step 1 back in the ready queue if it is unfinished (Part 1 says where). - Pick the next task and run it for one slice of
min(time_slice, remaining)units, whereremainingis the CPU time it still needs. If no task is runnable, the CPU is idle until the next arrival.
A task that arrives while another task's slice is running waits until that slice ends to be admitted.
Output
start end NAME start end idle NAME done C wait W switches S preemptions P
- First, the timeline:
start end NAMElines in time order, from 0 to the last completion. Back-to-back slices of the same task form one segment. An idle stretch prints asstart end idle. - Next, one
NAME done C wait Wline per task, in input order.Cis the completion time;W = C - arrival - burstis the time the task was runnable but not running. - Last,
switches S preemptions P.
Part 1: Round robin
Under RR the scheduler uses round robin, which keeps the runnable tasks in a first-in, first-out ready queue.
- At each decision point the front task runs for one quantum of
time_sliceunits, or less if it finishes sooner. - Tasks admitted at the same decision point join the back of the queue in input order.
- A task whose quantum runs out before it finishes goes to the back, behind every task admitted at that decision point. A task arriving exactly when a quantum ends is therefore ahead of the task just preempted.
- If no other task is waiting, the same task is picked again at once. Its segment grows, with no context switch or preemption.
Limits
- 1 to 50 tasks.
arrivalis between 0 and 106.burstandtime_sliceare between 1 and 1000. A numericweightis between 1 and 10000.- Every quantity fits in a 32-bit
int.
Tests 1–4 cover this part. Every task in them has weight 1024, which round robin ignores.
Part 2: Smallest virtual runtime first
Under CFS the scheduler follows a simplified model of Linux's Completely Fair Scheduler (CFS).
- Weight: a task's share of the CPU relative to the others. A task of weight 2048 should get twice the CPU time of one of weight 1024, the weight Linux gives a task at the default priority.
- Virtual runtime (
vruntime): CPU time scaled by1024 / weight, so a heavier task'svruntimegrows more slowly. - Minimum granularity:
time_slicehere, the least time a picked task runs before the scheduler looks again.
Picking and charging
- At each decision point, pick the runnable task with the smallest
vruntime. Ties go to the task listed earlier in the input. - The picked task runs for
min(time_slice, remaining)units. - When a slice of
runits ends, step 1 addsr * 1024 / weightto the task'svruntime, using integer division that rounds down. - Each slice's charge is rounded down on its own. A task of weight 1536 that runs three slices of 2 units is charged 1 + 1 + 1 = 3, not 4.
- If the task that just ran still has the smallest
vruntime, it runs again and its segment grows, as in Part 1.
A newcomer's starting vruntime
- It starts at the smallest
vruntimeamong the tasks runnable at that moment, including the task settled in step 1 if unfinished and tasks admitted earlier at the same decision point. - If no task is runnable, the newcomer starts at 0.
- Without this rule a late arrival would start at 0 and hold the CPU until it caught up with tasks that had been running for a long time.
- Settling comes before admitting, so a newcomer sees the value the settled task was just charged.
Tests 5–7 cover this part.
Part 3: Real-time tasks
A real-time scheduling policy such as SCHED_FIFO lets a thread run until it chooses to yield, and normal threads cannot preempt it. A task whose weight field is RT is a real-time task.
Real-time tasks
- They always run before normal tasks.
- Waiting real-time tasks run one after another in the order they were admitted, and each runs its whole burst without interruption.
- They have no
vruntime, never enter the round-robin queue, and are not counted when a newcomer's startingvruntimeis chosen. - The end of a real-time task's run is a decision point.
- Normal tasks that arrive while a real-time task runs are admitted when it finishes.
Interrupting a normal slice
- A real-time task that arrives during a normal task's slice interrupts it at the arrival time, which is also a decision point.
- The interrupted task is settled for the units it actually ran. Under
CFSit is chargedran * 1024 / weightfor them. - Under
RRit goes to the front of the ready queue, ahead of every waiting task. Its next run uses only the unused part of the interrupted quantum: a quantum of 4 interrupted after 1 unit leaves 3. - A second interruption shrinks the unused part again. Once that quantum is used up, the task gets full quanta again.
- Under
CFSthe interrupted task is runnable again, and the next pick is byvruntimeas usual, with a full slice. - A real-time task that arrives exactly when a normal slice ends interrupts nothing: the slice ends normally, and the real-time task runs next.
The output format does not change. An interrupted segment counts as a preemption, and starting the real-time task counts as a context switch.
Tests 8–11 cover this part.
RR 2 3 A 0 5 1024 B 1 3 1024 C 2 1 1024
0 2 A 2 4 B 4 5 C 5 7 A 7 8 B 8 9 A A done 9 wait 4 B done 8 wait 4 C done 5 wait 2 switches 5 preemptions 3
With a quantum of 2, A runs 0-2. B arrived at 1 and C arrives at 2, exactly when A's quantum ends, so both are admitted before A rejoins the queue, which becomes B, C, A.
B runs 2-4 and still needs 1 unit, so it goes behind A. C needs only 1 unit and finishes at 5.
A runs 5-7 and goes behind B, B finishes at 8, and A finishes at 9. A waited 9 - 0 - 5 = 4 units.
The CPU changed task 5 times. Three segments (A at 0-2, B at 2-4, A at 5-7) ended before their task finished, so there are 3 preemptions.