Background

• The previous lecture introduced the basics of concurrency Processor – Processes and threads – Definition, representation, management • We now understand how a programmer can spawn concurrent computations • The OS now needs to partition one of the central resources, the CPU, between these concurrent tasks

2

Scheduling Scheduling

• The scheduler is the manager of the CPU • Threads alternate between performing I/O and resource performing computation • In general, the scheduler runs: • It makes allocation decisions – it chooses to run – when a switches from running to waiting certain processes over others from the ready – when a process is created or terminated queue – when an interrupt occurs – Zero threads: just loop in the idle loop • In a non-preemptive system, the scheduler waits for a running process to explicitly block, terminate or yield – One : just execute that thread – More than one thread: now the scheduler has to make a • In a preemptive system, the scheduler can interrupt a resource allocation decision process that is running. Terminated • The scheduling algorithm determines how jobs New Ready Running are scheduled Waiting 3 4 Process States Scheduling Evaluation Metrics

• There are many possible quantitative criteria for Terminated evaluating a scheduling algorithm: New Ready Running – CPU utilization: percentage of time the CPU is not idle – Throughput: completed processes per time unit Waiting – Turnaround time: submission to completion – Waiting time: time spent on the ready queue Processes are I/O-bound when they spend most of – Response time: response latency their time in the waiting state – Predictability: variance in any of these measures Processes are CPU-bound when they spend their time in the ready and running states • The right metric depends on the context

Time spent in each entry into the running state is called a CPU burst 5 6

Scheduling Algorithms FCFS Scheduling Algorithms LIFO

• First-come First-served (FCFS) (FIFO) • Last-In First-out (LIFO) – Jobs are scheduled in order of arrival – Newly arrived jobs are placed at the head of the ready queue – Non-preemptive – Improves response time for newly created threads • Problem: • Problem: – Average waiting time can be large if small jobs wait behind – May lead to starvation – early processes may never get the long ones CPU

P1 P2 P3 time 0 16 20 24

P2 P3 P1 0 4 8 24 – May lead to poor overlap of I/O and CPU and convoy effects

7 8 Problem Scheduling Algorithms SJF

• You work as a short-order cook • Shortest Job First (SJF) – A short order cook has to cook food for customers as they – Choose the job with the shortest next CPU burst come in and specify which dish they want – Provably optimal for minimizing average waiting time – Each dish takes a different amount of time to prepare • You want to minimize the average amount of P1 P3 P2 time the customers wait for their food 0 15 21 24 • What strategy would you use ? PP PP P – Note: most restaurants use FCFS. 3 2 2 3 1 0 3 9 24 • Problem: – Impossible to know the length of the next CPU burst

9 10

Shortest Job First Prediction Scheduling Algorithms SRTF

• Approximate the duration of the next CPU-burst from • SJF can be either preemptive or non-preemptive the durations of the previous bursts – The distinction occurs when a new, short job arrives while the – The past can be a good predictor of the future currently process has a long time left to execute • No need to remember entire past history • Preemptive SJF is called • Use exponential average: first th tnduration of the n CPU burst st ?n+1 predicted duration of the (n+1) CPU burst ?n+1 = ? tn + (1- ? ) ?n where 0 ? ? ? 1 ? determines the weight placed on past behavior

11 12 Priority Scheduling Round Robin

• Priority Scheduling • Round Robin (RR) – Choose next job based on priority – Often used for timesharing – For SJF, priority = expected CPU burst – Ready queue is treated as a circular queue (FIFO) – Can be either preemptive or non-preemptive – Each process is given a time slice called a quantum • Problem: – It is run for the quantum or until it blocks – Starvation: jobs can wait indefinitely – RR allocates the CPU uniformly (fairly) across all participants. If average queue length is n, each participant gets 1/n • Solution to starvation – As the time quantum grows, RR becomes FCFS – Age processes: increase priority as a function of waiting time – Smaller quanta are generally desireable, because they improve response time • Problem: – overhead of frequent context switch

13 14

RR with Time Quantum = 20 Scheduling Algorithms

Process Burst Time • Multi-level Queue Scheduling P1 53 • Implement multiple ready queues based on job “type” P2 17 – interactive processes – CPU-bound processes P3 68 P 24 – batch jobs 4 – system processes • The Gantt chart is: – student programs

P P P P P P P P P P • Different queues may be scheduled using different 1 2 3 4 1 3 4 1 3 3 algorithms 0 20 37 57 77 97 117 121 134 154 162 • Intra-queue CPU allocation can be either strict or proportional • Typically, higher average turnaround than SJF, • Problem: Classifying jobs into queues is difficult but better response time. – A process may have CPU-bound phases as well as interactive ones

15 16 Multilevel Queue Scheduling Scheduling Algorithms • Multi-level Feedback Queues Highest priority • Implement multiple ready queues System Processes – Different queues may be scheduled using different algorithms – Just like multilevel queue scheduling, but assignments are not static • Jobs move from queue to queue based on feedback Interactive Processes – Feedback = The behavior of the job, e.g. does it require the full quantum for computation, or does it perform frequent I/O ? Batch Processes • Very general algorithm Student Processes • Need to select parameters for: Lowest priority – Number of queues – Scheduling algorithm within each queue – When to upgrade and downgrade a job

17 18

Multilevel Feedback Queue Scheduling Real-time Scheduling

Highest priority • Real-time processes have timing constraints – Expressed as deadlines or rate requirements Quantum = 2 • Common RT scheduling policies – Rate monotonic Quantum = 4 – Simple, just one scalar priority related to the periodicity of the job Quantum = 8 – Priority = 1/rate – Static FCFS – Earliest deadline first (EDF) – Dynamic but more complex – Priority = deadline Lowest priority • Both schemes require admission control to provide guarantees 19 20 Turnaround Time Varies With The Time Quantum Scheduling on a Multiprocessor

• Two alternatives based on the total number of queues: – Each processor has its own separate queue – All processors share a common ready-queue, and autonomously pick threads to execute from this common queue whenever they are idle (work stealing) • Scheduling locally on any single processor is mostly the same as scheduling on a uniprocessor • Issues: – Want to keep threads local to a processor () – Want to keep related threads together (gang-scheduling)

21 22