我正在编写一个小程序来模拟操作系统内的调度程序,并且在执行期间我注意到当我为链接列表的头指针分配一个新值时,这不会延续到下一个函数中。它几乎就像我甚至没有写过。
//insert at head of Event queue
if(ecursor == ehead){
cout << "insert at head" << endl;
eve->prev = NULL;
eve->next = ecursor;
ecursor->prev = eve;
ehead = eve;
ecursor = eve;
}
我已经仔细检查了插入登录,以确保我没有遗漏任何东西,并且它适用于其他插入。在某些时候它永远不会正确指定头指针。
在插入之后,头指针被读入程序的主体,然后显示事件发生的时间(可能是我们刚刚使头部等于的事件)
事件类型:2
检查队列中的事件
ecount:1
检查事件顺序
eve-&gt;时间:0.0667091
ecursor-&gt;时间:0.0108831
尾部后插入
调度到达:1
事件类型:1
检查队列中的事件
ecount:1
检查事件顺序
eve-&gt;时间:0.015281
ecursor-&gt;时间:0.0667091
插入头部
时钟:0.0667091
类型1:到达
类型2:离开
正如您所看到的那样,最后一次插入是在头部插入的,新的头部时间应该是插入的事件时间,但时钟不显示该更改。
这里有一些参考程序
void insert_event(struct event* eve)
{
ecursor = ehead;
cout << "event type: " << eve->type << endl;
cout << "check for no events in queue" << endl;
cout << "ecount: " << ecount << endl;
if(ecount == 0){
ehead = eve;
eve->next = NULL;
eve->prev = NULL;
etail = ehead;
}
//if newEvent is preceded by other events/event queue not empty
else{
cout << "check event order" << endl;
//insert new arrival into Event queue
do{
cout << "eve->time: " << eve->time << endl;
cout << "ecursor->time: " << ecursor->time << endl;
//new arrival earlier than event cursor points to
if(ecursor->time >= eve->time){
//insert at head of Event queue
if(ecursor == ehead){
cout << "insert at 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 at 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 to 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 now
proc->start = simclock;
//generate service time for process
proc->serTime = genexp(1/Ts);
//if no processes to service, service arrival instantly
//add departure event
if(!server_busy){
phead = proc;
schedule_event_DEP(proc);
server_busy = true;
//set CPU to proc
inCPU = proc;
}
//behavior for process queueing
else{
//Non Premptive
if(sType == 1 || sType == 3)
//process to ready queue
phead->next = proc;
phead = proc;
}
}
//remove arrival from 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
do
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 from Event queue/process from 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;
}