#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
struct node
{
long program_no;
long arrival_time;
long cpu_time;
node *F;
node *B;
}*wq,*rq;
void menu(); //For the display of menu
void wqueue();
node* ready_process();
void display_queue(node *);
void rqueue(node *);
void running();
void interval(node *);
long input_pno_check(long);
long input_int();//For integer Input
long ts=0;
int TIME=0;
//////////////////////////Start of main////////////////////////////////////
void main()
{
rq=wq=NULL;
clrscr();
char str[10];
int n;
menu();
cout<<"\nEnter The No Of Processes ::" ; n=input_int();
for(int i=0;i<n;i++)
wqueue();
cout<<"\n\n\nEnter The Time Slice ::" ; ts=input_int();
display_queue(wq);
node *r;
while(wq!=NULL || rq!=NULL)
{
do{
if(wq!=NULL)
{
r=ready_process();
if(TIME==0)
TIME= r->arrival_time;
while(wq!=NULL && r->arrival_time<=TIME)
{
rqueue(r);
if(wq!=NULL)
r=ready_process();
}
running();
}
}while(wq!=NULL && r->arrival_time<=TIME);
running();
}
cout<<"\n\nAll Process Completed Successfully ::: ";
getch();
}
///////////////////
///////End of main////////////////////////////////////
////////////////////////////Page No input////////////////////////////
long input_pno_check(long pno)
{
for(node *p=wq; p!=NULL;p=p->F)
if(p->program_no==pno) //Pagram No Already Exist
return 0; // Return 0 if program id exists
return -1;
}
/////////////////////////End Of Page No Input////////////////////////
///////////////////////////////////////////////////////////////////////
void wqueue()
{
cout<<endl;
int valid,pno;
cout<<"Enter The Program Id ::";
do{
valid=-1;
pno=input_int();
valid=input_pno_check(pno);
if(valid==0)
cout<<"Program id Already Exist ::\nEnter The Program Id ::";
}while(valid==0);
node *p=new node;
p->program_no =pno; //so creating new nodes
cout<<"Arrival Time ::";p->arrival_time =input_int();
cout<<"CPU Time ::";p->cpu_time =input_int();
p-> F =NULL;
p-> B =NULL;
if(wq==NULL)
wq=p;
else
{
node *q=wq;
while(q->F!=NULL)
q=q->F;
q->F=p;
p->B=q;
}
}
/////////////////////////////////////////*****************//////////////////
void rqueue(node* program)
{
cout<<"\nTime "<<TIME<<" :: Process "<<program->program_no<<" Ready ::\n";
// Remove node from wq
{
if(program==wq)
{
wq=wq->F;
wq->B=NULL;
}
else
{
program->B->F=program->F;
program->F->B=program->B;
}
program->B=NULL;
program->F=NULL;
} //end of remove from wq
if(rq==NULL)
rq=program;
else
{
/*
program->F=rq;
rq->B=program;
rq=program;
*/
node *q=rq;
while(q->F!=NULL)
q=q->F;
q->F=program;
program->B=q;
}
}
/////////////////////////////////////////*****************//////////////////
node* ready_process()
{
node *p,*q;
p=wq;
q=wq;
while(q->F!=NULL)
q=q->F;
while(p!=q)
{
if(p->arrival_time > q->arrival_time)
p=p->F;
else
q=q->B;
}
return p;
}
////////////////////////////////////////////////////////////
void running()
{
node *p=rq,*r;
if(p->cpu_time>ts)
{
cout<<"\nTime "<<TIME<<" :: Process "<<p->program_no<<" Running ::\n";
p->cpu_time=p->cpu_time-ts;
for(int i=0;i<ts;i++)
{
TIME++;
if(wq!=NULL)
r=ready_process();
if(wq!=NULL && r->arrival_time<=TIME)
{
rqueue(r);
}
}
}
else if (p->cpu_time==ts)
{
cout<<"\nTime "<<TIME<<" :: Process "<<p->program_no<<" Running ::\n";
interval(p);
p->cpu_time=p->cpu_time-ts;
}
else
{
cout<<"\nTime "<<TIME<<" :: Process "<<p->program_no<<" Running ::\n";
interval(p);
p->cpu_time=0;
}
if(p->cpu_time==0)
{
rq=rq->F;
rq->B=NULL;
delete p;
p=NULL;
}
if(p!=NULL && p->F!=NULL)
{
rq=rq->F;
rq->B=NULL;
p->F=NULL;
for(node*q=rq;q->F!=NULL;q=q->F);
q->F=p;
p->B=q;
}
}
/////////////////////////////////////////////////////////////
void interval(node *p)
{
node *r;
for(int i=0;i<p->cpu_time;i++)
{
TIME++;
if(wq!=NULL)
r=ready_process();
if(wq!=NULL && r->arrival_time<=TIME)
{
rqueue(r);
cout<<"\nTime "<<TIME<<" :: Process "<<p->program_no<<" Running ::\n";
}
}
cout<<"\nTime "<<TIME<<" :: Process "<<p->program_no<<" Done ::\n";
char ch;
cout<<"\n\nDo You Want To Initiate AnOther Process [Y/N] :: ";cin>>ch;
if(ch=='y'||ch=='Y')
wqueue();
}
/////////////////To Display List wq and rd Both////////////////
void display_queue(node *top)
{
cout<<"\nProgram ID\tArrival Time\tCPU Time\t" ;
cout<<"\n----------\t------------\t--------\t"<<endl ;
long i=0;
for(node *p=top;p!=NULL;p=p->F)
{
cout<<p->program_no<<"\t\t";
cout<<p->arrival_time<<"\t\t";
cout<<p->cpu_time<<"\t\t";
cout<<endl;
i++; //counting the number
}
cout<<"Total Process ::"<<i<<endl<<endl;
}
//////////////////End of display function////////////////
/////////////////Start of integer input Function////////////////////////////
long input_int() //this function control wrong entry checks in integer values
{
char str[8];
long len=0,flag,value;
do
{
flag=0;
gets(str);
len=strlen(str);
for (long i=0;i<len;i++)
{
if (str[i] < 48 || str[i] > 57)
flag=1;
}
if (flag==1 || len<1) cout<<"Data Type Mismatch::\nEnter The Value Again :: " ;
value=atol(str);
}
while(flag==1 ||len<1);
return (value);
}
/////////////////End of integer input Function////////////////////////////
//////////////////Start of Menu Function//////////////////////////////////
void menu()
{
cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n";
cout<<"³ ////******THIS IS SIMULATED ENVIRNOMENT FOR ³\n";
cout<<"³ ROUND ROBIN FOR N PROCESSES*********/////// ³\n";
cout<<"³ ³\n";
cout<<"³ ENTER THE FOLLOWING REQUIRED VALUES TO GET THE RESULTS: ³\n";
cout<<"³ ³\n";
cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n";
}
//////////////////End of Menu Function//////////////////////////////////