我正在编写优先级调度抽象代码,其中我收到了进程数以及它们的运行时和优先级。具有最高优先级的进程首先运行,然后运行下一个进程。运行时加起来像runtime1 + (runtime1 + runtime2) + ...
。
当我执行它时,它运行时没有错误或警告,但它只接收我要求的值,并且不返回任何内容。我的代码出了什么问题?
#include <stdio.h>
#include <stdlib.h>
typedef struct process {
int priority;
int runtime;
} Process;
int main() {
int numberOfProcess;
int i;
printf("enter process number\n");
scanf("%d",&numberOfProcess);
Process proc[numberOfProcess];
for (i = 0; i < numberOfProcess; i++) {
printf("Enter the process runtime\n");
scanf("%d", &proc[i].runtime);
printf("Enter the priority\n");
scanf("%d", &proc[i].priority);
}
int done = 1;
int index;
int completedtimes[10];
int usage;
while (done > 0) {
done = -1;
int maxPriority = 0;
for (index = 0; index < numberOfProcess; index++) {
if (proc[index].priority > proc[maxPriority].priority)
maxPriority = index;
proc[maxPriority].priority = -1;
usage += proc[maxPriority].runtime;
completedtimes[maxPriority] = usage;
for (index = 0; index < numberOfProcess; i++)
if (proc[index].priority != -1)
done = -1;
for (index = 0; index < numberOfProcess; index++)
printf("process %d completed at %d sec", index, completedtimes[index]);
}
}
return 0;
}
答案 0 :(得分:1)
在循环级别2的第一个for循环中有一个无限循环,查看计数器索引和 i
while(done>0) {
done=-1;
int maxPriority=0;
for(index=0;index<numberOfProcess;index++) {
if(proc[index].priority>proc[maxPriority].priority)
maxPriority=index;
proc[maxPriority].priority=-1;
usage+=proc[maxPriority].runtime;
completedtimes[maxPriority]=usage;
for(index=0;index<numberOfProcess;i++)
if(proc[index].priority!=-1)
done=-1;
for(index=0;index<numberOfProcess;index++)
printf("process %d completed at %d sec", index, completedtimes[index]);
}
}
顺便说一下。由于您在不同的循环级别
中使用和重置索引,因此无法实现