//first all the processes are taken in the array of structure P[]
//THEN a[] contains the burst time of all the process in order of their arival
//min function calculates the minimum value in the array a[], beteen the index number 0 to j where j is number of process which have arived
//maximum valu of j can be equal to number of process
//if the a[i] is 0 means burst time remain is 0 and hence that procee need not be processed hence we will ignore it
// if a[i] is 0 till i to j and j is not equal to the number of process then cpu is idle all the arrived porcess have been finished and waiting for new process
// if a[i] is 0 for all the process and j=number of process then all process have been finished
// Moment burst time of the process becomes 0 at that time of interval value of cl(clock) become completition time of that process
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct process
{
int at;
int bt;
int rt;
int c;//process completiotion time
int wt;
int trt;
};
int min(int *a,int j)
{
int min;
int i=0;
int flag=0;
int pos;
for(i=0;i<=j;i++)
{
if(a[i]==0)
continue;
min=a[i];
pos=i;
flag=1;
break;
}
if(flag==0 &&j!=3)
return -2;
else if(flag==0)
return -1;
else
for(i=0;i<=j;i++)
{
if(a[i]==0)
continue;
if(a[i]<min)
{
min=a[i];
pos=i;
}
}
return pos;
}
void display(struct process *p)
{
int i=1;
for(i=0;i<4;i++)
{
printf("Arrival time%d ",p[i].at);
printf("Burst Time%d ",p[i].bt);
printf("comlete Time%d ",p[i].c);
printf("waiting Time%d ",(p[i].c-p[i].at-p[i].bt));
printf("\n\n");
}
}
main()
{
struct process p[100];
int b[100];
int a[4];//since number of process taken are four
puts("Enetr the process arival time and burst time");
int i=0;
int j=0;
while(1)
{
scanf("%d",&(p[i].at));
scanf("%d",&(p[i].bt));
a[j]=p[i].bt;
j++;
i++;
if(i==4)
break;
}
//array a[] contains the burst time of the process
//array p[] contains the proceess
int x=0;
int y=0;
int m;
j=0;
i=0;
int cl=0;
int pid=2;
while(cl<p[0].at)
{
printf("idle %d---",cl);
cl++;
printf("%d\n",cl);
}
while(x!=-1)
{
a[x]=a[x]-1;
cl++;
if(a[x]==0)
p[x].c=cl;
if(cl<p[i+1].at)
{
x=min(a,j);
if(x==-2)
{
while(cl<p[i+1].at)
{
printf("idle %d---",cl);
cl++;
printf("%d\n",cl);
}
}
}
if(cl==p[i+1].at)
{j++;
x=min(a,j);
i++;
}
if(i==4)
x=min(a,3);
}
printf("\n\n");
display(p);
getch();
}
//THEN a[] contains the burst time of all the process in order of their arival
//min function calculates the minimum value in the array a[], beteen the index number 0 to j where j is number of process which have arived
//maximum valu of j can be equal to number of process
//if the a[i] is 0 means burst time remain is 0 and hence that procee need not be processed hence we will ignore it
// if a[i] is 0 till i to j and j is not equal to the number of process then cpu is idle all the arrived porcess have been finished and waiting for new process
// if a[i] is 0 for all the process and j=number of process then all process have been finished
// Moment burst time of the process becomes 0 at that time of interval value of cl(clock) become completition time of that process
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct process
{
int at;
int bt;
int rt;
int c;//process completiotion time
int wt;
int trt;
};
int min(int *a,int j)
{
int min;
int i=0;
int flag=0;
int pos;
for(i=0;i<=j;i++)
{
if(a[i]==0)
continue;
min=a[i];
pos=i;
flag=1;
break;
}
if(flag==0 &&j!=3)
return -2;
else if(flag==0)
return -1;
else
for(i=0;i<=j;i++)
{
if(a[i]==0)
continue;
if(a[i]<min)
{
min=a[i];
pos=i;
}
}
return pos;
}
void display(struct process *p)
{
int i=1;
for(i=0;i<4;i++)
{
printf("Arrival time%d ",p[i].at);
printf("Burst Time%d ",p[i].bt);
printf("comlete Time%d ",p[i].c);
printf("waiting Time%d ",(p[i].c-p[i].at-p[i].bt));
printf("\n\n");
}
}
main()
{
struct process p[100];
int b[100];
int a[4];//since number of process taken are four
puts("Enetr the process arival time and burst time");
int i=0;
int j=0;
while(1)
{
scanf("%d",&(p[i].at));
scanf("%d",&(p[i].bt));
a[j]=p[i].bt;
j++;
i++;
if(i==4)
break;
}
//array a[] contains the burst time of the process
//array p[] contains the proceess
int x=0;
int y=0;
int m;
j=0;
i=0;
int cl=0;
int pid=2;
while(cl<p[0].at)
{
printf("idle %d---",cl);
cl++;
printf("%d\n",cl);
}
while(x!=-1)
{
a[x]=a[x]-1;
cl++;
if(a[x]==0)
p[x].c=cl;
if(cl<p[i+1].at)
{
x=min(a,j);
if(x==-2)
{
while(cl<p[i+1].at)
{
printf("idle %d---",cl);
cl++;
printf("%d\n",cl);
}
}
}
if(cl==p[i+1].at)
{j++;
x=min(a,j);
i++;
}
if(i==4)
x=min(a,3);
}
printf("\n\n");
display(p);
getch();
}
No comments:
Post a Comment