#include<stdio.h>
#include<conio.h>
main()
{
int b1[15],b2[15],a[15],p[15],w[15],t[15],pr[15],pr1[15];
int n,i,j,k,t1,tbtime=0;
int i1,j1,e;
printf("Enter the number of process\n");
scanf("%d",&n);
printf("Enter the burst time\n");
for(i=0;i<n;i++)
{
scanf("%d",&b1[i]);
p[i]=i;
}
printf("Enter the arrival time\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter priority:\n");
for(i=0;i<n;i++)
{
scanf("%d",&pr[i]);
}
for(i=0;i<n;i++)
{
pr1[i]=pr[i];
b2[i]=b1[i];
tbtime=tbtime+b2[i];
w[i]=0;
t[i]=0;
}
for(i=0;i<tbtime;i++)//time loop
{
//sorting of left burst time b2[]
for(i1=0;i1<n;i1++)
{
for(j1=i1+1;j1<n;j1++)
{
if(pr1[i1]>pr1[j1])
{
t1=pr1[i1];
pr1[i1]=pr1[j1];
pr1[j1]=t1;
t1=b2[i1];
b2[i1]=b2[j1];
b2[j1]=t1;
t1=a[i1];
a[i1]=a[j1];
a[j1]=t1;
t1=p[i1];
p[i1]=p[j1];
p[j1]=t1;
t1=w[i1];
w[i1]=w[j1];
w[j1]=t1;
t1=b1[i1];
b1[i1]=b1[j1];
b1[j1]=t1;
}
}
}//sorting end
for(j=0;j<n;j++)//process loop
{
if((a[j]<=i)&&(b2[j]>0))//checking whether the process has arrived for execution
{
//t[j]=t[j]+1;
b2[j]=b2[j]-1;
e=j;
break;
}
}
for(k=0;k<n;k++)
{
if((a[k]<=i)&&(b2[k]>0)&&(k!=e))//for waiting process
{
w[k]=w[k]+1;
}
}
// }//end of process loop
}//end of time loop
for(i=0;i<n;i++)
{
t[i]=w[i]+b1[i];
}
printf("process\t b-t\t a-t\t w-t\t t-t\n");
for(i=0;i<n;i++)
{
printf("%d\t %d\t %d\t %d\t %d\t",p[i],b1[i],a[i],w[i],t[i]);
printf("\n");
}
getch();
}
Its a simple approach coding for preemptive priority sjf scheduling..
ReplyDelete