What Are Goals?

The Linux Scheduler

2

1 Goals of the Linux Scheduler Priority Structure

 Generate illusion of concurrency  Real-time processes have the top 99 priority

 Maximize resource utilization (hint: mix CPU and levels. I/O bound processes appropriately)  Non-real-time processes have levels 100-  Meet needs of both I/O-bound and CPU-bound 139 processes

 Give I/O-bound processes better interactive response

 Do not starve CPU-bound processes

 Support Real-Time (RT) applications

3

2 Two Fundamental Resource Two Fundamental Resource Sharing Mechanisms Sharing Mechanisms

 ?  Prioritization

 ?  Resource partitioning

3 SCHED_FIFO SCHED_RR

 Used for real-time processes  Used for real-time processes

 Conventional preemptive fixed-priority  CPU “partitioning” among same priority scheduling processes

 Current process continues to run until it ends  Current process continues to run until it ends or a higher-priority real-time process becomes or its time quantum expires

runnable  Quantum size determines the CPU share

 Same-priority processes are scheduled  Processes of a lower priority run when no FIFO processes of a higher priority are present

4 Process Priority & Timeslice SCHED_NORMAL Recalculation

 Static priority  Used for non-real-time processes  A “nice” value

 Complex heuristic to balance the needs of  Inherited from the parent process I/O and CPU centric applications  Set up by user  Dynamic priority

 Based on static priority and applications characteristics (interactive or CPU-bound)

 Favor interactive applications over CPU-bound ones

 Timeslice is mapped from priority

10

5 Heuristics Heuristics if (static priority < 120) Quantum = 20 (140 – static priority) bonus = avg. sleep time mod 100 (ms) else Quantum = 5 (140 – static priority) dynamic priority = max (100, min (static priority – bonus + 5, 139)

(in ms)

6 Advanced Topic: How & When to Preempt? Priority Inversion

 Kernel sets the need_resched flag (per-process var) at various  What if a higher-priority process needs a locations

 scheduler_tick(), a process used up its timeslice resource locked by a lower-priority process?

 try_to_wake_up(), higher-priority process awaken  How long will the higher priority process have  Kernel checks need_resched at certain points, if safe, schedule() will be invoked to wait for lower-priority execution?  User preemption

 Return to user space from a system call or an interrupt handler  Kernel preemption

 A task in the kernel explicitly calls schedule()

 A task in the kernel blocks (which results in a call to schedule() )

13

7 Priority Inversion Priority Inversion

 Locks and priorities may be at odds.  Locks and priorities may be at odds. Locking results in priority inversion Locking results in priority inversion Attempt to S results in blocking High-priority task High-priority task

Preempt. Preempt.

Priority Lock S Lock S Inversion

Low-priority task Low-priority task

8 Priority Inversion Unbounded Priority Inversion

 How to account for priority inversion?  Consider the case below: a series of intermediate priority tasks is delaying a Attempt to lock S higher-priority one Attempt to lock S results in blocking results in blocking High-priority task High-priority task

Unlock S Preempt. Preempt. Lock S Unbounded Priority Inversion

Priority Intermediate-priority tasks … Lock S Inversion Lock S Preempt. Unlock S Low-priority task Low-priority task

9 Unbounded Priority Inversion Priority Inheritance Protocol

 How to prevent unbounded priority  Let a task inherit the priority of any higher- inversion? priority task it is blocking Attempt to lock S Attempt to lock S results in blocking results in blocking High-priority task High-priority task

Unlock S Preempt. Preempt. Unbounded Priority Inversion Lock S Intermediate-priority tasks … Intermediate-priority tasks … Lock S Lock S Preempt. Unlock S Low-priority task Low-priority task

10 Computing the Maximum Priority Inheritance Protocol Priority Inversion Time

 Question: What is the longest time a task  Consider the instant when a high-priority task can wait for lower-priority tasks? that arrives.  What is the most it can wait for lower priority ones?  Let there be N tasks and M locks Resource  Let the largest critical section of task i be of Queue If I am a task, priority 1 length Bi inversion occurs when (a) Lower priority task holds a Resource  Answer: ? Semaphore Queue resource I need (direct blocking) 2 (b) Lower priority task inherits a higher priority than me because it holds a resource the higher- Resource Semaphore Queue priority task needs (push-through M blocking)

11 Maximum Blocking Time Priority Ceiling Protocol

Priority Inheritance Protocol  Definition: The priority ceiling of a semaphore is Need Red the highest priority of any task that can lock it Need Blue Need Yellow  A task that requests a lock Rk is denied if its priority is not higher than the highest priority ceiling of all currently locked semaphores (say it

belongs to semaphore Rh)  The task is said to be blocked by the task holding lock

Rh  A task inherits the priority of the top higher- priority task it is blocking

12 Maximum Blocking Time Maximum Blocking Time

Need Blue but Priority Ceiling Protocol Need Blue but Priority Ceiling Protocol Priority is lower Priority is lower Than Red ceiling Need Red but Than Red ceiling Need Red but Priority is lower Priority is lower Need Yellow but Than Red ceiling Need Yellow but Than Red ceiling Priority is lower Priority is lower Than Red ceiling Than Red ceiling

Done Done

13 MP2 Bonus Points

 Implement the priority ceiling protocol in your and lock library (due separately from MP2) – details to follow.

14