operating systems , ch-05, (CPU Scheduling), 3rd level, College of Computers, Seiyun University. انظمة التشغيل لطلاب المستوى الثالث بكلية الحاسبات بجامعة سيئون المحاضرة 05
Size: 1.78 MB
Language: en
Added: May 13, 2024
Slides: 99 pages
Slide Content
Chapter 5: CPU Scheduling
•CPUschedulingisthecentralinmulti-programming
system.
2
•Maximum CPU utilizationobtainedwith
multiprogramming (prevent CPU from being idle).
•Processes residing in the main memory is selected by
the Scheduler that is:
Concerned with deciding a policy about which process is
to be selected.
Process selection based on a scheduling algorithm.
BasicConcepts
Scheduling can be
•Non-preemptive
Oncea processisallocatedthe
CPU,itdoesnot leave until terminate.
•Preemptive
OScanforce(preempt)a process
fromCPUat anytime.
Say, to allocate CPU to another higher-priority process.
9
BasicConcepts
Non-preemptive and Preemptive
•Preemptiveisharder:Needtomaintainconsistencyof
datasharedbetweenprocesses,andmoreimportantly,
kerneldatastructures(e.g.,I/Oqueues).
10
BasicConcepts
1.First-Come,First-Served(FCFS)Scheduling
SchedulingAlgorithms
Process BurstTime
P
1 24
P
2 3
P
3 3
Suppose that the processes arrive in the order: P
1 , P
2 , P
3
The Gantt Chart for the schedule is:
Note:A process may have many CPU bursts, but in the following
examples we show only one for simplicity.
First-Come, First-Served (FCFS) Scheduling
Process Burst Time
P
1 24
P
2 3
P
3 3
Suppose that the processes arrive in the order: P
1, P
2, P
3
The Gantt Chart for the schedule is:
P
1 P
2 P
3
24 27 300
Waiting Time
??????�??????�??????�
0 24 27
Averagewaitingtime:
(0+24+27)/3=17
TurnaroundTime
??????�??????�??????�
24 27 30
Averageturnaroundtime:
(24+27+30)/3=27
Response Time
??????�??????�??????�
0 24 27
Averageresponsetime:
(0+24+27)/3=17
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P
2, P
3, P
1
The Gantt chart for the schedule is:
P
1P
3P
2
63 300
Waiting Time
??????�??????�??????�
6 0 3
Averagewaitingtime:
(6+0+3)/3=3
Turnaround Time
??????�??????�??????�
30 3 6
Averageturnaroundtime:
(30+3+6)/3=13
Response Time
??????�??????�??????�
6 0 3
Averageresponsetime:
(6+0+3)/3=3
•Much better than previous case
•Convoy effectshort process behind long process
Shortest-Job-First (SJR) Scheduling
Associate with each process the length of its next CPU
burst. Use these lengths to schedule the process with the
shortest time
Two schemes:
nonpreemptive –once CPU given to the process it
cannot be preempted until completes its CPU burst
preemptive –if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF)
SJF is optimal –gives minimum average waiting time for a
given set of processes
Process Arrival Time Burst Time
P
1 0.0 7
P
2 2.0 4
P
3 4.0 1
P
4 5.0 4
SJF (non-preemptive)
Average waiting time = (0 + 6 + 3 + 7)/4 = 4
Example of Non-Preemptive SJF
P
1 P
3 P
2
73 160
P
4
8 12
Example of Preemptive SJF
Process Arrival Time Burst Time
P
1 0.0 7
P
2 2.0 4
P
3 4.0 1
P
4 5.0 4
SJF (preemptive)
Average waiting time = (9 + 1 + 0 +2)/4 = 3
P
1 P
3P
2
42
110
P
4
5 7
P
2 P
1
16
Priority Scheduling
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest
priority (smallest integer highest priority)
Preemptive
nonpreemptive
SJFis a priority scheduling where priority is the predicted
next CPU burst time
Problem Starvation –low priority processes may never execute
Solution Aging –as time progresses increase the priority of the
process
3.PriorityScheduling
SchedulingAlgorithms
(2/25)
Process Burst Time Priority
P
1 10 3
P
2 1 1
P
3 2 4
P
4 1 5
P
5 5 2
PriorityschedulingGanttChart
22
3.PriorityScheduling
SchedulingAlgorithms
(2/25)
Process Burst Time Priority
P
1 10 3
P
2 1 1 Highest priority
P
3 2 4
P
4 1 5
P
5 5 2
PriorityschedulingGanttChart
23
3.PriorityScheduling
SchedulingAlgorithms
Process Burst Time Priority
P
1 10 3
P
2 1 1
P
3 2 4
P
4 1 5
P
5 5 2
PriorityschedulingGanttChart
24
3.PriorityScheduling
SchedulingAlgorithms
(3/25)
25
Process Burst Time Priority
P
1 10 3
P
2 1 1
P
3 2 4
P
4 1 5
P
5 5 2
PriorityschedulingGanttChart
3.PriorityScheduling
SchedulingAlgorithms
(3/25)
26
Process Burst Time Priority
P
1 10 3
P
2 1 1
P
3 2 4
P
4 1 5
P
5 5 2
PriorityschedulingGanttChart
3.PriorityScheduling
SchedulingAlgorithms
(3/25)
27
Process Burst Time Priority
P
1 10 3
P
2 1 1
P
3 2 4
P
4 1 5
P
5 5 2
PriorityschedulingGanttChart
3.PriorityScheduling
SchedulingAlgorithms
28
Process Burst Time Priority
P
1 10 3
P
2 1 1
P
3 2 4
P
4 1 5
P
5 5 2
PriorityschedulingGanttChart
Waiting Time
??????�??????�??????�??????�??????�
6 0 1618 1
AverageWaitingtime=
[6+0+16+18+1]/5= 8.2msec
Turnaround Time
??????�??????�??????�??????�??????�
161 18196
AverageTurnaroundtime=
[16+1+18+19+6]/5= 12msec
Response Time
??????�??????�??????�??????�??????�
6 0 16 18 1
AverageResponsetime=
[6+0+16+18+1]/5=8.2msec
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 nprocesses in the ready queue and the time
quantum is q, then each process gets 1/nof the CPU time in
chunks of at most qtime units at once. No process waits
more than (n-1)q time units.
Performance
qlarge FIFO
q small q must be large with respect to context switch,
otherwise overhead is too high
Example of RR with Time Quantum = 20
Process Burst Time
P
1 53
P
2 17
P
3 68
P
4 24
The Gantt chart is:
Typically, higher average turnaround than SJF, but better response
P
1P
2P
3P
4P
1P
3P
4P
1P
3P
3
02037577797117121134154162
Time Quantum and Context Switch Time
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 dnuoR .
:mutnauq fo gniludehcs )RR( niboR4 ms
32
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
33
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 .
:mutnauq fo gniludehcs )RR( niboR dnuoR4 ms
P
1
0 4 7 10 14 18 22 26 30
P
2
P
3
P
1
P
1
P
1
P
1
P
1
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
34
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 .
:mutnauq fo gniludehcs )RR( niboR dnuoR4 ms
P
1
0 4 7 10 14 18 22 26
30
P
2
P
3
P
1
P
1
P
1
P
1
P
1
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
35
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 .
:mutnauq fo gniludehcs )RR( niboR dnuoR4 ms
0 4 7 10 14 18 22 26 30
P
1
P
2
P
3
P
1
P
1
P
1
P
1
P
1
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
36
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 .
:mutnauq fo gniludehcs )RR( niboR dnuoR4 ms
P
1
P
2
P
3
P
1
0 14 18 22 26 30
P
1
P
1
P
1
P
1
4 7 10
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
37
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 .
:mutnauq fo gniludehcs )RR( niboR dnuoR4 ms
P
1
P
2
P
3
P
1
P
1
0 18 22 26 30
P
1
P
1
P
1
144 7 10
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
38
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 .
:mutnauq fo gniludehcs )RR( niboR dnuoR4 ms
P
1
P
1
0 18 26 30144 7 10 22
P
1 P
2 P
3 P
1 P
1 P
1
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
39
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 .
:mutnauq fo gniludehcs )RR( niboR dnuoR4 ms
P
1 P
2 P
3 P
1 P
1 P
1 P
1 P
1
0 18 3026144 7 10 22
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
40
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 .
:mutnauq fo gniludehcs )RR( niboR dnuoR4 ms
P
1 P
2 P
3 P
1 P
1 P
1 P
1 P
1
0 18 3026144 7 10 22
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
41
Process BurstTime
P
1 24
P
2 3
P
3 3
All the processes arrive at the same time 0 niboR dnuoR .
:mutnauq fo gniludehcs )RR(4 ms
4 7 10
#ofcontextswitches=7
P
1 P
2 P
3 P
1 P
1 P
1 P
1 P
1
0 18 302614 22
4. RoundRobin(RR)Scheduling
SchedulingAlgorithms
(11/25)
Process BurstTime
P
1 24
P
2 3
P
3 3
Alltheprocessesarriveatthesametime0.
RoundRobin(RR)schedulingofquantum:4ms
P
1 P
2 P
3 P
1 P
1 P
1 P
1 P
1
42
0 18 3026144 7 10 22
Waiting Time
??????�??????�??????�
0( +10 −4) 4 7
Averagewaitingtime:
(6+4+7)/3=5.667ms
Turnaround
Time
??????�??????�??????�
30 7 10
AverageTurnaroundtime:
(30 + 7 + 10)/3 = 15.667ms
Response Time
??????�??????�??????�
0 47
AverageResponsetime:
(0+4+7)/3=3.667 ms
CHAPTER 5 -PART 2
SchedulingAlgorithms
(16/25)
5. Multilevel Queue Scheduling
Scheduling must be done between the queues:
Fixed 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.
SchedulingAlgorithms
(20/32)
5. Multilevel Queue Scheduling
Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 60
P
3 2 20
P
4 3 40
Multilevel Queue Fixed priority
non-preemptive
Using multi-processors
or multi-core processor
5. Multilevel QueueScheduling
SchedulingAlgorithms
(21/32)
Process
P
1P
2P
3
P
4
Arrival TimeBurst Time
0 7
160
220
340
�0
�1
�2
�0
�1
�2
5. Multilevel QueueScheduling
SchedulingAlgorithms
(21/32)
3
�1
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 60
P 2 20
P
4 3 40
7ms
�0
�1
�2
�0
�1
�2
7ms
5. Multilevel QueueScheduling
SchedulingAlgorithms
(21/32)
3
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 60
P 2 20
P
4 3 40
7ms
7ms 8ms
�1 �2
�0
�1
�2
�0
�1
�2
5. Multilevel QueueScheduling
SchedulingAlgorithms
(21/32)
Process
P
1P
2P
Arrival TimeBurst Time
0 7
1
2
3
6052
20
40
�1 �2
�0
�1
�2
�0
�1
�2
3
P
4
15ms
7ms 8ms
5. Multilevel QueueScheduling
SchedulingAlgorithms
(21/32)
Process
P
1P
2P
Arrival TimeBurst Time
0 7
1
2
3
6052
20
40
�1 �2
�2
�3
�0
�1
�2
�0
�1
�2
3
P
4
15ms
7ms 8ms 8ms
SchedulingAlgorithms
(21/32)
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 605244
P
3 2 2012
P
4 3 40
�1 �2
�
2
�3
�0
�1
�2
5. Multilevel QueueScheduling
�0
�1
�2
23ms
7ms 8ms 8ms
SchedulingAlgorithms
(21/32)
�0
�1
�2
5. Multilevel QueueScheduling
�0
�1
�2
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 605244
P
3 2 2012
P
4 3 40
23ms
7ms 8ms 8ms 8ms
�1 �2 �3 �4
�2
SchedulingAlgorithms
(21/32)
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 60524436
P
3 2 2012
P
4 3 4032
�1 �2
�2
�3
�0
�1
�2
5. Multilevel Queue Scheduling
�0
�1
�2
31ms
�4
7ms 8ms 8ms 8ms
16ms
SchedulingAlgorithms
(21/32)
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 60524436
P
3 2 2012
P
4 3 4032
�1 �2
�2
�3
�0
�1
�2
5. Multilevel Queue Scheduling
�0
�1
�2
31ms
�4
7ms 8ms 8ms
�2
�3
8ms
16ms12ms
36ms
SchedulingAlgorithms
(25/32)
5. Multilevel Queue Scheduling
Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 60
P
3 2 20
P
4 3 40
Multilevel Queue Fixed priority
non-preemptive
Using single-core
processor
5. Multilevel QueueScheduling
SchedulingAlgorithms
(26/32)
�0
�1
�2
�0
�1
�2
1
ProcessArrival TimeBurst Time
P 0 7
P
2 1 60
P
3 2 20
P
4 3 40
5. Multilevel QueueScheduling
SchedulingAlgorithms
(27/32)
�0
�1
�2
�1
�0
�1
�2
7ms
1
ProcessArrival TimeBurst Time
P 0 7
P
2 1 60
P
3 2 20
P
4 3 40
7ms
5. Multilevel QueueScheduling
SchedulingAlgorithms
(27/32)
�0
�1
�2
�1
�0
�1
�2
7ms
1
ProcessArrival TimeBurst Time
P 0 7
P
2 1 60
P
3 2 20
P
4 3 40
7ms
5. Multilevel QueueScheduling
SchedulingAlgorithms
(27/32)
�0
�1
�2
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 6052
P
3 2 20
P
4 3 40
�
1�2
7ms8ms
15ms
7ms
5. Multilevel QueueScheduling
SchedulingAlgorithms
(27/32)
�0
�1
�2
ProcessArrival TimeBurst Time
P
1 0 7
P
2 1 6052
P
3 2 2012
P
4 3 40
7ms8ms
�
1�2�
3
15ms
7ms23ms
5. Multilevel QueueScheduling
SchedulingAlgorithms
(27/32)
�0
�1
�2
Process
P
1P
2
P
3P
4
Arrival TimeBurst Time
0 7
1
2
3
6052
2012
4032
�
1�2�
3�
4
15ms31ms
7ms23ms
7ms8ms8ms8ms
5. Multilevel QueueScheduling
SchedulingAlgorithms
(27/32)
�0
�1
�2
Process
P
1P
2
P
3P
4
Arrival TimeBurst Time
0 7
1
2
3
605236
2012
4032
7ms23ms
�
1�2�
3�
4
15ms31ms
7ms8ms8ms8ms
�2
16ms
47ms
5. Multilevel QueueScheduling
SchedulingAlgorithms
(27/32)
�0
�1
�2
Process
P
1P
2
P
3P
4 605236
2012
4032
7ms23ms
�
1�2�
3�
4
15ms31ms
7ms8ms8ms8ms
�2
16ms
47ms
�3
12ms
Arrival TimeBurst Time
0 7
1
2
3
59ms
5. Multilevel QueueScheduling
SchedulingAlgorithms
(27/32)
�0
�1
�2
Process
P
1P
2
P
3P
4 605236
2012
4032
7ms23ms
�
1�2�
3�
4
15ms31ms
7ms8ms8ms8ms
�2
16ms
47ms
�3
12ms
Arrival TimeBurst Time
0 7
1
2
3
59ms
SchedulingAlgorithms
(32/32)
Student Example:
Using a Multilevel Queue Scheduling algorithm in Fig.1. Consider the
following processes with the relative CPU bursts.
ProcessArrival TimeBurst Time
P
1 0 20
P
2 1 60
P
3 2 5
P
4 2 40
Multilevel Queue Fixed priority
non-preemptive
Fig.1