简体   繁体   English

到达时间不正确的SJF算法

[英]SJF algorithm with arrival time not working properly

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;
   }

I think most of you know what this algorithm (SJF) should do, I was told to code one and this is what I got. 我想你们大多数人都知道该算法(SJF)应该做什么,我被告知编写一个代码,这就是我所得到的。 I decided to use a struct "process" which contains the times and id. 我决定使用包含时间和ID的结构“进程”。

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. 我认为按到达时间排序的效果很好,但有时在printf上,我得到相同字母(流程)的2倍,而我得到的“时间间隔”和“等待时间”不正确,我也不知道该怎么做修正此问题,因为我认为我使用的公式正确。 Any help would be nice. 你能帮忙的话,我会很高兴。

Wherever it says "rafaga" that means "burst time" 无论在什么地方说“ rafaga”,意思是“爆发时间”

In SJF algorithm,first you sort on the basis of arrival times followed by sorting on the basis of burst time . SJF算法中,首先根据到达时间 进行排序,然后根据突发时间进行排序。

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: 内部循环总是从一个索引开始,比外部索引更多,因此j = i + 1 ,而不是j = 0正确的排序是:

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;
     }

    }
}

Also your sort for burst time looks incorrect. 另外,您对burst time的排序看起来不正确。 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是最简单的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM