0% found this document useful (0 votes)
14 views5 pages

OS Pract 7 Priority

Uploaded by

Indo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

OS Pract 7 Priority

Uploaded by

Indo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical no.

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 :

Priority Scheduling Algorithm in C:


In the priority scheduling algorithm, each process has a priority associated
with it and as each process hits the queue, it is stored based on its priority so
that processes with higher priority is dealt with first. It should be noted that
equal priority processes are scheduled in FCFS order.

To prevent high priority processes from running indefinitely the scheduler


may decrease the priority of the currently running process at each clock tick
(i.e., at each clock interrupt). If this action causes its priority to drop below
that of the next highest process, a process switch occurs. Alternatively, each
process may be assigned a maximum time quantum that it is allowed to run.
When this quantum is used up, the next highest priority process is given a
chance to run.

 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:

Here is an example of a Priority Scheduling algorithm.

Process Arrival Time Burst Time Priority

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

Fig.: Gantt chart

Turnaround Time = Completion Time – Arrival Time


P1 turnaround time: 17 - 0 = 17
P2 turnaround time: 21 - 1= 20
P3 turnaround time: 11 - 2 = 9
P4 turnaround time: 26 - 3 = 23

Average Turnaround Time = (17+20+9+23)/4 = 17.25

Waiting Time = Turnaround Time – Burst Time


P1 waiting time: 17-8=9
P2 waiting time: 20-4 =16
P3 waiting time: 9-9=0
P4 waiting time: 23-5=18

Average Waiting Time = (9 + 16+ 18)/4 = 17/3 = 10.7


Program/Source Code:

#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
};

void calculateTimes(struct Process proc[], int n) {


int currentTime = 0, completed = 0;
int totalWaitingTime = 0, totalTurnAroundTime = 0;

while (completed < n) {


int idx = -1;
int highestPriority = 9999; // Assuming lower number means higher priority

// 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++;

// If the process is completed


if (proc[idx].remainingTime == 0) {
proc[idx].turnAroundTime = currentTime - proc[idx].arrivalTime;
proc[idx].waitingTime = proc[idx].turnAroundTime - proc[idx].burstTime;

totalWaitingTime += proc[idx].waitingTime;
totalTurnAroundTime += proc[idx].turnAroundTime;
completed++;
}
} else {
// If no process is ready, increase the time (idle time)
currentTime++;
}
}

// Display process details


printf("\nProcess\tBurst Time\tPriority\tArrival Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("P%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", proc[i].pid, proc[i].burstTime,
proc[i].priority, proc[i].arrivalTime, proc[i].waitingTime, proc[i].turnAroundTime);
}

printf("\nAverage Waiting Time: %.2f\n", (float)totalWaitingTime / n);


printf("Average Turnaround Time: %.2f\n", (float)totalTurnAroundTime / n);
}

int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);

struct Process proc[n];

for (int i = 0; i < n; i++) {


proc[i].pid = i + 1;
printf("Enter burst time, priority, and arrival time for process P%d: ", i + 1);
scanf("%d %d %d", &proc[i].burstTime, &proc[i].priority, &proc[i].arrivalTime);
proc[i].remainingTime = proc[i].burstTime; // Initialize remaining time
}

// Calculate waiting and turnaround times based on preemptive priority scheduling


calculateTimes(proc, 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`.

Conclusion : Successfully, Developed, Debuged and executed a C program to


simulate the Priority (pre-emptive ) CPU Scheduling algorithms to find
Turnaround Time and Waiting Time.

You might also like