到达时间不正确的SJF算法

编程入门 行业动态 更新时间:2024-10-11 13:25:13
本文介绍了到达时间不正确的SJF算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 typedef struct{ int tL; //Arrival time int rafaga,id,tE,tP; //burst,id,waitTime,TimeAround } process; int main(int argc, char** argv) { int n= 3; int i,j,rafagasum=0,k=1,rafagacomp; process p[n],aux; char id []= {'A','B','C','D','E','F'}; for(i=0;i<n;i++) { printf(" Process burst %d: ",i); scanf("%d",&p[i].rafaga); printf("Arrival time %d: ",i); scanf("%d",&p[i].tL); p[i].id=id[i]; } //Sort by arrival time for (i=0;i<n;i++){ for(j=0;j<n;j++){ if(p[i].tL<p[j].tL){ aux=p[j]; p[j]=p[i]; p[i]=aux; } } } for(j=0;j<n;j++){ rafagasum=rafagasum+p[j].rafaga; rafagacomp=p[k].rafaga; for(i=k;i<n;i++) { if(rafagasum>p[i].tL && p[i].rafaga<rafagacomp) aux=p[k]; p[k]=p[i]; p[i]=aux; } k++; } // We calculate waiting time p[0].tE=0; int sum=0; for(i=1;i<n;i++){ sum=sum+p[i-1].rafaga; p[i].tE=sum-p[i].tL; } //We calculate Timearound int sum1=0; for(i=0;i<n;i++){ sum1=sum1+p[i].rafaga; p[i].tP=sum1-p[i].tL; } printf(" ID Turnaround TWAit \n "); for(i=0;i<n;i++){ printf("%c \t %d \t %d \t \n", p[i].id,p[i].tP,p[i].tE); } return 0; }

我想你们中的大多数人都知道该算法(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算法

本文发布于:2023-11-27 11:23:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   算法   时间   SJF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!