编程调度程序。无法让多个队列工作

时间:2014-04-18 02:05:03

标签: c list queue scheduler

我一直在为这个调度程序项目工作一点。我目前遇到过一些我无法找到问题的事情。我可能会错过它,或者只是缺乏睡眠。我认为它发生在名为### MANAGing WAITING ###的代码块中。当我到达时间大约23时,它只是星星打印,事情已经完成了他们的IO爆发,而实际上在等待列表中应该只有1或2个进程。有人如何填补等候名单。我无法弄清楚为什么会这样。任何帮助将不胜感激。

    //Scheduler Project


typedef struct PCB{
  int pID;
  int arrivalTime;
  int totalBurst;
  int cpuBursts[10];
  int ioBursts[10];
  int waited;
  int currentCPUBurst;
  int currentIOBurst;
  int burstsRemaining;
  struct PCB* next;
  struct PCB* prev;
}PCB;


//#####################Creating Queues#####################
//Init queue head and tail
PCB* iHead = NULL;
PCB* iTail = NULL;

//Ready queue head and tail
PCB* rHead = NULL;
PCB* rTail = NULL;

//Waiting list head and tail
PCB* wHead = NULL;
PCB* wTail = NULL;

int Timer = 0;
PCB* running = NULL;

void calculateProcessStats();

//#####################Enqueues#####################
//Adds item to the back of init queue
void iEnqueue(PCB* item)
{
  if(iHead == NULL)    //Queue is empty.
    iHead = iTail = item;
  else{
    iTail->next = item;
    item->prev = iTail;
    iTail = item;
    iTail->next = NULL;
  }
}

//Adds item to the back of ready queue
void rEnqueue(PCB* item)
{
  if(rHead == NULL)    //Queue is empty.
    rHead = rTail = item;
  else{
    rTail->next = item;
    item->prev = rTail;
    rTail = item;
    rTail->next = NULL;
  }
  printf("Process %d has arrived\n", rTail->pID);
}

//Adds item to the back of waiting list
void wInsert(PCB* item)
{
  if(wHead == NULL)    //List is empty is empty.
    wHead = wTail = item;
  else{
    wTail->next = item;
    item->prev = wTail;
    wTail = item;
    wTail->next = NULL;
  }
}


//####################Dequeues##########################
//Removes item from front/head of the queue
PCB* iDequeue()
{
  PCB* temp = iHead;
  if(iHead == NULL)    //Queue is empty.
    return 0;
  else if(iHead==iTail && iHead != NULL){     //Single PCB in queue
    //free(iHead);
    iHead = NULL;
  }
  else{     //Two or more PCBs in queue
    iHead = iHead->next;
    //free(temp);
    iHead->prev = NULL;
  }
  return temp;
}

//Removes item from front/head of the queue
PCB* rDequeue()
{
  PCB* temp = rHead;
  if(rHead == NULL)    //Queue is empty.
    return 0;
  else if(rHead==rTail && rHead != NULL){     //Single PCB in queue
    //free(rHead);
    rHead = NULL;
  }
  else{     //Two or more PCBs in queue
    rHead = rHead->next;
    //free(temp);
    rHead->prev = NULL;
  }
  return temp;
}

//Removes node passed to functions. This is working on a queue
void wRemove(PCB* item)
{
  PCB* temp = wHead;
  while(temp != NULL)
    {
      if((temp->next == NULL) && (temp->pID == item->pID)){
        wHead = NULL;
        wTail = NULL;
      }
      else if(temp->pID == item->pID){
        temp->prev->next = temp->next;
        temp->next->prev = temp->prev;
      }
      temp = temp->next;
    }
}

//Prints init queue head to tail
void iPrintQueue()
{
  PCB* temp = iHead;
  while(temp != NULL){
    printf("pid=%d ", temp->pID);
    printf("N=%d ", temp->burstsRemaining);
    printf("Arrival Time=%d ", temp->arrivalTime);
    printf("cpuBursts:");
    for(int i = 0; i < 9; i++){
      if(temp->cpuBursts[i] == 0)
        break;
      printf("%d ", temp->cpuBursts[i]);
    }
    printf("\n                         ioBursts:");
    for(int i = 0; i < 9; i++){
      if(temp->ioBursts[i] == 0)
        break;
      printf("%d ", temp->ioBursts[i]);
    }
    printf("\n");
    temp = temp->next;
  }
}

//Prints ready queue head to tail
void rPrintQueue()
{
  PCB* temp = rHead;
  while(temp != NULL){
    printf("pid = %d ", temp->pID);
    printf("N=%d ", temp->burstsRemaining);
    printf("Arrival Time=%d ", temp->arrivalTime);
    printf("cpuBursts:");
    for(int i = 0; i < 9; i++){
      if(temp->cpuBursts[i] == 0)
        break;
      printf("%d ", temp->cpuBursts[i]);
    }
    printf("\n                         ioBursts:");
    for(int i = 0; i < 9; i++){
      if(temp->ioBursts[i] == 0)
        break;
      printf("%d ", temp->ioBursts[i]);
    }
    printf("\n");
    temp = temp->next;
  }
}



int main(int argc, char *argv[])
{

  //argv[0] = scheduler.cc
  //argv[1] = input file to read from
  //argv[2] = scheduling algorithm
  //argv[3] = quantum
  //argv[4] = cost of full context switch

  //This section opens files and write it to init queue (iHead, iTail) 
  FILE *file = fopen (argv[1], "r");
  if (file != NULL){     //Continue until entire file is empty
    char line [64];
    int pidCounter = 0;
    while(fgets(line, sizeof line, file) != NULL){     //Grabs first line and puts stores in line, loops
      char* token = strtok(line, " ");
      PCB* temp = malloc(sizeof(PCB));
      int tokenCounter = 1;
      int cpuBurstCounter = 0;
      int ioBurstCounter = 0;
      while(token){     //Loops on single line, each iteration is the next token
        if(tokenCounter == 1){
          temp->arrivalTime = atoi(token);
        }
        else if(tokenCounter == 2){
          temp->burstsRemaining = atoi(token);
        }
        else if(tokenCounter %2 == 1){     //Everything > 2 and odd (cpuBurst)
          temp->cpuBursts[cpuBurstCounter] = atoi(token);
          cpuBurstCounter++;
        }
        else {     //Everything > 2 and even (ioBurst)
          temp->ioBursts[ioBurstCounter] = atoi(token);
          ioBurstCounter++;
        }
        token = strtok(NULL, " ");     //Moves onto next token
        tokenCounter++;
      }
      temp->pID = pidCounter;
      pidCounter++;
      temp->currentIOBurst = 0;
      temp->currentCPUBurst = 0;
      temp->waited = 0;
      iEnqueue(temp);
    }
    fclose (file);
  }
  else{
    perror (argv[1]);
  }


  if((strcmp(argv[2], "FCFS") == 0) && (argc == 3)){     //First Come First Served NonPreemptive Algorithm
    while(1){
      Timer++;
      int timeLeft;
      printf("Time=%d\n", Timer);

      //##########MANAGE READY QUEUE##########
      if(iHead != NULL){
        while(iHead->arrivalTime == Timer){
          rEnqueue(iDequeue());
        }
      }

      //##########MANAGE RUNNING##########
      //Should only be entered during first iteration and after running is finished or sent to wait
      if((running == NULL) && (rHead != NULL)){
        running = rDequeue();
        timeLeft = running->cpuBursts[running->currentCPUBurst];
      }
      //Should enter this every time this is something running
      if(running != NULL){
        if(timeLeft == 0){
          if(running->burstsRemaining <= running->currentCPUBurst){
            calculateProcessStats();
            free(running);
          }
          wInsert(running);
          running->currentCPUBurst++;
          printf("Process %d is waiting\n", running->pID);
          running=NULL;
        }
        else{
          timeLeft--;
          printf("Process %d is running\n", running->pID);
        }      
      }

      //##########MANAGE WAITING QUEUE##########
      PCB* wTemp = wHead;
      while(wTemp != NULL){
        wTemp->waited++;
        printf("pid=%d > ", wTemp->pID);

        /*
          if(wTemp->waited == wTemp->ioBursts[wTemp->currentIOBurst]){
          printf("Process %d has completed IO and returned to Ready state", wTemp->pID);
          wRemove(wTemp);
          }
        */
        wTemp = wTemp->next;
      }



      //Remove this to have entire scheduler run
      getchar();
      printf("________________________________________________________________\n");
    }    


  }
  else if((strcmp(argv[2], "FCFS") == 0) && (argc == 5)){     //First Come First Server Preemptive Algorithm
    //argv[3] = quantum
    //argv[4] = cost of full context switch
    printf("This is the code for FCFS Preemptive");

    int quantum = atoi(argv[3]);
    int csCost = atoi(argv[4]);
    for(int i = 0; i < quantum; i++){

    }

  }
  else if(strcmp(argv[2], "SJNFP") == 0){     //Shortest Job Next Algorithm
    printf("This is the code for SJNFP");
    //create a sort function for ready queue to keep queue sorted by cpuBursts[currentCPUBurst]
    //dequeue from ready queue into running
  }
  else{
    printf("Error: The algorithm for %s does not exist\n", argv[2]);
    printf("Please use FCFS or SJNFP or check number of parameters for FCFS\n");
  }

  //When all processes finish
  //printf("Finished\nCPU Utilization: Avg/Max TAT=%f/%f, Avg/Max Wait Time=%f/%f, Avg/Max Response Time=%f/%f\n", avgTAT, maxTAT, avgWaitTime, maxWaitTime, avgResponseTime, maxResponseTime)

  return 0;
}


void calculateProcessStats(){
  int TAT = Timer - running->arrivalTime;
  int burstTime = 0;
  for(int i = 0; i < running->totalBurst; i++)
    burstTime += running->cpuBursts[i];
  int waitTime = TAT - burstTime;
  int ioTime = 0;
  for(int i = 0; i < running->totalBurst-1; i++)
    ioTime += running->ioBursts[i];
  printf("Process %d terminated: TAT=%d, Wait Time=%d, I/O Wait=%d\n", running->pID, TAT, waitTime, ioTime);
}

0 个答案:

没有答案