CPU Scheduling
CPU Scheduling
CPU SCHEDULING
Course Code L T P S Credits
23CS5103 3 0 2 0 4
Process and its State
Process:-
➢In order to accomplish its task, process needs the computer resources.
➢There may exist more than one process in the system which may require
the same resource at the same time. Therefore, the operating system has
to manage all the processes and the resources in a convenient and efficient
way.
Process and its State
Process States:-
✓From the Running state, a process can make the transition to the block or
wait state depending upon the scheduling algorithm or the intrinsic
behavior of the process.
✓When a process waits for a certain resource to be assigned or for the input
from the user then the OS move this process to the block or wait state and
assigns the CPU to the other processes.
Process and its State
5. Completion or termination
✓When a process finishes its execution, it comes in the termination state.
✓All the context of the process (Process Control Block) will also be
deleted the process will be terminated by the Operating system.
Operations of the process
1. Creation:- Once the process is created, it will be ready and come into the ready queue (main
memory) and will be ready for the execution.
2. Scheduling:- Out of the many processes present in the ready queue, the Operating system
chooses one process and start executing it. Selecting the process which is to be executed next, is
known as scheduling.
3. Execution:- Once the process is scheduled for the execution, the processor starts executing it.
Process may come to the blocked or wait state during the execution then in that case the
processor starts executing the other processes.
4. Deletion/killing:- Once the purpose of the process gets over then the OS will kill the process.
The Context of the process (PCB) will be deleted and the process gets terminated by the
Operating system.
CPU Scheduling
➢CPU scheduling is the process of deciding which process will own the CPU
to use while another process is suspended.
➢The main function of the CPU scheduling is to ensure that whenever the
CPU remains idle, the OS has at least selected one of the processes
available in the ready-to-use line.
Objectives of Process Scheduling Algorithm:
➢Utilization of CPU at maximum level. Keep CPU as busy as possible.
➢Throughput should be Maximum. i.e. Number of processes that complete their execution per
time unit should be maximized.
➢Minimum turnaround time, i.e. time taken by a process to finish execution should be the least.
➢There should be a minimum waiting time and the process should not starve in the ready queue.
➢Minimum response time. It means that the time when a process produces the first response
should be as less as possible.
Terminologies of CPU Scheduling algorithm
➢Arrival Time: Time at which the process arrives in the ready queue.
➢Turn Around Time: Time Difference between completion time and arrival
time. Turn Around Time = Completion Time – Arrival Time
➢ Waiting Time(W.T): Time Difference between turn around time and burst
time. Waiting Time = Turn Around Time – Burst Time
Example to understand the term
➢Arrival Time: 11 am (entered in the bank to deposit cash).
➢Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time (12 pm-11am) or we can
say 60 minute.
➢ Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time (60 minute-15 minute=45 minute)
➢Response Time: (The time at which a process gets CPU first time-Arrival Time)
Types of CPU Scheduling Algorithm
➢First come first serve scheduling algorithm states that the process that
requests the CPU first is allocated the CPU first and is implemented by
using FIFO queue.
1. First Come First Serve:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Arrival Time
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Non Pre-Emptive
1 P1 0 2
2 P2 1 2
3 P3 5 3
4 P4 6 4
1. First Come First Serve:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Arrival Time
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Non Pre-Emptive
1 P1 0 2 2
2 P2 1 2 4
3 P3 5 3 8
4 P4 6 4 12
1. First Come First Serve:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Arrival Time
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Non Pre-Emptive
1 P1 0 2 2 2 (2-0)
2 P2 1 2 4 3 (4-1)
3 P3 5 3 8 3 (8-5)
4 P4 6 4 12 6 (12-6)
Average TAT=2+3+3+6=14/4=3.5
1. First Come First Serve:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Arrival Time
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Non Pre-Emptive
1 P1 0 2 2 2 (2-0) 0 (2-2) 0 (0-0)
2 P2 1 2 4 3 (4-1) 1 (3-2) 1 (2-1)
3 P3 5 3 8 3 (8-5) 0 (3-3) 0 (5-5)
4 P4 6 4 12 6 (12-6) 2 (6-4) 2 (8-6)
Average TAT=2+3+3+6=14/4=3.5
Characteristics of FCFS:
➢This algorithm is not much efficient in performance, and the wait time is
quite high.
Advantages of FCFS:
➢Easy to implement
➢The average waiting time is much higher than the other algorithms.
➢FCFS is very simple and easy to implement and hence not much efficient.
2. Shortest Job First (SJF):
➢Shortest job first (SJF) is a scheduling process that selects the waiting
process with the smallest execution time to execute next.
➢Significantly reduces the average waiting time for other processes waiting
to be executed.
2. Shortest Job First (SJF):
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Burst Time
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Non Pre-Emptive
1 P1 1 3
2 P2 2 4
3 P3 1 2
4 P4 4 4
2. Shortest Job First (SJF):
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Burst Time
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Non Pre-Emptive
1 P1 1 3
2 P2 2 4
3 P3 1 2
4 P4 4 4
2. Shortest Job First (SJF):
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Burst Time
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Non Pre-Emptive
1 P1 1 3 6
2 P2 2 4 10
3 P3 1 2 3
4 P4 4 4 14
2. Shortest Job First (SJF):
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Burst Time
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Non Pre-Emptive
1 P1 1 3 6 5
2 P2 2 4 10 8
3 P3 1 2 3 2
4 P4 4 4 14 10
Average TAT=5+8+2+10=25/4=6.25
2. Shortest Job First (SJF):
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Burst Time
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Non Pre-Emptive
1 P1 1 3 6 5 2 2
2 P2 2 4 10 8 4 4
3 P3 1 2 3 2 0 0
4 P4 4 4 14 10 6 6
Average TAT=5+8+2+10=25/4=6.25
Characteristics of SJF:
31
Disadvantages of SJF:
32
3. Priority Scheduling:
33
3. Priority Scheduling:
• In the case of any conflict, that is, where there are more
than one processor with equal value, then the most
important CPU planning algorithm works on the basis of the
FCFS (First Come First Serve) algorithm.
34
3. Priority Scheduling:
Example:
Priority Process Arrival Burst Completion TAT WT RT Criteria - Priority
no No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
10 P1 0 5 Priority:-Higher the
number, higher the priority
20 P2 1 4
30 P3 2 2
40 P4 4 1
3. Priority Scheduling:
Example:
Priority Process Arrival Burst Completion TAT WT RT Criteria - Priority
no No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
10 P1 0 5 12 Priority:-Higher the
number, higher the priority
20 P2 1 4 8
30 P3 2 2 4
40 P4 4 1 5
3. Priority Scheduling:
Example:
Priority Process Arrival Burst Completion TAT WT RT Criteria - Priority
no No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
10 P1 0 5 12 12 7 (12-5) Priority:-Higher the
number, higher the priority
20 P2 1 4 8 7 3 (7-4)
30 P3 2 2 4 2 0 (2-2)
40 P4 4 1 5 1 0 (1-1)
3. Priority Scheduling:
Example:
Priority Process Arrival Burst Completion TAT WT RT Criteria - Priority
no No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
10 P1 0 5 12 12 7 (12-5) Priority:-Higher the
number, higher the priority
20 P2 1 4 8 7 3 (7-4)
30 P3 2 2 4 2 0 (2-2)
40 P4 4 1 5 1 0 (1-1)
Average TAT=22/4=5.5
Characteristics of Priority Scheduling:
• When the higher priority work arrives while a task with less
priority is executed, the higher priority work takes the place of
the less priority one and the latter is suspended until the
execution is complete.
• Less complex
40
Disadvantages of Priority Scheduling:
41
4. Round robin:
42
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 Given Time Quantum - 2
2 P2 1 4
3 P3 2 2
4 P4 4 1
Ready Queue
Running
(RAM)
Ready
Running Queue
(CPU)
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 Given Time Quantum - 2
2 P2 1 4
3 P3 2 2
4 P4 4 1
Ready Queue P1
Running
(RAM) 0
Ready
Running Queue
(CPU) P1
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 Given Time Quantum - 2
2 P2 1 4
3 P3 2 2
4 P4 4 1
Ready Queue P1
Running
(RAM) 0
Ready
Running Queue
P1
(CPU) P1
0 2
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 Given Time Quantum - 2
2 P2 1 4
3 P3 2 2
4 P4 4 1
Ready Queue P1
Running
(RAM) 0
Ready
Running Queue
P1
(CPU) P1
0 2
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 Given Time Quantum - 2
2 P2 1 4
3 P3 2 2
4 P4 4 1
Ready Queue P1 P2 P3
Running
0
Ready
Running Queue
P1
0 2 P1
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 Given Time Quantum - 2
2 P2 1 4 2
3 P3 2 2
4 P4 4 1
P1
Ready Queue P1 P2 P3 P1
Running
0
Ready
Running Queue
P1 P2
0 2 4 P1
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 Given Time Quantum - 2
2 P2 1 4 2
3 P3 2 2
4 P4 4 1
Ready Queue P1 P2 P3 P1 P4 P2
Running
0
Ready
Running Queue
P1 P2
0 2 4
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 Given Time Quantum - 2
2 P2 1 4 2
3 P3 2 2 0
4 P4 4 1
Ready Queue P1 P2 P3 P1 P4 P2
Running
0
Ready
Running Queue
P1 P2 P3
0 2 4 6
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 1 Given Time Quantum - 2
2 P2 1 4 2
3 P3 2 2 0
4 P4 4 1
Ready Queue P1 P2 P3 P1 P4 P2
Running
0
Ready
Running Queue 2
P1 P2 P3 P1
0 2 4 6 8 P1
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 1 Given Time Quantum - 2
2 P2 1 4 2
3 P3 2 2 0
4 P4 4 1
Ready Queue P1 P2 P3 P1 P4 P2 P1
Running
0
Ready
Running Queue
P1 P2 P3 P1
0 2 4 6 8
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 1 Given Time Quantum - 2
2 P2 1 4 2
3 P3 2 2 0
4 P4 4 1 0
Ready Queue P1 P2 P3 P1 P4 P2 P1
Running
0
Ready
Running Queue
P1 P2 P3 P1 P4
0 2 4 6 8 9
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 1 Given Time Quantum - 2
2 P2 1 4 2
3 P3 2 2 0
4 P4 4 1 0
Ready Queue P1 P2 P3 P1 P4 P2 P1
Running
0
Ready
Running Queue
P1 P2 P3 P1 P4
0 2 4 6 8 9
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 1 Given Time Quantum - 2
2 P2 1 4 2 0
3 P3 2 2 0
4 P4 4 1 0
Ready Queue P1 P2 P3 P1 P4 P2 P1
Running
0
Ready
Running Queue
P1 P2 P3 P1 P4 P2
0 2 4 6 8 9 11
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 1 Given Time Quantum - 2
2 P2 1 4 2 0
3 P3 2 2 0
4 P4 4 1 0
Ready Queue P1 P2 P3 P1 P4 P2 P1
Running
0
Ready
Running Queue
P1 P2 P3 P1 P4 P2 P1
0 2 4 6 8 9 11 12
4. Round robin:
Example:
S.no Process Arrival Burst Completion TAT WT RT Criteria - Time Quantum
No Time(AT) Time Time (CT) (CT-AT) (TAT-BT) Mode- Pre-Emptive
1 P1 0 5 3 1 12 12 7 0 (0-0) Given Time Quantum - 2
2 P2 1 4 2 11 10 6 1 (2-1)
3 P3 2 2 0 6 4 2 2 (4-2)
4 P4 4 1 0 9 5 4 4 (8-4)
Ready Queue P1 P2 P3 P1 P4 P2 P1
Running
0
Ready
Running Queue
P1 P2 P3 P1 P4 P2 P1
0 2 4 6 8 9 11 12
Characteristics of Round robin:
60
5. Longest Job First(LJF):
61
Characteristics of LJF:
• If two processes have the same burst time then the tie is broken
using FCFS i.e. the process that arrived first is processed first.
62
Advantages of LJF:
63
Disadvantages of LJF:
64
6. Shortest Remaining Time First:
65
Characteristics of Shortest remaining time first:
66
Advantages of SRTF:
67
Disadvantages of SRTF:
• Like the shortest job first, it also has the potential for process
starvation.
68
7. Longest Remaining Time First:
69
Characteristics of longest remaining time first:
• If two processes have the same burst time then the tie is broken
using FCFS i.e. the process that arrived first is processed first.
70
Advantages of LRTF:
71
Disadvantages of LRTF:
72
8. Highest Response Ratio Next:
73
Characteristics of Highest Response Ratio Next:
74
Characteristics of Highest Response Ratio Next:
75
Advantages of HRRN:
76
Disadvantages of HRRN:
77