简体   繁体   中英

C program to calculate all the times of a preemptive SJF scheduling not working

#include <stdio.h>
#include <string.h>
#define MAX 1000
int main()
{
  int at[] = {7, 5, 3, 3, 4};
  int bt[] = {8, 1, 1, 1, 6};
  int x = sizeof(arrival_time) / sizeof(arrival_time[0]);
  int min[x];
  int pid[x];
  int ct[x];
  int tat[x];
  int wt[x];
  int flag[x];
  int st = 0, total = 0;
  float avg_wt = 0, avg_turnaround_time = 0;
  for (int i = 0; i < x; i++)
  {
    pid[i] = i + 1;
    flag[i] = 0;
    min[i] = bt[i];
  }
    while(1)
  {
    int temp = -1, minBurst = MAX;

    if(total == x)
    break;

    for (int i = 0; i < x; i++)
    {
        if ( (st >= at[i]) &&
         (bt[i] < minBurst) && flag[i] == 1 )
          {
            minBurst = bt[i];
            temp = i;
          }
    }
   
    if ( temp == -1 )
      st++;

    else
    {

      bt[temp]--;
      st++;

      if (bt[temp] == 0)
      {
        ct[temp] = st;
       flag[temp] = 1;
        total++;
      }
    }
  }

I get no output when I try to run the program and no errors as well. I suspect a problem in the else statement but can't point it out. I have already made sure there were no system compiler issues. I'm working on Ubuntu 20.04 LTS.

The bug seems to be here:

&& is_completed[i] == 1

Since is_completed[i] is initialized to zero this is always false. Consequently you'll never get into the code that handles the data so temp will stay at -1 . In other words - you'll just keep increasing the system time and the while will be an endless loop because (total == x) will be false all the time.

Try:

&& is_completed[i] == 0

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