/* 2.simulate Shortest Job First (preemptive) */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,k,n,Bt[10],wt[10],B[10],A[10],Tt=0;
char s[20],start[20];
int max=0,min,Time=0,z[50];
float Twt=0.0,Awt;
int w=0,flag=0,t=0;
clrscr();
printf("\n Enter no. of processes ::");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the Burst time for process P%d::",i);
scanf("%d",&Bt[i]);
printf("\n Enter the Arrival time for process P%d::",i);
scanf("%d",&A[i]);
B[i]=Bt[i];
if(B[i]>max)
max=B[i];
if(A[i]>Time)
Time=A[i];
wt[i]=0;
s[i]='T';
start[i]='F';
Tt+=B[i];
}
i=1;k=0;
z[k]=0;
k++;
while(t<Time)
{
if(A[i]<=t && B[i]!=0)
{
if(flag==0)
wt[i]=wt[i]+w;
B[i]=B[i]-1;
if(B[i]==0)
s[i]='F';
start[i]='T';
t++;
w=w+1;
if(s[i]!='F')
{
j=1;
flag=1;
while(j<=n&&flag!=0)
{
if(s[j]!='F'&&B[i]>B[j]&&A[i]<=t&&i!=j)
{
flag=0;
z[k]=w;
wt[i]=wt[i]-w;
i=j;
k++;
}
else
flag=1;
j++;
}
}
else
{
i++;
j=1;
while(A[j]<=t && j<=n)
{
if(B[i]>B[j] && s[j]!='F')
{
flag=0;
i=j;
}
j++;
}
}
}
else
{
if(flag==0)
i++;
}
}
while(w<Tt)
{
min=max+1;
i=1;
while(i<=n)
{
if(min>B[i]&&s[i]=='T')
{
min=B[i];
j=i;
}
i++;
}
i=j;
if(w==Time &&start[i]=='T')
{
w+=B[i];
z[k]=w;
k++;
s[i]='F';
}
else
{
wt[i]+=w;
w+=B[i];
z[k]=w;
k++;
s[i]='F';
}
}
printf("\n..............................................\n");
printf("\nPno\tBt\tAt\tWt");
printf("\n..............................................\n");
for(i=1;i<=n;i++)
{
wt[i]=wt[i]-A[i];
printf("\nP%d\t%d\t%d\t%d",i,Bt[i],A[i],wt[i]);
}
printf("\n..............................................\n");
printf("\n\n Gannt Chart ::\n");
for(i=0;i<k;i++)
printf("%4d",z[i]);
for(i=1;i<=n;i++)
Twt+=wt[i];
printf("\n\n Total waiting Time ::%f",Twt);
Awt=Twt/n;
printf("\n\n Average waiting Time ::%f",Awt);
getch();
}
/* Input and Output :-
Enter no. of processes ::4
Enter the Burst time for process P1::8
Enter the Arrival time for process P1::0
Enter the Burst time for process P2::4
Enter the Arrival time for process P2::1
Enter the Burst time for process P3::9
Enter the Arrival time for process P3::2
Enter the Burst time for process P4::5
Enter the Arrival time for process P4::3
..............................................
Pno Bt At Wt
..............................................
P1 8 0 9
P2 4 1 0
P3 9 2 15
P4 5 3 2
..............................................
Gannt Chart ::
0 1 5 10 17 26
Total waiting Time ::26.000000
Average waiting Time ::6.500000
*/
No comments:
Post a Comment