4.priority Scheduling

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

4).Implement priority scheduling algorithm.

While executing, no process should wait for more than 10


seconds. If waiting time is more than 10 seconds, that process has to be executed for atleast 1 second
before waiting again.

Aim: Implement priority scheduling algorithm

Description: In this algorithm, processes are scheduled based on their priority, and no process should
wait for more than 10 seconds. If a process has been waiting for more than 10 seconds, it will be
executed for at least 1 second before going back to the waiting queue. The priority scheduling algorithm
with the specified conditions and prints the execution order of processes.

PROGRAM:

#include <stdio.h>
#include <stdlib.h>

// Structure to represent a process


struct Process {
char name;
int priority;
int burst_time;
int wait_time;
};

// Function to perform priority scheduling


void priority_scheduling(struct Process processes[], int n) {
int current_time = 0;
while (n > 0) {
int highest_priority = -1;
int highest_priority_index = -1;
for (int i = 0; i < n; i++) {
if (processes[i].burst_time > 0 && processes[i].priority > highest_priority &&
processes[i].wait_time <= 10) {
highest_priority = processes[i].priority;
highest_priority_index = i;
}
}

if (highest_priority_index == -1) {
// No process can be scheduled, so just increment the time
current_time++;
} else {
if (processes[highest_priority_index].burst_time > 10) {
current_time += 10;
processes[highest_priority_index].burst_time -= 10;
processes[highest_priority_index].wait_time += 10;
printf("Process %c executed for 10 seconds at time %d\n",
processes[highest_priority_index].name, current_time);
} else {
current_time += processes[highest_priority_index].burst_time;
processes[highest_priority_index].wait_time += processes[highest_priority_index].burst_time;
printf("Process %c executed for %d seconds at time %d\n",
processes[highest_priority_index].name,
processes[highest_priority_index].burst_time, current_time);
processes[highest_priority_index].burst_time = 0;
n--;
}
}
}
}

int main() {
struct Process processes[] = {
{'P', 1, 25, 0},
{'Q', 2, 15, 0},
{'R', 3, 10, 0},
};
int n = sizeof(processes) / sizeof(processes[0]);
priority_scheduling(processes, n);
return 0;
}

Sample output

Process R executed for 10 seconds at time 10


Process Q executed for 10 seconds at time 20
Process Q executed for 5 seconds at time 25
Process P executed for 10 seconds at time 35
Process P executed for 10 seconds at time 45

Result

You might also like