0% found this document useful (0 votes)
19 views

Priority Scheduling Algorithm

SCHEDULING ALGORITHM IN OS

Uploaded by

benazirrose2704
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Priority Scheduling Algorithm

SCHEDULING ALGORITHM IN OS

Uploaded by

benazirrose2704
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Priority Scheduling Algorithm

Introduction
Priority Scheduling is a scheduling algorithm used in operating systems to manage processes
based on their priority. The CPU is allocated to the process with the highest priority.

Types of Priority Scheduling :


1. Preemptive Priority Scheduling : The CPU is preempted from the current running
process if a new process with a higher priority arrives.
2. Non-preemptive Priority Scheduling : The current process runs to completion even
if a higher priority process arrives.

Priority Scheduling Criteria :


✓ Processes are scheduled based on priority.
✓ Lower numerical values for priority indicate higher importance.
✓ If two processes have the same priority, the scheduling can depend on the arrival
time.

Key Terms :
1. Process: A process is a program in execution, including the program's code, current
activity, and data. Processes can have various states, such as ready, running, or
waiting, depending on their status in the execution lifecycle.
2. Priority: The importance level assigned to a process, which determines its scheduling
order.
3. Ready Queue: A queue that holds all processes in memory that are ready to execute
and waiting for CPU time.
4. Arrival Time (AT): The time at which a process arrives in the ready queue.
5. Burst Time (BT): The time required by a process for execution on the CPU.
6. Completion Time (CT): The time at which a process completes its execution.
7. Turnaround Time (TAT): The total time taken from the arrival to the completion of
the process. Calculated as:
TAT = CT – AT.
8. Waiting Time (WT): The total time a process spends in the ready queue waiting for
execution. Calculated as:
WT=TAT − BT.
9. Response Time (RT): Time from arrival to first CPU allocation. Calculated as :
RT = (Time at which CPU is allocated) - AT
10. Gantt Chart : The Gantt Chart helps visualize the order in which processes are
executed.

Algorithm Steps :
➢ Step 1. Initialize the Process List:
Collect all processes with their AT, BT, and Priority.
➢ Step 2. Sort Processes by Priority:
Order processes by priority (highest to lowest). If priorities are equal, use AT as a
tiebreaker.
➢ Step 3. Execute Processes:
Start executing the process with the highest priority.
If preemptive, check periodically if a new process with a higher priority has
arrived. If yes, preempt the current process.
If non-preemptive, let the process finish execution.
➢ Step 4. Calculate Metrics:
Upon completion, calculate CT, TAT, WT, and RT for each process.
➢ Step 5. Repeat the above steps until All Processes Are Executed.

Example :
Criteria : Priority
Mode : Preemptive
Process AT BT Priority
P1 0 4 1
P2 1 3 2
P3 2 2 3
P4 3 1 4

Step-by-Step Execution :
➢ At time 0 :
Ready Queue:
P1
0
P1 arrives (AT 0, Prio 1). Since it's the only process, P1 starts execution.
Gantt Chart :
P1
0

➢ At time 1:
RQ :
P1 P2
0 1
P2 arrives (AT 1, Prio 2). P1 continues since P2 has a lower priority.
Gantt Chart :
P1
0

➢ At time 2:
RQ :
P1 P2 P3
0 1 2
P3 arrives (AT 2, Prio 3). P1 continues since P3 has a lower priority.
Gantt Chart :
P1
0
➢ At time 3:
RQ :
P1 P2 P3 P4
0 1 2 3
P4 arrives (AT 3, Prio 4). P1 continues since P4 has a lower priority.
Gantt Chart :
P1
0
➢ At Time 4 :
P1 completes at time 4. Next, P2 starts executing since it has the highest remaining priority.
Gantt Chart :
P1 P2
0 4
➢ At Time 7 :
P2 completes at time 7. P3 starts executing next.
Gantt Chart :
P1 P2 P3
0 4 7
➢ At Time 9 :
P3 completes at time 9. Finally, P4 executes.
Gantt Chart :
P1 P2 P3 P4
0 4 7 9
➢ At Time 10 :
P4 completes at time 10.
Gantt Chart :
P1 P2 P3 P4
0 4 7 9 10

Calculations :
Process AT BT Priority CT TAT WT RT
P1 0 4 1 4 4 0 0
P2 1 3 2 7 6 3 3
P3 2 2 3 9 7 5 5
P4 3 1 4 10 7 6 6
Averages :
TAT = (4+7+9+10)/4 = 30/4 = 7.5
WT = (0+3+5+6)/4 = 14/4 = 3.5
RT = (0+3+5+6)/4 = 14/4 = 3.5

Advantages :
• Provides timely execution of higher-priority processes.
• Can handle real-time tasks more effectively.
Disadvantages
• Starvation: Lower priority processes may never get executed if there are always
higher priority processes.
• Overhead: Frequent context switching due to preemption can increase overhead.

You might also like