g
Algorithm
s
Group No. 2 1
Overview
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Implementation in C++
Demonstration
Involvement of Operating System
state
o 2. Switches from running to ready
state
o 3. Switches from waiting to ready
o 4. Terminates
o it terminates
Preemptive
o OS can force (preempt) a process
higher-priority process
Time
o A[ ] Array to store Arrival
Time
o Wt[ ] Array to store Waiting
Time
o Twt Total Waiting Time
o Awt Average Waiting Time
CPU Scheduling Algorithms 9
CONT
Implementation in C++
Operations
:o
Getdata() To get number of
processes and Burst Times from
the user
o Fcfs() First Come, First Served
Algorithm
o Sjf() Shortest Job First
(normal) Algorithm
o SjfP() Shortest Job First
(Preemption) Algorithm
o SjfNp() Shortest Job First (non
preemption) Algorithm
o Priority() Priority Algorithm
CPU Scheduling Algorithms 10
First Come, First
Served
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 P2 P3
0 24 27 30
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
CPU Scheduling Algorithms 11
First Come First CONT
Served
Suppose that the processes arrive in the
order :
P2 , P3 , P1 (P1:24,P2:3,P3:3)
The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
0 3 7 14
0 2 4 5 7 8 12 16
CONT
Shortest Job First
Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Average waiting time = (9 + 1 + 0 +2)/4 = 3
Gantt Chart
P2 P5 P1 P3 P4
0 1 6 16 18 19
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Average
waiting time = {[0+(10-4)]
+4+7}/3 = 5.6
Executable (.exe)
Memory
CP
U Scheduling
Algorithms
Execut
e
CPU Scheduling Algorithms 21
CPU
Schedulin
g
Algorithm
s
Group No.
2
Group No. 2 22