简体   繁体   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)?

Below is the two relevant functions for my question, but please ask if you require any of the other functions to help me solve this problem.以下是我的问题的两个相关功能,但请询问您是否需要任何其他功能来帮助我解决这个问题。 This program simulates a single server queue with exponential interarrival time, exponential service time, and FCFS scheduling discipline.该程序模拟具有指数到达时间、指数服务时间和 FCFS 调度规则的单个服务器队列。 This is an assignment and I am meant to modify it to use SJF scheduling by sorting the queue list by CPU_time.这是一项任务,我打算通过按 CPU_time 对队列列表进行排序来修改它以使用 SJF 调度。 Here is my attempt at a bubble sort, but when it is run it is not making it to the do-while loop.这是我对冒泡排序的尝试,但是当它运行时,它并没有进入 do-while 循环。 What is the best way to sort the linked list as they are being added to it?在将链表添加到链表时对其进行排序的最佳方法是什么?

/*********************************************************************/
/* 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;
}

That's really funny, I JUST did an assignment similar to this.这真的很有趣,我刚刚做了一个类似的作业。 Don't add the new nodes to the end of the queue.不要将新节点添加到队列的末尾。 Just add them where they need to be as you push them.只需在推送它们时将它们添加到需要的位置。 Run through the list until you find a node that has a CPU time that's smaller than (or bigger than, depending on how you want it) the one you're pushing and insert it there.遍历列表,直到找到一个 CPU 时间小于(或大于,取决于您想要的方式)您推送的节点并将其插入到那里的节点。

This means you'll probably have to check node->next->next->cpuTime since you'll want to push it between two nodes.这意味着您可能必须检查 node->next->next->cpuTime 因为您希望在两个节点之间推送它。

I hope I understood the question right.我希望我正确理解了这个问题。 Good luck.祝你好运。

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

相关问题 如何解决 C 中的 CPU 调度模拟程序中的分段错误(核心转储)错误? - How do I solve Segmentation Fault (Core Dump) error in my CPU Scheduling Simulation program in C? 如何使用 pthread 库模拟 CPU 调度(多级队列)? - How to simulate CPU scheduling(multilevel queue) using pthread library? C 程序计算抢占式 SJF 调度的所有时间不起作用 - C program to calculate all the times of a preemptive SJF scheduling not working 如何在冒泡排序算法中交换链表的节点? - How do I swap the nodes of a linked list in a bubble-sort algorithm? 如何对链接列表进行冒泡排序? - How do I Bubble Sort a linked list? 在C程序中将链表中的节点随机化 - Randomizing nodes in linked list, C program 如何以百分比百分比获取 C 程序中的 CPU 使用率 - How to get CPU usage in C program with percentage % 如何并行化多CPU的c程序 - how to parallelize the c-program for multi CPU 如何在C ++(Linux)中获得CPU时钟速度? - How do I get CPU Clock Speed in C++ (Linux)? 为什么我的程序没有为 C 中的循环(量子 = 4)调度模拟程序准确计算平均响应时间? - Why is my program not calculating Mean Response Time accurately for a Round Robin (Quantum = 4) Scheduling Simulation Program in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM