I'm working on a project in which I have to implement the Round Robin algorithm
in a c++ code using a circular linked list. I didn't really understand the codes I found on the internet so I decided to do it all myself. The algorithm is easy to understand, but I can't seem to realize what am I doing wrong in my code:
#include<iostream>
#include<cstring>
using namespace std;
struct Nod{
char nameProc[100];
int arrivalT;
int burstT;
Nod *next;
};
Nod *head = NULL;
Nod *tail = NULL;
struct process
{
char nameProcess[100];
int arrival_Time;
int burst_Time;
int inList;
};
void deleteHead (Nod *&head)
{
Nod* toDelete = head;
head = head->next;
delete toDelete;
return;
}
void addArrivals(int time,Nod *&head,struct process p[],int n){
int i;
for(i=0;i<n;i++){
if((p[i].arrival_time<=time)&&(p[i].inlist==0)) {
="" if(head="=NULL)" if="" the="" list="" is="" empty
="" {
="" head="new" nod;="" memory="" for="" first="" one
="" strcpy(head-="">nameProc,p[i].nameProcess);
head->arrivalT = p[i].arrival_Time;
head->burstT = p[i].burst_Time;
head->next = head; tail =head; p[i].inList=1;
}
else { Nod * nod = new Nod; strcpy(nod->nameProc,p[i].nameProcess); nod->arrivalT = p[i].arrival_Time;
nod->burstT = p[i].burst_Time;
nod->next = head; tail->next = nod; tail = nod; p[i].inList=1;
}
}
}
return;
}
void exeProc(Nod *&head, int quantum,int &time)
{ tail=head; head=head->next; if(head->burstT>quantum) {
head->burstT=head->burstT-quantum;
time=time+quantum;
cout <<"At "<<time<<" ,"<<="" head-="">nameProc<< " has been executing for "<<quantum<<" secs.="" it="" has="" "<<head-="">burstT<<" more."<<endl;
}
="" else="" {="" if(head-="">burstT>0) {time=time+head->burstT; cout<<"At "<<time<<" ,"<<head-="">nameProc<<" has been executing for "<<head->burstT<<" secs. The process "<<head->nameProc<<" is finished."<<endl;
head-="">burstT=0;
deleteHead(head);
}}
}
int main()
{
int quantum,n,i,time=0; struct process p[100];
cout<<"Give quantum: "; cin>>quantum;
while (quantum<1||quantum>10)
{
cout << "Quantum is not valid. Give a valid one: ";
cin >>quantum;
}
cout<<"Give number of proc:";
cin>>n;
for(i=0;i<n;i++)
{="" cout<<"add="" name,="" at,="" bt="" "<<i+1<<":="" ";="" arrival="" time="" &="" burst="" time
="" cin="">>p[i].nameProcess;
cin>>p[i].arrival_Time;
cin>>p[i].burst_Time;
p[i].inList=0; }
Nod*head=NULL;
Nod*tail=NULL;
do{
addArrivals(time,head,p,n);
exeProc(head,quantum,time);
}while(head!=NULL); cout<<"At "<<time<<", all="" they="" are="" finished."<<endl;
return="" 0;
}
If anyone could give me some suggestions I'd deeply appreciate it.
What I have tried:
I tried so many different ways of using those functions I've made but I can't seem to get it to give the right output order of the processes. For the input:
name arrival burstTime with quantum=3
P1 0 5
P2 1 3
P3 3 6
P4 5 1
P5 6 4
I am getting the order: P1 P2 P1 P5 P3 P4 P3 P5
Instead of: P1 P2 P3 P1 P4 P5 P3 P5