【发布时间】:2015-01-30 21:58:05
【问题描述】:
观看这个:https://www.youtube.com/watch?v=GjrxO-PDPdk YouTube 上的视频启发了我在 C 中实现循环算法:
#include <stdio.h>
#include <stdlib.h>
int check_if_done(int processes[], int n)
{
int i;
for(i=0; i<n; i++)
if(processes[i] > 0)
return 0;
return 1;
}
int main()
{
int processes[5];
int waiting_times[5];
processes[0] = 6;
processes[1] = 5;
processes[2] = 2;
processes[3] = 3;
processes[4] = 7;
int tq = 2;
int i = 0, j, n = 5;
for(j=0; j<n; j++)
waiting_times[j] = 0;
while(1)
{
if(check_if_done(processes, n))
break;
if(processes[i] > 0)
{
printf("P%d = %d\n", i+1, processes[i]);
waiting_times[i] += processes[i];
processes[i] -= tq;
}
i ++;
if(i == n)
{
printf("\n");
i = 0;
}
}
printf("\n");
for(i=0; i<n; i++)
{
printf("P%d waiting time = %d\n", (i+1), waiting_times[i]);
}
return 0;
}
这是我的第一个调度方法,它似乎可以按需工作(是吗?)。但是,我在计算平均等待时间时遇到了一些麻烦。我得到了:
P1 waiting time = 12
P2 waiting time = 9
P3 waiting time = 2
P4 waiting time = 4
P5 waiting time = 16
代替:
P1 waiting time = 13
P2 waiting time = 15
P3 waiting time = 4
P4 waiting time = 12
P5 waiting time = 16
【问题讨论】:
-
第二组数字从何而来?你似乎认为这是你应该得到的输出,但你没有解释原因。
-
@blh83:这些值是根据我在 SO 上发布的 YT 视频计算得出的。他们正在等待每个流程的时间,我知道如何计算它们,但在实施这些计算时遇到了困难。
标签: c scheduling