简体   繁体   English

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

[英]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.当我尝试运行程序时,我没有得到 output 并且也没有错误。 I suspect a problem in the else statement but can't point it out.我怀疑 else 语句中存在问题,但无法指出。 I have already made sure there were no system compiler issues.我已经确定没有系统编译器问题。 I'm working on Ubuntu 20.04 LTS.我正在研究 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.由于is_completed[i]被初始化为零,这总是错误的。 Consequently you'll never get into the code that handles the data so temp will stay at -1 .因此,您永远不会进入处理数据的代码,因此temp将保持在-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.换句话说 - 您将继续增加系统时间, while将是一个无限循环,因为(total == x)将一直为假。

Try:尝试:

&& is_completed[i] == 0

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

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