pointer assignments not holding through multiple functions c++ -
i'm writing small program simulate schedulers inside os , during execution noticing when assign new value head pointer of linked list not carry on next function. if don't have written.
//insert @ head of event queue if(ecursor == ehead){ cout << "insert @ head" << endl; eve->prev = null; eve->next = ecursor; ecursor->prev = eve; ehead = eve; ecursor = eve; }
ive double checked insert login make sure im not missing anything, , works other insertions. @ point never assigns head pointer correctly.
after insert head pointer read main body of program displays time @ event take place (presumably event in made head equal to)
event type: 2
check events in queue
ecount: 1
check event order
eve->time: 0.0667091
ecursor->time: 0.0108831
insert after tail
scheduling arrival: 1
event type: 1
check events in queue
ecount: 1
check event order
eve->time: 0.015281
ecursor->time: 0.0667091
insert @ head
clock: 0.0667091
type 1: arrival
type 2: departure
as can see last insertion inserted @ head , new head time should inserted event time clock not show change.
here of program reference
void insert_event(struct event* eve) { ecursor = ehead; cout << "event type: " << eve->type << endl; cout << "check no events in queue" << endl; cout << "ecount: " << ecount << endl; if(ecount == 0){ ehead = eve; eve->next = null; eve->prev = null; etail = ehead; } //if newevent preceded other events/event queue not empty else{ cout << "check event order" << endl; //insert new arrival event queue do{ cout << "eve->time: " << eve->time << endl; cout << "ecursor->time: " << ecursor->time << endl; //new arrival earlier event cursor points if(ecursor->time >= eve->time){ //insert @ head of event queue if(ecursor == ehead){ cout << "insert @ head" << endl; eve->prev = null; eve->next = ecursor; ecursor->prev = eve; ehead = eve; ecursor = eve; } //insert inside of event queue else{ cout << "insert inside " << endl; ecursor->prev->next = eve; eve->next = ecursor; eve->prev = ecursor->prev; ecursor->prev = eve; } break; } //new arrival after final event in event queue //insert @ tail of event queue else if(ecursor == etail && (ecursor->time < eve->time)){ cout << "insert after tail" << endl; ecursor->next = eve; eve->prev = ecursor; etail = eve; break; } ecursor = ecursor->next; }while(ecursor != null); //reset ecursor head of event queue ecursor = ehead; //increment event queue count ecount++; } int process_event_arr(struct event* eve){ cout << "processing arrival: " << pacount << endl; //generate new process struct process* proc = new process; //process time starts proc->start = simclock; //generate service time process proc->sertime = genexp(1/ts); //if no processes service, service arrival instantly //add departure event if(!server_busy){ phead = proc; schedule_event_dep(proc); server_busy = true; //set cpu proc incpu = proc; } //behavior process queueing else{ //non premptive if(stype == 1 || stype == 3) //process ready queue phead->next = proc; phead = proc; } } //remove arrival event queue remove_event(eve); eve = null; //schedule new arrival event schedule_event_arr(); //increment process count pcount++; pacount++; } int process_event_dep(struct event* eve){ pcursor = phead; cout << "processing dep: " << depcount << endl; //process completed process eve->link->finish = simclock; cout << " if no ready queue release cpu" << endl; if(pcount == 0) server_busy = false; else{ cout << "creating temp process" << endl; struct process* temp = new process; cout << "assigning temp process cursor time" << endl; temp->start = pcursor->start; cout << "schedule next process" << endl; switch(stype){ case 1: //fcfs if(pcursor->start < temp->start) temp = pcursor; pcursor = pcursor->next; while(pcursor != null); schedule_event_dep(temp); incpu = temp; break; default: //error break; }; }; //remove event event queue/process ready queue remove_event(eve); eve = null; //incremement departure count //decrement process/event count dcount++; pcount--; ecount--; } int run_sim(){ struct event* eve = new event; while(!(dcount > 10000)){ eve = ehead; simclock = eve->time; cout << "clock: " << simclock << endl; switch (eve->type){ case arr: process_event_arr(eve); break; case dep: process_event_dep(eve); break; case qtum: process_event_tslice(eve); break; default: //error break; } ehead = eve->next; free(eve); eve = null; } return 0; }
Comments
Post a Comment