Priority Scheduling Program In C
ProgramingWHAT IS PRIORITY SCHEDULING PROGRAM
Priority Scheduling is a non-preemptive scheduling algorithm where each process is assigned a priority and the CPU is allocated to the process with the highest priority. In Priority Scheduling, the process with the highest priority is executed first, and if two or more processes have the same priority, they are executed in a first-come, first-served (FCFS) manner.
Here's the continuation of the program
1. Reading inputs from the user:
scanf("%d %d", &bt[i], &pri[i]);
}
Sorting the processes based on priority:
c
Copy code
// sorting processes based on priority
for (i = 0; i < n; i++) {
pos = i;
for (j = i + 1; j < n; j++) {
if (pri[j] < pri[pos]) {
pos = j;
}
}
temp = pri[i];
pri[i] = pri[pos];
pri[pos] = temp;
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
}
2. Calculating waiting time and turnaround time for each process
// calculating waiting time and turnaround time
wt[0] = 0;
for (i = 1; i < n; i++) {
wt[i] = 0;
for (j = 0; j < i; j++) {
wt[i] += bt[j];
}
}
3. Printing the final output
printf("\nProcess\t\tBurst Time\tPriority\tWaiting Time\tTurnaround Time");
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d", i + 1, bt[i], pri[i], wt[i], tat[i]);
}
return 0;
}
Here's a Priority Scheduling program in C that takes input for the number of processes, their burst time, and their priority, and then schedules the processes based on their priority using the non-preemptive priority scheduling algorithm.
#include <stdio.h>
int main() {
int n, i, j, pos, temp;
int bt[20], pri[20], wt[20], tat[20];
printf("Enter number of processes: ");
scanf("%d", &n);
printf("\nEnter burst time and priority for each process:\n");
for (i = 0; i < n; i++) {
printf("\nP[%d]: ", i + 1);
scanf("%d %d", &bt[i], &pri[i]);
}
// sorting processes based on priority
for (i = 0; i < n; i++) {
pos = i;
for (j = i + 1; j < n; j++) {
if (pri[j] < pri[pos]) {
pos = j;
}
}
temp = pri[i];
pri[i] = pri[pos];
pri[pos] = temp;
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
}
// calculating waiting time and turnaround time
wt[0] = 0;
for (i = 1; i < n; i++) {
wt[i] = 0;
for (j = 0; j < i; j++) {
wt[i] += bt[j];
}
}
printf("\nProcess\t\tBurst Time\tPriority\tWaiting Time\tTurnaround Time");
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d", i + 1, bt[i], pri[i], wt[i], tat[i]);
}
return 0;
}
In this program, we first take input for the number of processes and their burst time and priority. We then sort the processes based on their priority using a selection sort algorithm. After that, we calculate the waiting time and turnaround time for each process and print the final result.
Note: This program assumes that the arrival time of all the processes is zero.