Search This Blog

Tuesday, February 23, 2010

sjf and priority scheduling programs

SJF


main()
{
int b[10],p[10],t[10],w[10],n,i,j,tot=0;
float avg;
printf("\n Enter the number of Process:");
scanf("%d",&n);
for(i=0;i
{
printf("\n Enter the process no:");
scanf("%d",&p[i]);
printf("\n Enter the burst no:");
scanf("%d",&b[i]);
}
for(i=0;i
{
for(j=i+1;j
{
if(b[i]>b[j])
{
int temp;
temp=p[i];
p[i] = p[j];
p[j] = temp;
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
w[0] = 0;
t[0] = b[0];
for(i=1;i
{
w[i] = t[i-1];
t[i] = w[i] + b[i];
}
printf("\n\n Gantt Chart");
printf("\n\n_______________________________________________________________");
printf("\n| P%d\t|",p[0]);
for(i=1; i
printf("P%d\t|",p[i]);
printf("\n---------------------------------------------------------------\n");
printf("%d",w[0]);
printf("\t%d",t[0]);
for(i=1;i
printf("\t%d",t[i]);
printf("\n\n");
printf("\n Process \t Burst Time \t Waiting Time \t Turnaround time");
printf("\n ------- \t ---------- \t ------------ \t ---------------\n");
for(i=0;i
{
printf("\n P%d \t %d \t %d \t %d ",p[i],b[i],w[i],t[i]);
tot = tot + w[i];
}
avg = (float)tot/n;
printf("\n\n The average waiting time is %f",avg);

}

Priority Scheduling

main()
{
int b[10],p[10],t[10],w[10],prior[10],n,i,j,tot=0;
float avg;

printf("\n Enter the number of Process:");
scanf("%d",&n);
for(i=0;i
{
printf("\n Enter the process no:");
scanf("%d",&p[i]);
printf("\n Enter the burst no:");
scanf("%d",&b[i]);
printf("\n Enter the Priority:");
scanf("%d",&prior[i]);
}
for(i=0;i
{
for(j=i+1;j
{
if(prior[i]>prior[j])
{
int temp;
temp = prior[i];
prior[i] = prior[j];
prior[j] = temp;
temp=p[i];
p[i] = p[j];
p[j] = temp;
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
w[0] = 0;
t[0] = b[0];
for(i=1;i
{
w[i] = t[i-1];
t[i] = w[i] + b[i];
}
printf("\n\n Gantt Chart");
printf("\n\n_______________________________________________________________");
printf("\n| P%d\t|",p[0]);
for(i=1;i
printf("P%d\t|",p[i]);
printf("\n---------------------------------------------------------------\n");
printf("%d",w[0]);
printf("\t%d",t[0]);
for(i=1;i
printf("\t%d",t[i]);
printf("\n\n");
printf("\n Process \t Burst Time \t Priority \t Waiting Time \t Turnaround time");
printf("\n ------- \t ---------- \t -------- \t ------------ \t ---------------\n");
for(i=0;i
{
printf("\n P%d \t %d \t %d \t %d \t %d ",p[i],b[i],prior[i],w[i],t[i]);
tot = tot + w[i];
}
avg = (float)tot/n;
printf("\n\n The average waiting time is %f",avg);

}

No comments:

Post a Comment