Download presentation
Presentation is loading. Please wait.
Published byRaymond Shelton Modified over 8 years ago
1
Lecturer 5: Process Scheduling Process Scheduling Criteria & Objectives Types of Scheduling Long term Medium term Short term CPU Scheduling Algorithms First-Come, First-Served (FCFS) Shortest Job First (SJF) Priority (PRI) Round-Robin (RR) 1
2
CPU Scheduling 2 Scheduling: deciding which process are given access to resources from moment to moment.
3
Chapter 5: CPU Scheduling 3 Always want to have CPU (or CPU’s) working Usually many processes in ready queue Ready to run on CPU Focus on a single CPU here Need strategies for Allocating CPU time Selecting next process to run Deciding what happens after a process completes a system call, or completes I/O Short-term scheduling Must not take much CPU time to do the scheduling
4
CPU & I/O Bursts Process execution is nothing but a sequence of CPU & I/O bursts Processes alternate between these two states 4
5
CPU Burst & I/O Burst Alternation (Fig. 5.1) 5 What happens when the I/O burst occurs? What is the process state at that time? In general, most programs have short CPU bursts
6
Types of Scheduling Long-term scheduling the decision to add to pool of processes to be executed Mid-term scheduling the decision to add to the number of processes that are partially or fully in memory Short-term scheduling decision as to which available process will be executed I/O scheduling decision as to which process’s pending request shall be handled by an available I/O device 6
7
Short term CPU Scheduler Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them CPU scheduling decisions may take place when a process: 1.Switches from running to waiting state 2.Switches from running to ready state 3.Switches from waiting to ready 4.Terminates Scheduling under 1 and 4 is non-preemptive All other scheduling is preemptive 7
8
Preemptive & Non-preemptive Scheduling SO what is Preemptive & Non-preemptive scheduling? Non-preemptive Once CPU is allocated to a process, it holds it till it Terminates (exits) Blocks itself to wait for I/O Requests some OS service 8
9
Preemptive & Non-preemptive Scheduling Preemptive Currently running process may be interrupted and moved to the ready state by the OS Windows 95 introduced preemptive scheduling 9
10
Dispatcher Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context switching to user mode jumping to the proper location in the user program to restart that program (IP) Dispatch latency – time it takes for the dispatcher to stop one process and start another running 10
11
For scheduling Criteria & Objectives CPU utilization – keep the CPU as busy as possible Throughput – # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process (total time spent on the system) Waiting time – amount of time a process has been waiting in the ready queue Response time – amount of time it takes from when a request was submitted until the first response is produced, not output 11
12
Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time 12
13
CPU Scheduling Algorithms 13 First-Come, First-Served (FCFS) Complete the jobs in order of arrival Shortest Job First (SJF) Complete the job with shortest next CPU burst Priority (PRI) Processes have a priority Allocate CPU to process with highest priority Round-Robin (RR) Each process gets a small unit of time on CPU (time quantum or time slice)
14
FCFS: First-Come First-Served 14 Ready queue data structure: a FIFO queue Assuming we have (at least) a multiprogrammed system Example 1 Draw Gantt chart Compute the average waiting time for processes with the following next CPU burst times and ready queue order: P1: 20 P2: 12 P3: 8 P4: 16 P5: 4 Waiting time: Time period spent in the ready queue (assume processes terminate) Also calculate average waiting time across processes Assume 0 context switch time CPU burst times E.g., time units are milliseconds
15
Solution: Gantt Chart Method 15 Waiting times? P1: 0 P2: 20 P3: 32 P4: 40 P5: 56 Average wait time: 148/5 = 29.6 FCFS: First-Come First-Served P1 20 P2 32 P3P4P5 4056600
16
FCFS: First-Come First-Served 16 Advantage: Relatively simple algorithm Disadvantage: long waiting times
17
Scheduling Algorithms: First-Come, First-Served (FCFS) 17 Example 2: Three processes arrive in order P1, P2, P3. P1 burst time: 24 P2 burst time: 3 P3 burst time: 3 Waiting Time P1: 0 P2: 24 P3: 27 Completion Time: P1: 24 P2: 27 P3: 30 Average Waiting Time: (0+24+27)/3 = 17 Average Completion Time: (24+27+30)/3 = 27 P1P2P3 024 2730
18
Scheduling Algorithms: First-Come, First-Served (FCFS) 18 What if their order had been P2, P3, P1? P1 burst time: 24 P2 burst time: 3 P3 burst time: 3
19
Scheduling Algorithms: First-Come, First-Served (FCFS) 19 What if their order had been P2, P3, P1? P1 burst time: 24 P2 burst time: 3 P3 burst time: 3 Waiting Time P1: 0 P2: 3 P3: 6 Completion Time: P1: 3 P2: 6 P3: 30 Average Waiting Time: (0+3+6)/3 = 3 (compared to 17) Average Completion Time: (3+6+30)/3 = 13 (compared to 27) P1P2P3 03 630
20
SJF Provably shortest average wait time BUT: What do we need to actually implement this?
21
SJF Solution Waiting times (how long did process wait before being scheduled on the CPU?): P1: 40 P2: 12 P3: 4 P4: 24 P5: 0 Average wait time: 16 (Recall: FCFS scheduling had average wait time of 29.6) P1 4 P2 12 P3P4P5 2440600 21
22
Priority Scheduling 22 Have to decide on a numbering scheme 0 can be highest or lowest
23
Starvation Problem 23 Priority scheduling algorithms can suffer from starvation (indefinite waiting for CPU access) In a heavily loaded system, a steady stream of higher-priority processes can result in a low priority process never receiving CPU time I.e., it can starve for CPU time One solution: aging Gradually increasing the priority of a process that waits for a long time
24
Which CPU Scheduling Algorithms Can be Preemptive? 24 FCFS (First-come, First-Served) Non-preemptive SJF (Shortest Job First) Can be either Choice when a new (shorter) job arrives Can preempt current job or not Priority Can be either Choice when a processes priority changes or when a higher priority process arrives
25
RR (Round Robin) Scheduling 25 Used in time-sharing or multi-tasking systems typical kind of scheduling algorithm in a contemporary general purpose operating system Method Give each process a unit of time (time slice, quantum) of execution on CPU Then move to next process in ready queue Continue until all processes completed Necessarily preemptive Requires use of timer interrupt Time quantum typically between 10 and 100 milliseconds Linux default appears to be 100ms
26
RR (Round Robin) Scheduling: Example 26 CPU job burst times & order in queue P1: 20 P2: 12 P3: 8 P4: 16 P5: 4 Draw Gantt chart, and compute average wait time Time quantum of 4 Like our previous examples, assume 0 context switch time
27
Solution 27 Waiting times: P1: 60 - 20 = 40 P2: 44 - 12 = 32 P3: 32 - 8 = 24 P4: 56 - 16 = 40 P5: 20 - 4 = 16 Average wait time: 30.4 P1P2 4 P3P4P5P1 812162020 2424 P2P3 28 P4P1P2P4 3232 3636 4040 444848 P1P4 52 P1 566060 complete s 0
28
Example 2of RR with Time Quantum = 4 28 ProcessBurst Time P 1 24 P 2 3 P 3 3 The Gantt chart is: P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P1 0 47 101418222630
29
Example of RR with Time Quantum = 4 29 Process Burst Time P 1 24 P 2 3 P 3 3 Waiting Time: P1: (10-4) = 6 P2: (4-0) = 4 P3: (7-0) = 7 Completion Time: P1: 30 P2: 7 P3: 10 Average Waiting Time: (6 + 4 + 7)/3= 5.67 Average Completion Time: (30+7+10)/3=15.67 P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P1 0 47 101418222630
30
Example 3 of RR with Time Quantum = 20 30 Waiting Time: P1: (68-20)+(112-88) = 72 P2: (20-0) = 20 P3: (28-0)+(88-48)+(125-108) = 85 P4: (48-0)+(108-68) = 88 Completion Time: P1: 125 P2: 28 P3: 153 P4: 112 Average Waiting Time: (72+20+85+88)/4 = 66.25 Average Completion Time: (125+28+153+112)/4 = 104.5 A process can finish before the time quantum expires, and release the CPU.
31
Performance Characteristics of Scheduling Algorithms 31 Different scheduling algorithms will have different performance characteristics RR (Round Robin) Average waiting time often high Good average response time Important for interactive or timesharing systems SJF Best average waiting time Some overhead with respect to estimates of CPU burst length & ordering ready ‘queue’
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.