[英]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.