我想你们中的大多数人都知道该算法(SJF)应该做什么,我被告知编写一个代码,这就是我所得到的. 我决定使用包含时间和ID的结构进程".
I think most of you know what this algorithm (SJF) should do, I was told to code one and this is what I got. I decided to use a struct "process" which contains the times and id.
我认为按到达时间排序的效果很好,但有时在printf上,我得到2倍相同的字母(过程),而我得到的Timearound和Waiting时间不正确,而且我实际上不是因为我认为我使用的公式是正确的,所以知道如何解决该问题.任何帮助都很好.
I think that the sort by arrival time is working just fine but sometimes, on the printf, I get 2 times the same letter (process) and the Timearound and Waiting times that I get are not right and I don't really know how to fix that since I think that the formulas I used are right. Any help would be nice.
无论是"rafaga"是什么意思,"burst time"
Wherever it says "rafaga" that means "burst time"
推荐答案在 SJF 算法中,首先,您根据到达时间 以下进行排序通过根据爆发时间进行排序.
In SJF algorithm,first you sort on the basis of arrival times followed by sorting on the basis of burst time.
现在查看您的排序代码,似乎您正在使用 选择排序 ,并且您的代码不正确.内循环总是比外索引多一个索引,因此j = i + 1,而不是j = 0正确的排序是:
Now looking at your sorting code, it seems you are using selection sort, and your code is not correct. The inner loop always begins from one index more than outer index therefore j = i + 1, instead of j = 0 correct sorting is:
for (i = 0;i < n;i++){ for(j = i+1;j < n;j++) { if(p[i].tL < p[j].tL){ aux=p[j]; p[j]=p[i]; p[i]=aux; } } }您对burst time的排序也似乎不正确.照顾好这些小事情,您的代码将是正确的.附带说明,一旦您了解了基础知识, SJF 是最简单的代码.
Also your sort for burst time looks incorrect. Take care of these little things and your code will be correct. On a side note SJF is the simplest to code once you understand the basics.
更多推荐
到达时间不正确的SJF算法
发布评论