#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
typedef struct process
{
int pid,at,bt,ct,wt,tat;
#include<conio.h>
#include<iomanip.h>
typedef struct process
{
int pid,at,bt,ct,wt,tat;
}process;
typedef struct queue
{
process p[30];
int r,f;
}queue;
void init(queue *q)
{
q->r=q->f=-1;
}
void disp(queue *q)
{
cout<<"\nPID ARR.TIME EXE.TIME COMP.TIME WAIT.TIME TURNAROUND TIME";
int i;
for(i=q->f;i<=q->r;i++)
{ cout<<"\n";
cout<<setw(3)<<q->p[i].pid;
cout<<setw(8)<<q->p[i].at;
cout<<setw(8)<<q->p[i].bt;
cout<<setw(9)<<q->p[i].ct;
cout<<setw(9)<<q->p[i].wt;
cout<<setw(9)<<q->p[i].tat;
}
}
int empty(queue *q)
{
if(q->f==-1)
return 1;
return 0;
}
int full(queue *q)
{
if(q->r==29)
return 1;
return 0;
}
void insert(queue *q,process p)
{
if(full(q))
{
cout<<"\n overflow!!!";
return;
}
if(empty(q))
q->r=q->f=0;
else
q->r=q->r+1;
q->p[q->r]=p;
}
process del(queue *q)
{
if(empty(q))
{
cout<<"\nunderflow!!!";
}
process p;
p=q->p[q->f];
if(q->r==q->f)
init(q);
else
q->f=q->f+1;
return p;
}
process front(queue *q)
{
return(q->p[q->f]);
}
void sort(queue *q)
{
int i,j;
process temp;
int n=q->r-q->f+1;
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(q->p[j].bt>q->p[j+1].bt)
{
temp=q->p[j+1];
q->p[j+1]=q->p[j];
q->p[j]=temp;
}
}
}
}
void main()
{
queue arr,ready,comp;
int n,i,time=0;
int ttat=0,twt=0;
float atat,awt;
process p;
clrscr();
init(&arr);
cout<<"\nEnter the no of processes = ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter PID = ";
cin>>p.pid;
cout<<"Enter arrival time = ";
cin>>p.at;
cout<<"Enter burst time = ";
cin>>p.bt;
insert(&arr,p);
}
init(&comp);
init(&ready);
while(!empty(&arr)||!empty(&ready))
{
while(!empty(&arr))
{
p=front(&arr);
if(p.at<=time)
{
p=del(&arr);
insert(&ready,p);
}
else
break;
}
sort(&ready);
if(empty(&ready))
time++;
else
{
p=del(&ready);
p.ct=time+p.bt;
time=p.ct;
p.tat=p.ct-p.at;
p.wt=p.tat-p.bt;
insert(&comp,p);
}
}
disp(&comp);
for(i=comp.f;i<=comp.r;i++)
{
ttat=ttat+comp.p[i].tat;
twt=twt+comp.p[i].wt;
}
atat=(float)ttat/n;
awt=(float)twt/n;
cout<<"\n Avg waiting time = "<<awt;
cout<<"\n Avg turn-around time = "<<atat;
getch();
}
No comments:
Post a Comment