Hey guys. I'm working on making a round robin process scheduling algorithm and I'm working on it bit by bit. However, I seem to have hit a snag. The following code gives me a segmentation fault after the outputs have displayed. I'm sure I'm missing something completely obvious, but I don't see it. :(
The data file is located here:
Data file
The pogram takes the time quanta and context switch time as arguments, eg. "rr 5 5" is 5 ms burst and 5 ns sontext switch.
Here is my code:
Code:#include <iostream>
#include <fstream>
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);
float rr (struct process *proc);
int main(int argc, char* argv[]) {
struct process *plist,*ptmp;
int init = 0;
int quanta = (int) argv[1];
int context_switch = (int) argv[2];
float cswitch = context_switch/100;
ifstream input;
input.open("rrdata.txt");
if (input.fail()) {
cerr << "unable to open file rrdata.txt for reading" << endl;
exit(1);
}
ofstream output;
output.open("output.txt");
if (output.fail()) {
cerr << "unable to open file output.txt" << endl;
exit(1);
}
//Initialize the list of processes
int arrive,burst;
input>>arrive>>burst;
output<<arrive<<' '<<burst<<endl;
plist = init_process(arrive,burst);
init++;
input>>arrive>>burst;
plist -> next = init_process(arrive,burst);ptmp = plist->next;
init++;
output<<arrive<<' '<<burst<<endl;
for (int i = 0; i < 97; i++) {
input>>arrive>>burst;
ptmp -> next = init_process(arrive,burst);ptmp = plist->next;
init++;
output<<arrive<<' '<<burst<<endl;
}
input>>arrive>>burst;
ptmp -> next = init_process(arrive,burst);
init++;
output<<arrive<<' '<<burst<<endl;
cout<<init<<endl;
cout<<argv[1]<<' '<<argv[2]<<endl;
float avgburst = rr(plist);
return 0;
}
struct process *init_process (int arrive, int burst) {
struct process *proc;
proc = (struct process*) malloc(sizeof(struct 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);
};
float rr (struct process *proc){
float burst = 0;
for(int i=0; i<5; i++){
burst = burst + proc->burst;
proc = proc -> next;
}
return(burst/100);
};