3. Experiment
Aim: Write a program to implement Priority CPU Scheduling algorithm.
Theory:
Priority CPU Scheduling executes processes based on their priority.
Each process is assigned a priority number.
The CPU always picks the ready process with the highest priority (lower number = higher priority or higher number = higher priority, depending on implementation).
It can be preemptive or non-preemptive.
If a new process with higher priority arrives, it may interrupt (preempt) the currently running process.
Advantages:
Allows important or urgent tasks to execute first.
Better control over CPU allocation based on urgency or importance.
Allows for preemptive scheduling, which makes the algorithm more responsive.
Helps in real-time systems where certain tasks must be finished promptly.
Disadvantages:
Starvation: Low-priority processes may wait indefinitely if high-priority ones keep arriving.
Requires prior knowledge or criteria to assign priorities accurately.
May suffer from priority inversion (lower-priority holds resources needed by higher-priority).
Less fair to lower-priority tasks.
Flowchart
Algorithm
Step 1: Start
Step 2: Input number of processes (n).
Step 3: Loop from 1 to n:
Input Arrival Time, Burst Time, and Priority for each process.
Step 4: Sort processes by Arrival first; if Arrival is same, then by Higher Priority.
Step 5: Set current_time = 0.
Step 6: Loop through all processes:
If current_time < Arrival, then CPU waits until Arrival.
Then execute process immediately.
Update:
Start Time = current_time
Finish Time = Start Time + Burst
Waiting Time = Start Time - Arrival
Turnaround Time = Finish Time - Arrival
current_time = Finish Time
Step 7: Display all process details.
Step 8: End
Source Code :
// C++ code for Non-preemptive Priority CPU Scheduling where number of processes is given by the user.
#include <iostream>
#include <algorithm>
using namespace std;
struct Process {
int id,at, bt, pr, wt, tat,ct;
};
bool compareProcess(Process a, Process b) {
if (a.at == b.at) return a.pr < b.pr;
return a.at < b.at;
}
int main()
{
int n;
cout << "Enter number of processes: ";
cin >> n;
Process p[n];
for(int i = 0; i < n; i++) {
p[i].id = i + 1;
cout << "Enter Arrival Time for P" << i + 1 << ": "; cin >> p[i].at;
cout << "Enter Burst Time for P" << i + 1 << ": "; cin >> p[i].bt;
cout << "Enter Priority for P" << i + 1 << ": "; cin >> p[i].pr;
}
sort(p, p + n, compareProcess);
int current = 0;
for(int i = 0; i < n; i++) {
if (current < p[i].at) current = p[i].at;
p[i].ct = current + p[i].bt;
p[i].tat = p[i].ct - p[i].at;
p[i].wt = p[i].tat - p[i].bt;
current = p[i].ct;
}
cout << "PID\tAT\tBT\tPr\tCT\tTAT\tWT\n";
for(int i = 0; i < n; i++) {
cout << p[i].id << '\t' << p[i].at << '\t' << p[i].bt << '\t' << p[i].pr << '\t' << p[i].ct << '\t' << p[i].tat << '\t' << p[i].wt << '\n';
}
}
// Program should be with output in Lab file
MCQs on Priority CPU Scheduling Algorithm
1. What principle does Priority Scheduling follow?
a) First Come First Served
b) Shortest Job First
c) Higher Priority First
d) Round Robin
Answer: c) Higher Priority First
2. Priority can be:
a) Static
b) Dynamic
c) Both
d) None
Answer: c) Both
3. If two processes have the same priority, which is typically used to break the tie?
a) Arrival order
b) CPU burst
c) Process size
d) User
Answer: a) Arrival order
4. In preemptive priority, a new process can interrupt:
a) A process with lower priority
b) A process with higher priority
c) All processes
d) None
Answer: a) A process with lower priority
5. The main problem with priority scheduling is:
a) Low throughput
b) Low CPU utilization
c) Starvation
d) Short waiting time
Answer: c) Starvation
6. To avoid starvation, we use:
a) Preemption
b) Aging
c) Large time quantum
d) First come first served
Answer: b) Aging
7. Priority is often represented by:
a) Large number for highest
b) Small number for highest
c) Average waiting time
d) CPU utilization
Answer: b) Small number for highest
8. Priority scheduling is mainly used in:
a) Batch systems
b) Interactive or real-time systems
c) File transfer
d) Educational software
Answer: b) Interactive or real-time systems
9. What happens when a process with higher priority enters ready queue?
a) It waits for its turn
b) It preempts the CPU immediately
c) It exits immediately
d) It merges with a lower-priority process
Answer: b) It preempts the CPU immediately
10. The phenomenon where lower-priority process waits indefinitely is called:
a) Preemption
b) Aging
c) Starvation
d) Dispatch
Answer: c) Starvation
11. To solve starvation, we can:
a) Reduce context switches
b) Implement aging
c) Sort by CPU burst
d) Increase quantum
Answer: b) Implement aging
12. Priority scheduling can be:
a) Preemptive only
b) Non-preemptive only
c) Both
d) Neither
Answer: c) Both
13. Small priority number means:
a) Higher priority
b) Lower priority
c) Medium priority
d) undefined
Answer: a) Higher priority
14. If priorities are not defined in the process control block, OS typically:
a) Initializes all to zero
b) Initializes all to maximum
c) Initializes all to the same
d) Initializes randomly
Answer: c) Initializes all to the same
15. The main advantage of priority scheduling is:
a) Fairness to all
b) Better control of process urgency
c) Lower waiting time for all
d) Higher throughput
Answer: b) Better control of process urgency