首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >队列排序函数排序错误的队列

队列排序函数排序错误的队列
EN

Stack Overflow用户
提问于 2015-04-10 01:38:46
回答 1查看 44关注 0票数 0

下面是我的队列排序函数,其中调度列表、实时和作业队列都被定义为队列结构。

代码语言:javascript
运行
复制
struct Queue {
  pcbptr front;
  pcbptr back;
};
typedef struct Queue queue;

而pcbptr定义为

代码语言:javascript
运行
复制
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;

现在实际的功能

代码语言:javascript
运行
复制
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;
  }
}

下面是我对队列的实现

代码语言:javascript
运行
复制
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应移至实时。

当我运行程序时,将打印适当的打印行,因此这是输出,这是预期的:

代码语言:javascript
运行
复制
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",但它似乎没有延迟打印,可能是什么原因呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-10 04:08:49

问题在于如何将元素从一个队列移动到另一个队列。在队列中插入mypcb,但不考虑与其链接的其他元素。所以你有原始名单

代码语言:javascript
运行
复制
job:      1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
realtime: 

然后,您希望在队列之间移动第五个元素,但不更改节点之间的链接,因此您得到了如下所示

代码语言:javascript
运行
复制
job:      1 -> 2 -> 3 -> 4 -> 6 -> 7
realtime: 5 -> 6 -> 7

要解决这个问题,您需要更改队列中的链接,我想这是可行的。

代码语言:javascript
运行
复制
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 );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29551943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档