Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Lecture Notes CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.

Similar presentations


Presentation on theme: "Operating Systems Lecture Notes CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002."— Presentation transcript:

1 Operating Systems Lecture Notes CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002

2 CPU Scheduling Main Question: Which process to select from ready queue? –Preemption vs. non-preemption –Evaluating scheduling algorithms –Algorithms (FCFS, SJF, RR, Priority, Multilevel Queue) Readings: Silberschatz et al., chapter 6 head tail ready queue registers...... PCB 7 PCB 9 registers... PCB 1

3 CPU Scheduling: What Policy? Multiprogramming goal: keep busy. Run some process at all times Policy determines how you allocate resources to processes No single policy is best for all situations! Best policy depends on goal of the system –Single-user desktop PC –Compute server for scientific applications –Interactive time-sharing system

4 CPU Burst --- I/O Burst Cycle Empirically, most programs alternate between CPU bursts and I/O bursts. This makes multiprogramming desirable.

5 CPU Burst Times Varies with application and hardware. But histogram almost always looks exponential. Most bursts short, a few are long.

6 Short-term scheduler Selects next job from ready queue. –Ready queue not necessarily FIFO! Must run when: –A process switches from running to waiting (I/O, wait(), etc.) –A process terminates If preemptive, can also run when: –A process switches from running to ready (Interrupt) –A process switches from waiting to ready (I/O completion)

7 Preemptive scheduler: Pros and Cons Preemption seems a good choice to avoid this. BUT: –Interrupt during shared user data update Problem: Can cause inconsistency or corruption Remedy: Synchronization (Chapter 7) –Interrupt during system call Problem: Could cause kernel data inconsistency or corruption Remedy: Disable interrupts during kernel data updates Must keep disable time super short or might miss interrupts Headache for app programmers Headache for OS designer OS’s like Windows 3.1 and early Apple MacOS were non-preemptive. Badly-behaved apps could kill these systems. Preemption increases complexity for OS designer AND programmers

8 Note on Dispatch Latency The dispatcher is a piece of kernel code that: –Switches context –Flips protection bit to user mode –Jumps to correct location in user program Dispatch latency is the time it takes from the interrupt to the final jump

9 Scheduling algorithm: Evaluation criteria Generally want to maximize –CPU utilization % of time CPU is in use –Throughput # of processes completed per unit time Generally want to minimize –Turnaround time time from submission to completion admit time + ready time + CPU time + I/O time –Wait time amount of time spend in ready queue –Response time time from submission to first output (important in interactive systems) Usually optimize average but there are other choices.

10 Algorithm 1: First Come, First Served (FCFS) Characteristics: –A non-preemptive scheme. –Like Bangkok Bank when only one service desk is open. –One long-running process can clog the queue for a long time. Example: Process Arrival Burst time P1024 P213 P323 Avg wait time: ( 0 + 24 + 27 ) / 3 = 17 ms BUT opposite order wait time = ??? P1P1 P2P2 P3P3 2427300 Gantt Chart ( 0 + 3 + 6 ) / 3 = 3 ms. Much improved!

11 Algorithm 2: Non-Preemptive Shortest Job First (SJF) Characteristics: –Always assign job with shortest next CPU burst to CPU –Provably optimizes average wait time among non-preemptive algorithms. Example: Process Arrival Burst P1 0 7 P2 2 4 P3 4 1 P4 5 4 Avg wait time: ( 0 + 6 + 3 + 7 ) / 4 = 4 ms FCFS wait time: ( 0 + 5 + 7 + 7 ) / 4 = 4.75 ms Gantt Chart P1P1 P3P3 P2P2 73160 P4P4 812

12 Algorithm 3: Preemptive Shortest Job First (SJF) Characteristics: –Like Shortest Job First, but running job can be preempted. –Provably optimizes average wait time among preemptive algorithms. Example: Process Arrival Burst P1 0 7 P2 2 4 P3 4 1 P4 5 4 Average waiting time = (9 + 1 + 0 +2)/4 = 3 ms Reason that SJF is provably optimal: moving a shorter process earlier always decreases short process’ wait time more than it increases long process’ wait time. Gantt Chart 2110 P4P4 7 P2P2 P1P1 16 P1P1 P3P3 P2P2 45

13 Implementing SJF SJF is great, but how do we implement it? –We don’t know a priori how long a job’s burst time is –We have to try to predict the burst time

14 Burst time prediction with exponential average

15 Priority Scheduling Characteristics: –Always schedule the ready process with highest priority –Priority scheduling can be preemptive or non-preemptive SJF is a special case of priority scheduling: –process priority = the inverse of remaining CPU time

16 Priority Scheduling Issues Priorities backwards or forwards? –Unix: -20 is “highest” priority, +20 is “lowest” –Show output of the “top” Unix program on the ITLAB server Where do the priorities come from? –Usually internally derived by the OS –Sometimes externally derived by users/managers Problem: Low-priority processes suffer from starvation. They may have to wait indefinitely. Solution: Process aging (gradually increase priority of old processes)

17 Round-Robin Characteristics: –For time-sharing systems –Similar to FCFS but preemptive –Ready queue is a circular queue –Define a short time quantum, e.g. 20 ms Algorithm: Before starting a process, set timer to generate interrupt after quantum expires If (CPU burst time < quantum) thenprocess gives up CPU voluntarily elsetimer generates interrupt after quantum expires interrupt causes context switch to kernel mode running process is moved to tail of the ready queue switch to next process in queue

18 Round Robin Example [Quantum = 20] Process Arrival Burst Time P 1 053 P 2 117 P 3 268 P 4 324 Notice the long wait times but short response times Issue: What’s the “right” time quantum? Too short: too many context switches (too much overhead) Too long: approaches FCFS performance Rule of thumb: large enough to handle 80% of the CPU bursts P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P3 02037577797117121134154162 Gantt Chart

19 Variations on scheduling: Multilevel queues Use more than one ready queue –e.g. “foreground queue” for interactive programs and “background queue” system maintenance, batch programs Use different scheduling algorithm for each queue –e.g. RR for the foreground queue, FCFS for background queue New problem: how to split time between the queues? –Absolute priority: can cause starvation –Time division: e.g. 80% foreground, 20% background

20 Variations on scheduling: Multilevel feedback queues Don’t fix a process in a queue: let it move. One example: several queues with different priorities –Let I/O bound processes float upward (higher priority) –Move CPU hogs downward (lower priority) –Move processes waiting a too long gradually upward Flexible system but complex implementation.

21 What have we learned? What CPU scheduling is Preemptive vs. non-preemptive scheduling How to evaluate different algorithms Some of the important scheduling algorithms: –FCFS –SJF (both preemptive and non-preemptive versions) Provably optimal, but impossible to implement Can be approximated with burst time prediction –Priority scheduling –Round-robin


Download ppt "Operating Systems Lecture Notes CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002."

Similar presentations


Ads by Google