#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
typedef struct process
#include<conio.h>
#include<iomanip.h>
typedef struct process
{
int pid,at,bt,et,ct,wt,tat;
int priority;
}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 main()
{
queue arr,ready,comp;
int n,i,time=0,ts,start,end;
int ttat=0,twt=0;
float atat,awt;
process p,p1;
clrscr();
init(&arr);
cout<<"\nEnter time slice = ";
cin>>ts;
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;
p.et=p.ct=p.wt=0;
insert(&arr,p);
}
init(&comp);
init(&ready);
while(!empty(&arr)) //process at time =0 moved into ready queue
{
p=front(&arr);
if(p.at<=time)
{
p=del(&arr);
insert(&ready,p);
}
else
break;
}
while(!empty(&arr)||!empty(&ready))
{
if(empty(&ready))
{
time++;
while(!empty(&arr)) //processes at current time added to ready queue
{
p=front(&arr);
if(p.at<=time)
{
p=del(&arr);
insert(&ready,p);
}
else
break;
}
}
else
{
start=time;
p=del(&ready);
if(ts>p.bt-p.et)
{
time=time+p.bt-p.et;
p.et=p.bt;
}
else
{
p.et=p.et+ts;
time=time+ts;
}
while(!empty(&arr))
{
p1=front(&arr);
if(p1.at<=time)
{
p1=del(&arr);
insert(&ready,p1);
}
else
break;
}
if(p.et==p.bt)
{
p.ct=time;
p.tat=p.ct-p.at;
p.wt=p.tat-p.bt;
insert(&comp,p);
}
else
{
insert(&ready,p);
}
end=time;
cout<<"\n("<<start<<")-P"<<p.pid<<"-("<<end<<")";
}
}
cout<<"\n";
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