Monday, 2 September 2013

SJF CPU SCHEDULING C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
program in C to implement shortest job first CPU scheduling algo
calculate avg. waiting time & average turn around time*/

#include<stdio.h>
#include<conio.h>
int main()
{
int p[10],temp;
int tot=0,wt[10],pt[10],tat[10],i,j,n,temp1,tot1=0;
float avg=0,avg1=0;
printf("enter no of processes:");
scanf("%d",&n);
printf("enter process time");
for(i=0;i<n;i++)
{
scanf("%d",&pt[i]);
p[i]=i;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
temp=p[i];
p[i]=p[j];
p[j]=temp;

}
}
}
wt[0]=0;
for(i=1;i<=n;i++)
{
wt[i]=wt[i-1]+pt[i-1];

tot=tot+wt[i];
}
for(i=0;i<n;i++)
   {tat[i]=wt[i]+pt[i];
    tot1=tot1+tat[i];}
avg=(float)tot/n;
avg1=(float)tot1/n;
printf("p_no.\t P_time\t Waiting_Time\t  Turn_Around_Time\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t %d\n",p[i],pt[i],wt[i],tat[i]);
printf("avg waiting time=%f\n",avg);
printf("avg turn around time=%f",avg1);
printf("\n\n \t GANTT CHART \n");
    for(i=0;i<n;i++)
        printf("P%d \t",i);
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d \t",wt[i]);
    }
        getch();
}
/* OUTPUT
Enter the no. of processes 5
Enter the burst time of processes
12 8 4 6 10
P_no.  P_Time  Waiting_Time  Turn_Around_Time
2 4     0 4
3 6     4 10
1 8     10 18
4       10        18 28
0 12     28 40

Average waiting time is 20.000000
Average turn around time is 20.000000

GANTT CHART
P0 P1 P2 P3 P4
0 4 10 18 28*/

No comments:

Post a Comment