简体   繁体   中英

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. This is an assignment and I am meant to modify it to use SJF scheduling by sorting the queue list by CPU_time. Here is my attempt at a bubble sort, but when it is run it is not making it to the do-while loop. 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.

This means you'll probably have to check node->next->next->cpuTime since you'll want to push it between two nodes.

I hope I understood the question right. Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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