Sunday, 30 August 2015

Implement the SJF(Shortest job first) scheduling for, Given the list of processes, their CPU burst times and arrival times, print the Gantt chart for SJF and also compute and print the average waiting time and average turnaround time.

#include<stdio.h>

main()

{

float avgwt,avgtt;

char pname[10][10],c[10][10];

int wt[10],tt[10],bt[10],at[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=0; printf("\n\n Enter the number of processes: "); scanf("%d",&n);

printf("\n\n Enter the NAME, BURSTTIME, and ARRIVALTIME of the processes ");

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

{

printf("\n\n NAME : "); scanf("%s",&pname[i]); printf("\n\n BURST TIME : ");

scanf("%d",&bt[i]);

printf("\n\n ARRIVAL TIME : "); scanf("%d",&at[i]);


}

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

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

{

if(at[i]==at[j])

if(bt[i]>bt[j])

{

t=at[i];

at[i]=at[j];

at[j]=t;

q=bt[i];

bt[i]=bt[j];

bt[j]=q;

strcpy(c[i],pname[i]);

strcpy(pname[i],pname[j]);

strcpy(pname[j],c[i]);

}

if(at[i]!=at[j])

if(bt[i]>bt[j])

{

t=at[i];

at[i]=at[j];

at[j]=t;

q=bt[i];

bt[i]=bt[j];

bt[j]=q;

strcpy(c[i],pname[i]);

strcpy(pname[i],pname[j]);

strcpy(pname[j],c[i]);

}

}

wt[0]=0;

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

{

wt[i+1]=wt[i]+bt[i]; sum=sum+(wt[i]-at[i]);

sbt=sbt+(wt[i+1]-at[i]); tt[i]=wt[i]+bt[i]; ss=ss+bt[i];

}

printf("\n\n GANTT CHART");

printf("\n\n---------------------------------------------------------------------- \n");

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

printf("|\t%s\t",pname[i]);

printf("\n-----------------------------------------------------------------------\n");

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

printf("%d\t\t",wt[i]);

printf("%d\n",ss);

printf("\n--------------------------------------------------------------------------");

printf("\n\n Total WAITING TIME = %d ",sum);

printf("\n\n Total TURNAROUND TIME = %d ",sbt);

avgwt=(float)sum/n;

avgtt=(float)sbt/n;

printf("\n\n Average WAITING TIME = %f ",avgwt);

printf("\n\n Average TURNAROUND TIME = %f ",avgtt);

}

OUTPUT:

Enter the number of processes: 5

Enter the NAME, BURSTTIME, and ARRIVALTIME of the processes

NAME : p0

BURST TIME : 2

ARRIVAL TIME : 0

NAME : p1

BURST TIME : 4

ARRIVAL TIME : 0

NAME : p2

BURST TIME : 5

ARRIVAL TIME : 0

NAME : p3

BURST TIME : 6

ARRIVAL TIME : 0

NAME : p4

BURST TIME : 8

ARRIVAL TIME : 0

GANTT CHART

----------------------------------------------------------------------

| p0 | p1 | p2 | p3 | p4

-----------------------------------------------------------------------

0  2   6 11  17  25

Total WAITING TIME = 36

Total TURNAROUND TIME = 61

Average WAITING TIME = 7.200000

No comments:

Post a Comment