Priority Scheduling
#include <stdio.h>
#include <conio.h>
struct proc
{
int id;
int burst;
int pi;
};
int main()
{
int n, i, j, total_waiting_time, waiting_time;
printf("Enter the number of processess: ");
scanf("%d", &n);
struct proc p[n], temp;
//To reduce complexity we have taken arrival time of all -
// -processes to be same(0);
//If we take arrival time, than first sort on basis of arrival time,
//and can also sort for burst time for processes having same priority.
for (i = 0; i < n; i++)
{
p[i].id = i;
printf("Enter the priority of the process: ");
scanf("%d", &p[i].pi);
printf("Enter the burst time of the process: ");
scanf("%d", &p[i].burst);
}
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (p[j].pi > p[j + 1].pi)
{
temp.pi = p[j].pi;
temp.burst = p[j].burst;
temp.id = p[j].id;
p[j].id = p[j + 1].id;
p[j].burst = p[j + 1].burst;
p[j].pi = p[j + 1].pi;
p[j + 1].id = temp.id;
p[j + 1].pi = temp.pi;
p[j + 1].burst = temp.burst;
}
}
}
waiting_time = 0, total_waiting_time = 0;
printf("\nScheduling information \n");
for (i = 0; i < n; i++)
{
total_waiting_time += waiting_time;
printf("process id: %d Priority: %d burst time:%d waiting time: %d\n",
p[i].id, p[i].pi, p[i].burst, waiting_time);
waiting_time += p[i].burst;
}
printf("\nThe average waiting time: %1.2f: ",
((float)total_waiting_time / n));
}
Comments
Post a Comment