CPU Scheduling Lecture No.5 - University of the Agriculture

advertisement
Introduction to Operating System
Created by : Zahid Javed
CPU Scheduling
Fifth Lecture
Today Agenda
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
CPU Scheduling (Core Definitions)
Scheduling Objectives
CPU I/O Burst Cycle
Working of CPU Scheduler
Preemptive Scheduling
Non-Preemptive Scheduling
Dispatcher
Scheduling Criteria
Scheduling Algorithm Optimization Criteria
First-Come, First-Served (FCFS) Scheduling
FCFS Scheduling (non-preemptive)
Shortest-Job-First (SJF) Scheduling
Determining Length of Next CPU Burst
Example of SJF Non-preemptive SJF Example
Priority Scheduling
Non-Preemptive Priority Scheduling
Prove It (SJF is Optimal in Non- Preemptive)
Round Robin (RR)
Multilevel Queue Scheduling
Multilevel Feedback Queue
CPU Scheduling
(Core Definitions)
 General rule — keep the CPU busy; an idle CPU is a
wasted CPU
 Major source of CPU idleness: I/O (or waiting for it)
 CPU scheduling (short-term scheduling) is the act of
selecting the next process for the CPU to “service” once
the current process leaves the CPU idle
 Many algorithms for making this selection
 Implementation-wise, CPU scheduling manipulates the
operating system’s various PCB queues
 The dispatcher is the software that performs the dirty
work of passing the CPU to the next selected process
Scheduling Objectives
 Maximize CPU utilization
 Maximize utilization of other resource(Disk, printer)
 Maximize throughout = number of jobs completed per unit
time.
 Minimize waiting time = total time a job spends waiting in
the various queues for resource to become available.
 Minimize turnaround time = Waiting + Computation + I /O
time
 Minimize response time (timesharing) = time from entry of a
command until first output starts to appear.
 Fairness: All comparable jobs should be treated equally
CPU I/O Burst Cycle
Almost all processes alternate
between two states in a
continuing cycle, as shown in
Figure below :
 A CPU burst of performing
calculations, and
 An I/O burst, waiting for
data transfer in or out of
the system.
Working of 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.
2.
3.
4.
Switches from running to waiting state (Ex. I/O Request)
Switches from running to ready state (Ex. Interrupts Occur)
Switches from waiting to ready (Ex. Completion of I/O )
Terminates
• Scheduling under 1 and 4 is non-preemptive
• All other scheduling is preemptive
Preemptive Scheduling
Preemptive: Preemptive algorithms are driven by the
notion of prioritized computation. The process with the
highest priority should always be the one currently using
the processor. If a process is currently using the processor
and a new process with a higher priority enters, the ready
list, the process on the processor should be removed and
returned to the ready list until it is once again the highestpriority process in the system
Non-Preemptive Scheduling
Non-Preemptive: Non-preemptive algorithms are designed
so that once a process enters the running state(is allowed
a process), it is not removed from the processor until it has
completed its service time. context_switch() is called only
when the process terminates or blocks.
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
• Dispatch latency – time it takes for the dispatcher
to stop one process and start another running
Scheduling Criteria
Scheduling Criteria
Scheduling Algorithm Optimization
Criteria
 Max CPU utilization
 Max throughput
 Min turnaround time
 Min waiting time
 Min response time
Scheduling Algorithms
1.
2.
3.
4.
5.
6.
First-Come, First-Served (FCFS) Scheduling
Shortest-Job-First (SJF) Scheduling
Priority Scheduling
Round robin Scheduling
Multilevel queue Scheduling
Multilevel feedback-queue Scheduling
First-Come, First-Served (FCFS)
Scheduling
Process
Burst Time
P1
24
P2
3
P3
3
• Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1
0
P2
24
P3
27
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
30
FCFS Scheduling (non-preemptive)
Suppose that the processes arrive in the order
P2 , P3 , P1
• The Gantt chart for the schedule is:
P2
0
•
•
•
•
P3
3
P1
6
30
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect short process behind long process
Convoy Effect
Convoy Effect
FCFS Scheduling (non-preemptive)
Process
Name
Arrival time
Burst time
Finish time
Waiting
time
Process 1
0
24
30
6
Process 2
0
3
3
0
Process 3
0
3
6
3
• Average waiting time: (6 + 0 + 3)/3 = 3
• It is not good scheduling algorithm for system where
response time is important (i.e. real time or time
sharing system)
Shortest-Job-First (SJF) Scheduling
• Pick the job that will take the least amount of time to
complete
• Associate with each process the length of its next
CPU burst. Use these lengths to schedule the
process with the shortest time
• SJF is optimal – gives minimum average waiting time
for a given set of processes
– The difficulty is knowing the length of the next
CPU request
• Two schemes of this algorithm
 Non-preemptive
 preemptive
Determining Length of Next CPU Burst
• Can only estimate the length
• Can be done by using the length of previous CPU
bursts, using exponential averaging
1. t n  actual length of n th CPU burst
2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define :
 n 1   tn  1    n
Example of SJF
P4
0
Process
Burst time
P1
6
P2
8
P3
7
P4
3
P1
3
P3
9
P2
16
24
Non-preemptive SJF Example
Process
Arrival time
Burst time
P1
0
7
P2
2
4
P3
4
2
P4
5
4
Gantt Chart
P1
P3
P2
P4
0
7
9
13
Waiting time for the processes in this example
Process
Name
Arrival time
Burst time
Finish time
17
Waiting
time
Process 1
0
7
7
0
Process 2
2
4
13
7
Process 3
4
2
9
3
Process 4
5
4
17
8
Average waiting time: (0 + 7 + 3 + 8) / 4 = 4.5
Note:- Waiting time= Finish Time-(Arrival time + Burst time)
Shortest-remainingtime-first scheduling
(SRTF)
• Preemptive or non-preemptive
• Set Scheduling algorithm
– Every 1 arrival time
– 100 Microsecond
– New job come
Preemptive SJF Example
Process
Arrival time
Burst time
P1
0
7
P2
2
4
P3
4
1
P4
5
4
Gantt Chart
P1
P2
P3
P2
P4
P1
0
2
4
5
7
11
16
Waiting time for the processes in this example
Process
Name
Arrival time
Burst time
Finish time
Waiting
time
Process 1
0
7
16
9
Process 2
2
4
7
1
Process 3
4
2
5
0
Process 4
5
4
11
2
Average waiting time: (9 + 1 + 0 + 2) / 4 = 3
Priority Scheduling
• A priority number (integer) is associated with
each process
• The CPU is allocated to the process with the
highest priority (smallest integer mean highest
priority)
• Preemptive
• Non-preemptive
• SJF is a priority scheduling where priority is the
predicted next CPU burst time
Priority Scheduling
Problem :Starvation – low priority processes may never
execute
Solution :Aging – as time progresses increase the priority of
the process
Non-Preemptive Priority Scheduling
Process
Burst time
Priority
P1
10
3
P2
1
1
P3
2
4
P4
1
5
P5
5
2
Gantt Chart
P2
0
P5
1
P1
6
P3
16
P4
18
Average waiting time: (1 + 6 + 16 + 18 + 19) / 5 = 8.2
19
Preemptive Priority Scheduling
Process
Arrival time
Burst time
Priority
P1
0
10
3
P2
2
1
1
P3
4
2
2
P4
6
1
4
P5
8
5
5
Gantt Chart
P1
0
P2
2
P1
3
P3
4
P1
6
P4
13
14
P5
19
F=(0+5+8) / 3= 4.33
S=(0+2+5) / 3=2.33
Prove It
(SJF is Optimal in Non- Preemptive)
Logical Argument:
Decrease in the wait times for short processes is
much more than increase in the wait times for long
processes
Gantt Chart
P1
P2
P3
5
3
2
Change Order
P3
P2
P1
2
3
5
Round Robin (RR)
• Each process gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds. After this
time has elapsed, the process is preempted and
added to the end of the ready queue.
• If there are n processes in the ready queue and the
time quantum is q, then each process gets 1/n of the
CPU time in chunks of at most q time units at once.
No process waits more than (n-1)*(q+tcs)time units.
• If n=6 and q=5 then (6-1)*5= 25
• Performance
– q large  FIFO or FCFS
– q small  q must be large with respect to context
switch, otherwise overhead is too high
Example of RR with Time Quantum = 4
Process
Burst time
P1
24
P2
3
P3
3
The Gantt chart is:
P1
0
P2
4
P3
7
P1
10
P1
14
P1
18
P1
22
P1
26
30
• Typically, higher average turnaround than SJF,
but better response and it is preemptive
scheduling
Example of RR with Time Quantum = 20
Process
Burst time
F
S
T
P1
53
33
13
P2
17
P3
68
48
28
P4
24
4
F
8
The Gantt chart is:
P1
0
P2
20
P3
37
P4
57
P1
77
P3
97
117
P4
121
P1
134
P3
154
P3
162
Example of RR with Time Quantum = 20
Process
Turnaround
time
Waiting time
P1
134
134 – 53 = 81
P2
37
37 – 17 = 20
P3
162
162 – 68 = 94
P4
121
121 – 24 = 97
 Average waiting time: - 73
 Average waiting time for SJF: - 38
 Typically, higher average turnaround than SJF,
but better response
Time Quantum and Context
Switch Time
Multilevel Queue Scheduling
• Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
• Each queue has its own scheduling algorithm
– foreground – RR
– background – FCFS
Multilevel Queue Scheduling
Multilevel Queue Scheduling
• Scheduling must be done between the
queues:
– Fixed (Absolute) priority scheduling; (i.e.,
serve all from foreground then from
background). Possibility of starvation.
– Time slice – each queue gets a certain
amount of CPU time which it can schedule
amongst its processes; i.e., 80% to
foreground in RR
– 20% to background in FCFS
Multilevel Feedback Queue
• A process can move between the various queues;
aging can be implemented this way
• Multilevel-feedback-queue scheduler defined by the
following parameters:
– number of queues
– scheduling algorithms for each queue
– method used to determine when to upgrade a
process
– method used to determine when to demote a
process
– method used to determine which queue a
process will enter when that process needs
service
Example of Multilevel Feedback
Queue
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS
• Scheduling
– A new job enters queue Q0 which is served FCFS.
When it gains CPU, job receives 8 milliseconds. If it
does not finish in 8 milliseconds, job is moved to
queue Q1.
– At Q1 job is again served FCFS and receives 16
additional milliseconds. If it still does not
complete, it is preempted and moved to queue Q2.
Multilevel Feedback Queue
Download