Virtualizing CPU - Scheduling
Introduction
Assumptions
- Each job runs for same among of time.
- All jobs arrive at the same time.
- All jobs only use the CPU (i.e., no I/O operations).
- The running time of each job is known.
Scheduling Metrics
Performance Metric: Turnaround Time (往返時間)
- The time at which the job completes minus the time at which the job arrived in the system.
Another Metric: Fairness
New Metric: Response Time
- The time from when the job arrives to the first time it is scheduled.
Scheduling Strategies
First In First Out (FIFO)
a simple and easy to implement strategy.
Example:
- A arrived just before B which also arrived just before C.
- Each job runs for 10 seconds.

Problem:
- If we relax the assumption 1 (Each job runs for same among of time):
- A runs for 100 seconds, B and C run for 10 seconds each.

- A runs for 100 seconds, B and C run for 10 seconds each.
- If we relax the assumption 1 (Each job runs for same among of time):
Shortest Job First (SJF)
run the shortest job first, then the next shortest, and so on.
Example:
- A arrived just before B which also arrived just before C.
- A runs for 100 seconds, B and C run for 10 seconds each.

Problem:
- If we relax the assumption 2 (All jobs arrive at the same time):
- A arrives at t = 0, B and C arrive at t = 10.

- A arrives at t = 0, B and C arrive at t = 10.
- If we relax the assumption 2 (All jobs arrive at the same time):
Shortest Time-to-Completion First (STCF)
Add a preemption to SJF, when a new job enters the system:
- Determine the remaining jobs and new job
- Schedule the job which has the least time left
Example:
- A arrives at t = 0 and needs to run for 100 seconds.
- B and C arrive at t = 10 and need to run for 10 seconds.

Round Robin (RR)
- Run a job for a time slice, and switch to the next job in the run queue until the jobs are finished.
- The length of the time slice must be a multiple of the timer-interrupt period.
Noted: RR is fair, but performs poorly on performance metrics like turnaround time.
Example:
- A, B and C arrive at the same time and each of them run for 5 seconds.
In SJF:

In RR:

Length of time slice
Shorter time slice
- Better response time.
- The cost of context switching will dominate the overall performance.
Longer time slice
- Worse response time.
- Reduce the cost of switching.
Incorporating I/O
- Relax the assumption 3 (All jobs only use the CPU).
- When a job initiates an I/O request:
- The job is blocked until the I/O request completed.
- The scheduler should schedule another job on the CPU.
When the I/O completes,
- An interrupt is raised.
- The OS moves the process from the blocked state back to the ready state.
Example:
- A and B need 50ms of CPU time each.
- In every 10ms, A issues an I/O request (takes 10ms).
- B simply uses the CPU for 50ms and performs no I/O request.
- The scheduler runs A first.

Multi-Level Feedback Queue
Basic Rules
MLFQ has a number of distinct queues
- each queue is assigned a different priority level (優先次序).
A job that is ready to run is on a signle queue
- a job on a higher priority is chosen to run.
- use round-robin scheduling among jobs in the same queue.
- Rule 1: If Priority(A) > Priority(B), A runs
- Rule 2: If Priority(A) = Priority(B), A and B run in RR
- Example:

Change Priority
- MLFQ varies the priority of a job based on its observed behavior.
- MLFQ priority adjustment algorithm:
- Rule 3: When a job enters the system, it is placed at the highest priority
- Rule 4a: If a job uses up an entire time slice while running, its priority will be reduced
- Rule 4b: If a job gives up the CPU before the time slice is up (e.g., I/O request), it stays at the same priority level
Examples
Single Long-Running Job
- A three-queue scheduler with time slice 10ms.

- According to rule 4a, its priority will be reduce after one time slice.
- A three-queue scheduler with time slice 10ms.
Along Came a Short Job
- Job A: a long-running CPU-intensive job
- Job B: a short-running interactive job, and arrives at time T = 100

- According to rule 3, when a new job enters the system, it is placed at the highest priority.
I/O Request
- Job A: a long-running CPU-intensive job
- Job B: an intensive job that need the CPU only for 1ms before performing an I/O request

- According to rule 4b, MLFQ approach keeps an interactive job at the highest priority.
Problems
Starvation
- If there are many interactive jobs in the system, the long-running jobs will never receive any CPU time.
Game the schedule
- After running 99% of a time slice, issue an I/O operation, and the job can gain a higher percentage of CPU time.
- i.e., A job will give up the CPU when it almost reach the time slice, so that it can keep the high priority.
A program may change its behavior over time
- i.e., CPU bound process → I/O bound process
Improvements
Priority Boost
Rule 5: After some time period S, move all the jobs to the topmost queue (highest priority)
Example:
- A long-running job A with two short-running interactive job B and C
- It will induce starvation of A, Rule 5 can avoid this situation.

Prevent gaming of scheduler
- Improvement on rule 4:
Rule 4: Once a job uses up its time allotment at a given level (regardless of how many times it has given up the CPU), its priority is reduced
Example:

Tuning MLFQ
- Lower Priority, Longer Quanta (time slice)
- The high-priority queues → Short time slice
- The low-priority queues → Longer time slice

Summary
- The refined set of MLFQ Rules:
- If Priority(A) > Priority(B), A runs
- If Priority(A) = Priority(B), A & B run in Round Robin
- When a job enters the system, it is placed at the highest priority
- Once a job uses up its time allotment at a given level, its priority is reduced
- After some time period S, move all the jobs in the system to the topmost queue
Proportional Share
- A fair-share scheduler
- Guarentee that each job obtain a certain percentage of CPU time
- Not optimized for turnaround time or response time
Lottery Scheduling
Tickets
- Represent the share of a resource that a process should receive.
- The percentage of tickets represents its share of the system resource.
Scheduling
- The scheduler picks a winning ticket, then, load the state of winning process and runs it.

- The scheduler picks a winning ticket, then, load the state of winning process and runs it.
Ticket Mechanisms
Ticket Currency
- A user can allocate tickets among their own jobs in a currency.
- The system converts the currency into the correct global value.
Ticket Transfer
- A process can temporarily hand off its tickets to another process.
Ticket Inflation
- A process can temporarily change the umber of tickets it owns.
- If any one process needs more CPU time, it can boost its tickets.


