[英]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)?
[英]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++;
}
}
}
当我尝试运行程序时,我没有得到 output 并且也没有错误。 我怀疑 else 语句中存在问题,但无法指出。 我已经确定没有系统编译器问题。 我正在研究 Ubuntu 20.04 LTS。
错误似乎在这里:
&& is_completed[i] == 1
由于is_completed[i]
被初始化为零,这总是错误的。 因此,您永远不会进入处理数据的代码,因此temp
将保持在-1
。 换句话说 - 您将继续增加系统时间, while
将是一个无限循环,因为(total == x)
将一直为假。
尝试:
&& is_completed[i] == 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.