Priority scheduling

SCHEDULING ALGORITHMS

Priority Scheduling 


// Program

// 3. Priority scheduling

#include<stdio.h>

#include<stdlib.h>

typedef struct process

{

int pid, ft, wt, bt, priority;

}pr;

int n;

pr p[10];

void ps();

void disp_gantt();

int main()

{

int i;

printf(“\nHow many processes : “);

scanf(“%d”,&n);

printf(“Enter the values for burst time and priority: \n”);

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

{

printf(“\tEnter for Process %d : “,i);

printf(“\tBurst time : “);

scanf(“%d”,&p[i].bt);

printf(“\t\t\t\tPriority : “);

scanf(“%d”,&p[i].priority);

p[i].pid=i;

}

ps();

}

void ps()

{

int i,j;

pr temp;

float avgwt, avgtat, sum_wt=0, sum_tat=0;

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

for(j=0;j<n-i-1;j++)

if(p[j].priority>p[j+1].priority)

{

temp=p[j];

p[j]=p[j+1];

p[j+1]=temp;

}

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

{

if(i==0)

p[i].wt=0;

else

p[i].wt=p[i-1].ft;

p[i].ft=p[i].wt+p[i].bt;

sum_wt=sum_wt+p[i].wt;

sum_tat=sum_tat+p[i].ft;

}

avgwt=sum_wt/n;

avgtat=sum_tat/n;

disp_gantt(n);

printf(“\nAverage Turnaround Time : %5.2f ms”,avgtat);

printf(“\nAverage Waiting Time : %5.2f ms \n\n”, avgwt);

}

void disp_gantt()

{

int i;

printf(“\n\nGANTT CHART\n\n\t”);

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

printf(“P%d\t”,p[i].pid);

printf(“\n   0\t”);

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

printf(”    %d\t”,p[i].ft);

printf(“\n”);

}