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)
Operating System Concepts
CPU Scheduling
2
How is the OS to decide which of several tasks to take off a queue?
Scheduling: deciding which threads are given access to resources from moment to moment.
Operating Systems Lec 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
CPU & I/O Bursts
Process execution is nothing but a sequence of CPU & I/O bursts
Processes alternate between these two states
CPU bursts distribution
CPU Burst & I/O Burst Alternation (Fig.
In general, most programs have short CPU bursts
What happens when the I/O burst occurs?
What is the process state at that time?
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
•
•
•
•
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
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
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
Used in all subsequent versions of windows
Mac OS X uses it
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
Criteria & Objectives For scheduling
•
•
•
•
•
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
Optimization Criteria
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
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)
FCFS: First-Come First-Served
14
Ready queue data structure: a FIFO queue
Assuming we have (at least) a multiprogrammed system
Example
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:
CPU burst times
E.g., time units are milliseconds
Time period spent in the ready queue (assume processes terminate)
Also calculate average waiting time across processes
Assume 0 context switch time
0
P1
20
P2 P3
32 40
P4 P5
56 60
Waiting times?
P1: 0
P2: 20
P3: 32
P4: 40
P5: 56
Average wait time: 148/5 = 29.6
FCFS: First-Come First-Served
16
Advantage: Relatively simple algorithm
Disadvantage: long waiting times
Scheduling Algorithms: First-Come,
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:
0
P1 P2 P3
24 27 30
P1: 24
P2: 27
P3: 30
Average Waiting Time: (0+24+27)/3 = 17
Average Completion Time: (24+27+30)/3 = 27
Operating Systems Lec 3
Scheduling Algorithms: First-Come,
What if their order had been P2, P3, P1?
P1 burst time: 24
P2 burst time: 3
P3 burst time: 3
Operating Systems Lec 3
Scheduling Algorithms: First-Come,
What if their order had been P2, P3, P1?
P1 burst time: 24
P2 burst time: 3
P3 burst time: 3
Waiting Time
0
P2 P3
3 6
P1: 0
P2: 3
P3: 6
Completion Time:
P1
30
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)
Operating Systems Lec 3
SJF
Provably shortest average wait time
BUT: What do we need to actually implement this?
P5 P3
0 4 12
P2
SJF Solution
P4 P1
24 40 60
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)
Priority Scheduling
22
Have to decide on a numbering scheme
0 can be highest or lowest
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
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
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
Round Robin (RR) Scheduling
26
Round Robin Scheme
Each process gets a small unit of CPU time (time quantum)
Usually 10-100 ms
After quantum expires, the process is preempted and added to the end of the ready queue
Suppose N processes in ready queue and time quantum is Q ms:
Each process gets 1/N of the CPU time
In chunks of at most Q ms
What is the maximum wait time for each process?
No process waits more than (n-1)q time units
Performance Depends on Size of Q
Small Q => interleaved
Large Q is like FCFS
Q must be large with respect to context switch time, otherwise overhead is too high (spending most of your time context switching!)
Operating Systems Lec 3
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
complete s
Solution
28 complete s complete s complete s s
0
P1 P2 P3 P4 P5 P1 P2 P3 P4 P1 P2 P4 P1 P4 P1
4 8 12 16 2
0
2
4
28 3
2
3
6
4
0
44 4
8
52 56 6
0
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
Example 2of RR with Time Quantum = 4
29
Process Burst Time
P
1
P
2
P
3
24
3
3
The Gantt chart is:
P
1
0 4
P
2
P
3
P
1
P
1
P
1
P
1
P
1
7 10 14 18 22 26 30
Operating Systems Lec 3
Example of RR with Time Quantum = 4
30
Process Burst Time
P
P
P
1
2
3
24
3
3
P
1
P
2
P
3
P
1
P
1
P
1
P
1
P
1
0 4 7 10 14 18 22 26 30
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
Operating Systems Lec 3
Performance Characteristics of Scheduling
Algorithms
33
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’
Example 3 of RR with Time Quantum = 20
34
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
A process can finish before the time quantum expires, and release the CPU.
Average Waiting Time: (72+20+85+88)/4 = 66.25
Average Completion Time: (125+28+153+112)/4 = 104.5
Operating Systems Lec 3
Comparing FCFS and RR
35
Assuming zero-cost context switching time, is RR always better than FCFS?
Assume 10 jobs, all start at the same time, and each require
100 seconds of CPU time
Job #
RR scheduler quantum of 1 second
Completion Times (CT)
Both FCFS and RR finish at the same time
But average response time is much worse under RR!
Bad when all jobs are same length
1
2
…
9
10
FCFS CT
100
200
…
900
1000
RR CT
1000
Also: cache state must be shared between all jobs with RR but can be devoted to each job with FIFO
Total time for RR longer even for zero-cost context switch!
991
992
…
999
Operating Systems Lec 3