Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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.
  • policy is RR or CFS. time_slice is a positive integer: the quantum under RR, the minimum granularity under CFS.
  • Each entry of tasks is one line NAME arrival burst weight.
  • On stdin, the first line is policy, the second time_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.

FieldMeaning
NAME1 to 8 uppercase letters or digits, unique
arrivalWhen the task becomes runnable
burstTotal CPU time the task needs
weightMatters 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:

  1. Settle the task whose slice just ended. If its burst is used up, its completion time is t; under CFS it is also charged (Part 2).
  2. Admit every task with arrival <= t that has not been admitted yet, in input order.
  3. Under RR, put the task from step 1 back in the ready queue if it is unfinished (Part 1 says where).
  4. Pick the next task and run it for one slice of min(time_slice, remaining) units, where remaining is 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 NAME lines in time order, from 0 to the last completion. Back-to-back slices of the same task form one segment. An idle stretch prints as start end idle.
  • Next, one NAME done C wait W line per task, in input order. C is the completion time; W = C - arrival - burst is 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.

  1. At each decision point the front task runs for one quantum of time_slice units, or less if it finishes sooner.
  2. Tasks admitted at the same decision point join the back of the queue in input order.
  3. 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.
  4. 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.
  • arrival is between 0 and 106. burst and time_slice are between 1 and 1000. A numeric weight is 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 by 1024 / weight, so a heavier task's vruntime grows more slowly.
  • Minimum granularity: time_slice here, the least time a picked task runs before the scheduler looks again.

Picking and charging

  1. At each decision point, pick the runnable task with the smallest vruntime. Ties go to the task listed earlier in the input.
  2. The picked task runs for min(time_slice, remaining) units.
  3. When a slice of r units ends, step 1 adds r * 1024 / weight to the task's vruntime, using integer division that rounds down.
  4. 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.
  5. 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 vruntime among 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

  1. They always run before normal tasks.
  2. Waiting real-time tasks run one after another in the order they were admitted, and each runs its whole burst without interruption.
  3. They have no vruntime, never enter the round-robin queue, and are not counted when a newcomer's starting vruntime is chosen.
  4. The end of a real-time task's run is a decision point.
  5. Normal tasks that arrive while a real-time task runs are admitted when it finishes.

Interrupting a normal slice

  1. A real-time task that arrives during a normal task's slice interrupts it at the arrival time, which is also a decision point.
  2. The interrupted task is settled for the units it actually ran. Under CFS it is charged ran * 1024 / weight for them.
  3. Under RR it 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.
  4. A second interruption shrinks the unused part again. Once that quantum is used up, the task gets full quanta again.
  5. Under CFS the interrupted task is runnable again, and the next pick is by vruntime as usual, with a full slice.
  6. 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.

Example
Input
RR
2
3
A 0 5 1024
B 1 3 1024
C 2 1 1024
Output
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
Explanation

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.