Infinite loop in Round Robin process scheduling algorithm
Hey again.
Well, I got all the kinks out of it from before, but now I'm at a block again. My Round Robin algorithm seems to be stuck in an infinite loop. :(
I can't figure out where it's looping forever, and I'm not so good at debugging on Linux. Can anyone help me out?
What it's supposed to do it take a list of processes (sorted by time arrived, and also containing the required burst time), process them, and output the average wait time and average turnaround time. I still have to implement checking to see if a process has arrived at a time or not, but I will leave that until this is working.
The data file is located here.
Edit: I almost forgot, this program takes two numerical arguments for the time quanta and context switch time, eg. rr 5 5.
Here is what I have so far:
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
/* Process Data Structure */
struct process {
int arrive; /* Process Arrival Time */
int burst; /* CPU Burst Time */
float working; /* Working time*/
float waiting; /* Waiting time*/
struct process *next;
};
struct process *init_process (int pid, int burst);
void rr (process *proc, int burst, float cswitch);
int main(int argc, char* argv[]) {
process *plist,*ptmp;
int init = 0;
int quanta = static_cast<int>(std::strtol(argv[1], NULL, 0));
int context_switch = static_cast<int>(std::strtol(argv[2], NULL, 0));
float cswitch = context_switch/1000;
ifstream input;
input.open("rrdata.txt");
if (input.fail()) {
cerr << "unable to open file rrdata.txt for reading" << endl;
exit(1);
}
//Initialize the list of processes
int arrive,burst;
input>>arrive>>burst;
plist = init_process(arrive,burst);
init++;
input>>arrive>>burst;
plist -> next = init_process(arrive,burst);ptmp = plist->next;
init++;
for (int i = 0; i < 98; i++) {
input>>arrive>>burst;
ptmp -> next = init_process(arrive,burst);ptmp = ptmp->next;
init++;
}
input>>arrive>>burst;
ptmp -> next = init_process(arrive,burst);
init++;
rr(plist,quanta,cswitch);
input.close();
return 0;
}
struct process *init_process (int arrive, int burst) {
struct process *proc;
proc = new process;
if (proc == NULL) {
printf("Fatal error: memory allocation failure.\nTerminating.\n");
exit(1);
};
proc->arrive = arrive;
proc->burst = burst;
proc->working = 0;
proc->waiting = 0;
proc->next = NULL;
return(proc);
};
void rr (process *proc, int quanta, float cswitch){
float burst = 0;
float wait = 0;
float switches = 0;
float time = 0;
process *head = proc; /*Used to keep a pointer to the top node.*/
process *trail = proc; /*Used to keep a pointer to the previous node in the event that a node must be removed.*/
int processesleft = 100;
int i = 0;
ofstream output;
output.open("output.txt");
if (output.fail()) {
cerr << "unable to open file output.txt" << endl;
exit(1);
}
while(processesleft != 0){
/*If there is more then quanta burst time left and there is another process in the queue, do this:*/
if(proc->burst > quanta && proc->next != NULL)
{
proc->working += quanta;
proc->waiting = time - proc->waiting;
proc->burst -= quanta;
trail = proc;
proc = proc->next;
time += quanta;
switches++;
output<<"time removed, new process"<<endl;
}
/*If there is less then or equal to the quanta burst time left and there is another process in the queue, do this:*/
else if(proc->burst <= quanta && proc->next != NULL)
{
proc->working += proc->burst;
proc->waiting += time-proc->waiting;
proc->burst = 0;
time += proc->burst;
burst = proc->working;
wait = proc->waiting;
proc = proc->next;
trail->next = proc;
switches++;
processesleft--;
output<<"time reomved, process removed, new process"<<endl;
}
/*If there is more then quanta burst time left and there is not another process in the queue, do this:*/
else if(proc->burst > quanta && proc->next != NULL)
{
proc->working += quanta;
proc->waiting = time - proc->waiting;
proc->burst -= quanta;
time += quanta;
output<<"time removed, no new process"<<endl;
}
/*If there is less then or equal to the quanta burst time left and there is not another process in the queue, do this:*/
else if(proc->burst <= quanta && proc->next != NULL)
{
proc->working += proc->burst;
proc->waiting += time-proc->waiting;
time += proc->burst;
proc->burst = 0;
burst = proc->working;
wait = proc->waiting;
processesleft--;
output<<"time removed,process removed"<<endl;
}
}
switches *= cswitch;
burst += switches;
output<<wait/100<<" "<<burst/100;
output.close();
};