下面是我的队列排序函数,其中调度列表、实时和作业队列都被定义为队列结构。
struct Queue {
pcbptr front;
pcbptr back;
};
typedef struct Queue queue;
而pcbptr定义为
struct PCB {
int pid;
int arrival_time;
int time_left;
int priority;
int status;
int printers;
int modems;
int scanners;
int cds;
int memory; //might need a struct for memory block?
struct PCB* next;
struct PCB* prev;
};
typedef struct PCB pcb;
typedef pcb * pcbptr;
现在实际的功能
void starthostd(){
int i=0;
while(dispatchlist.front != NULL){
if(dispatchlist.front->priority == 0){
enqueue(dispatchlist.front, &realtime);
dispatchlist.front = dispatchlist.front->next;
printf("the %d pcb was moved to realtime queue\n", i );
}
else{
enqueue(dispatchlist.front, &jobqueue);
dispatchlist.front = dispatchlist.front->next;
printf("the %d pcb was moved to job queue\n", i );
}
i++;
}
while(realtime.front != NULL){
printf("blah");
realtime.front = realtime.front->next;
}
}
下面是我对队列的实现
void enqueue( pcbptr mypcb, queue* queue) {
if (queue->front == NULL){ //empty
queue->front = mypcb;
}
else{
queue->back->next = mypcb;
}
queue->back = mypcb; //set this pcb to the end of the queue
}
基本上,我最初在dispatchlist中有7个pcbptr,前4个具有优先级1,第5个具有优先级0,最后2个具有优先级1。
因此,应该发生的是,多氯联苯1,2,3,4,6,7应移至作业队列,多氯联苯5应移至实时。
当我运行程序时,将打印适当的打印行,因此这是输出,这是预期的:
the 0 pcb was moved to job queue
the 1 pcb was moved to job queue
the 2 pcb was moved to job queue
the 3 pcb was moved to job queue
the 4 pcb was moved to realtime queue
the 5 pcb was moved to job queue
the 6 pcb was moved to job queue
(我知道上述陈述中的数字落后于1)
但是,预期的结果应该是只打印一次blah,因为实时队列中只有一个pcb。但是,多氯联苯的印刷次数为3次,5,6,7次。
在我看来,一旦一个pcb被移动到实时队列中,每个其他元素也被移动到实时队列中,尽管它不应该被移到实时队列中。
有人能发现我可能错过了什么吗?
谢谢
PS:我有一个附带的问题,我插入了一个usleep(5000)到同时循环,打印"blah",但它似乎没有延迟打印,可能是什么原因呢?
发布于 2015-04-10 04:08:49
问题在于如何将元素从一个队列移动到另一个队列。在队列中插入mypcb
,但不考虑与其链接的其他元素。所以你有原始名单
job: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
realtime:
然后,您希望在队列之间移动第五个元素,但不更改节点之间的链接,因此您得到了如下所示
job: 1 -> 2 -> 3 -> 4 -> 6 -> 7
realtime: 5 -> 6 -> 7
要解决这个问题,您需要更改队列中的链接,我想这是可行的。
if(dispatchlist.front->priority == 0){
pcbptr t = dispatchlist.front;
dispatchlist.front = dispatchlist.front->next;
t->next = NULL;
enqueue(t, &realtime);
printf("the %d pcb was moved to realtime queue\n", i );
}
https://stackoverflow.com/questions/29551943
复制相似问题