繁体   English   中英

在 CPU 调度模拟 C 程序 (SJF) 中,如何按链表中的 CPU 时间对节点进行排序?

[英]How do I sort nodes by CPU time in linked list as they are put on the “queue” in CPU scheduling simulation C program (SJF)?

以下是我的问题的两个相关功能,但请询问您是否需要任何其他功能来帮助我解决这个问题。 该程序模拟具有指数到达时间、指数服务时间和 FCFS 调度规则的单个服务器队列。 这是一项任务,我打算通过按 CPU_time 对队列列表进行排序来修改它以使用 SJF 调度。 这是我对冒泡排序的尝试,但是当它运行时,它并没有进入 do-while 循环。 在将链表添加到链表时对其进行排序的最佳方法是什么?

/*********************************************************************/
/* Name: Puton_queue                                            */
/* Description                                                          */
/*    This procedure inserts a customer at the end of the given         */
/* queue. The parameters are as follows:                        */
/*     pqueue - pointer to the queue.                                   */
/*     pcust - index of the customer to be inserted.                    */
/* The procedure performs the following steps:                          */
/*     1 - get a free node for the customer.                    */
/*     2 - inset the node ar the end of the queue.              */
/*         2a - into an empty queue                             */
/*         2b - normal insertion                                */
/*********************************************************************/
void Puton_queue(struct Queue_struct *pqueue, struct Custs *pcust, struct Custs *CPU_time)
  {
  struct Queue *newnode;
  /* get an new node */

  printf(" My CPUTIME is %ld:\n", CPU_time);

  newnode = (struct Queue *) malloc(sizeof(struct Queue));
  /* now loc is the index of a free node in queue */
  /* put information in the node */
  newnode->cust_index = pcust;  
  newnode->CPU_time=CPU_time;
  newnode->next = NULL;
 /* check to see if the queue is initially empty */
  if(pqueue->q_last == NULL)
         {
         pqueue->q_head = newnode;
         pqueue->q_last = newnode;
         return;
         }
  /* otherwise add it to the end of the queue and relink */
  pqueue->q_last->next = newnode;
  pqueue->q_last = newnode;

  int i;
  bool swapped=TRUE;
  struct Queue *currentnode;
  struct Queue *lastnode = NULL;

  do
  {
      swapped = TRUE;
      currentnode = pqueue->q_head;
      printf("nodeIME is %ld:\n", currentnode->CPU_time);
      while(currentnode->next != lastnode)
      {
          if(currentnode->CPU_time < currentnode->next->CPU_time)
          {
              swap(currentnode, currentnode->next);
              swapped = FALSE;
          }
      }
      lastnode = currentnode;
  }
  while(swapped);

 return;
  }


/*********************************************************************/
/* Name: swap                                                          */
/* Description                                                          */
/*    This function is used to swap two nodes in the queue list       */                                                    
/*********************************************************************/
void swap(struct Queue *a, struct Queue *b)
{
    struct Queue *tem;

    tem = a->cust_index;
    a->cust_index = b->cust_index;
    b->cust_index = tem;

    tem = a->next;
    a->next = b->next;
    b->next = tem;
}

这真的很有趣,我刚刚做了一个类似的作业。 不要将新节点添加到队列的末尾。 只需在推送它们时将它们添加到需要的位置。 遍历列表,直到找到一个 CPU 时间小于(或大于,取决于您想要的方式)您推送的节点并将其插入到那里的节点。

这意味着您可能必须检查 node->next->next->cpuTime 因为您希望在两个节点之间推送它。

我希望我正确理解了这个问题。 祝你好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM