计算抢占式 SJF 调度不工作的所有时间的 C 程序

时间:2021-02-04 07:32:49

标签: c algorithm

#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++;
      }
    }
  }

当我尝试运行程序时没有输出,也没有错误。我怀疑 else 语句中存在问题,但无法指出。我已经确定没有系统编译器问题。我正在使用 Ubuntu 20.04 LTS。

1 个答案:

答案 0 :(得分:1)

错误似乎在这里:

&& is_completed[i] == 1

由于 is_completed[i] 被初始化为零,所以这总是假的。因此,您永远不会进入处理数据的代码,因此 temp 将停留在 -1。换句话说 - 你只会不断增加系统时间,while 将是一个无限循环,因为 (total == x) 将一直为假。

试试:

&& is_completed[i] == 0
相关问题