OS Pract 7 Priority
OS Pract 7 Priority
7
Roll no: 2217021 Batch : 2nd DOP :
Name : Epshita Ninawe DOC:
AIM : Develop, debug and Execute a C program to simulate the priority CPU scheduling
algorithms to find turnaround time and waiting time.
THEORY :
Waiting Time:
The total time a process spends in the ready queue waiting for its turn to get
CPU time.
Formula:
Waiting Time = Turnaround Time - Burst Time
Turnaround Time:
The total time taken from when a process is submitted to when it is
completed, including waiting time and execution time.
Formula:
Turnaround Time = Completion Time - Arrival Time
or
Turnaround Time = Waiting Time + Burst Time
Example:
P1 0 8 2
P2 1 4 3
P3 2 9 1
P4 3 5 4
In this example, there are 4 processes with their arrival time, burst time, and
priority. The execution order, waiting time, and turnaround time for each
process will be as given below.
P1 P3 P1 P2 P4
0 2 11 17 21 26
#include <stdio.h>
struct Process {
int pid; // Process ID
int burstTime; // Burst Time
int priority; // Priority
int arrivalTime; // Arrival Time
int remainingTime; // Remaining Time
int waitingTime; // Waiting Time
int turnAroundTime; // Turnaround Time
};
// Find the process with the highest priority that has arrived
for (int i = 0; i < n; i++) {
if (proc[i].arrivalTime <= currentTime && proc[i].remainingTime > 0) {
if (proc[i].priority < highestPriority) {
highestPriority = proc[i].priority;
idx = i;
}
}
}
if (idx != -1) {
// Execute the selected process for 1 time unit
proc[idx].remainingTime--;
currentTime++;
totalWaitingTime += proc[idx].waitingTime;
totalTurnAroundTime += proc[idx].turnAroundTime;
completed++;
}
} else {
// If no process is ready, increase the time (idle time)
currentTime++;
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
return 0;
}
Output:
Preemptive Priority Scheduling Algorithm :
1. Initialize Structures:
- Create a `Process` structure with fields: `pid`, `burstTime`, `priority`,
`arrivalTime`, `remainingTime`, `waitingTime`, `turnAroundTime`.
2. Input Processes:
- Read the number of processes `n`.
- For each process, read `burstTime`, `priority`, and `arrivalTime`, then set
`remainingTime` to `burstTime`.
3. Set Up Variables:
- Initialize `currentTime`, `completed`, `totalWaitingTime`, and
`totalTurnAroundTime`.
4. Scheduling Loop:
- While `completed` < `n`:
- Find the process with the highest priority that has arrived.
- If found, decrement its `remainingTime` and increment `currentTime`.
- If it completes, calculate `waitingTime` and `turnAroundTime`, and increment
`completed`.
- If none found, increment `currentTime` .
5. Output Results:
- Print each process’s details and calculate averages for `waitingTime` and
`turnAroundTime`.