/ / Najkrótszy pozostały czas Algorytm - java, tablice, algorytm, proces, zaplanowane zadania

Najkrótszy pozostały czas Algorytm - java, tablice, algorytm, proces, zaplanowane zadania

Mam tę tablicę uporządkowaną według czasu serii w zależności od czasu przybycia:

Process   Burst  Arrival
1           10       0
2           5       1
3           2       2

To, co próbuję tutaj osiągnąć, to mieć tablicę ze wszystkimi uporządkowanymi tablicami w następującej kolejności:

Array Shortest remaining time algorithm

Process   Burst
1         1
2         1
3         2
2         4
1         9

Pozostał najkrótszy czas Najkrótszy pozostały czas planowania

Czas przybycia P2 P1 potrzebuje 9 milisekund więcejskończyć. Ponieważ jednostka centralna B pęka w 5 milisekundach <9 milisekund, dlatego wykonanie P1 zostanie wstrzymane, a P2 zostanie wykonane, ale w miarę zbliżania się P3 wykonanie P2 wymaga 3 kolejnych milisekund, a ponieważ P3 potrzebuje tylko 2 milisekund do wykonania, więc P3 przejmuje P2 i wkrótce.

To jest mój kod dla fcfs:

    int[] startTime = new int[myArr.length];
int[] CompletionTime = new int[myArr.length];
int[] wait = new int[myArr.length];
int[] turnaround = new int[myArr.length];
int[] idl = new int[myArr.length];


for(int v=0; v<Metd.length; v++){

if(Metd[v].contains("FCFS")){





System.out.println("--------------------------------------First Come First Served--------------------------------------");
System.out.println();

Arrays.sort(myArr, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[2], o2[2]);
}
});

System.out.println("____Process_____");
System.out.println("P   "+"B  "+"A  ");
for(int t=0; t<myArr.length; t++){


System.out.println(myArr[t][0]+"  "+myArr[t][1]+"  "+myArr[t][2]+"  ");
}

//System.out.println("After "+Arrays.deepToString(myArr));

int y=0;
int num1=0;

int diff=0;
for(int j=0; j<myArr.length; j++){

y=myArr[j][1];
num1=myArr[j][0];



if(j>0 && myArr[j-1][1]< myArr[j][2] && counter<myArr[j][2]){
//startTime[j+1]=y;
diff= myArr[j][2]- counter;
//System.out.println("Differencia :"+diff);
idl[j]=diff;

Thread r= new Thread(new Idle("System Idle for: "+diff, diff));
r.start();
counter=counter+diff;
//System.out.println("counter :"+counter);
}

startTime[j]=counter;
Thread t = new Thread(new Process("Process "+num1, y));
t.start();
counter=counter+y;
CompletionTime[j]=counter;

try {

t.join(); // Wait for it to finish.


} catch (InterruptedException e) {

e.printStackTrace();
}

}





for(int k=0; k<startTime.length; k++){
int waitdiff=startTime[k]-myArr[k][2];  //Wait = Start Time – Arrival Time
wait[k]=waitdiff;

int turndiff=CompletionTime[k]-myArr[k][2];
turnaround[k]=turndiff;
}


double avewait=0;
double aveTurn=0;
double sumidl=0;

System.out.println("________________________________________Performance Metrics_______________________________________________");
System.out.println("Process         Start           Completion      Wait            Turnaround");

for(int q=0; q<startTime.length; q++){

System.out.println("P"+myArr[q][0]+"            "+startTime[q]+"            "+CompletionTime[q]+"           "+wait[q]+"         "+turnaround[q]+"");


avewait=avewait+wait[q];
aveTurn=aveTurn+turnaround[q];
sumidl=sumidl+idl[q];

}
double value= counter/(counter+sumidl);
System.out.println();
System.out.println("Average Wait Time : "+avewait/myArr.length);
System.out.println("Average Turnaround Time : "+aveTurn/myArr.length);
System.out.println("CPU Utilization : "+df.format(value)+"%");

System.out.println("-----------------------------------------------------------------------------------------------");

Arrays.fill( startTime, 0 );
Arrays.fill( CompletionTime, 0 );
Arrays.fill( wait, 0 );
Arrays.fill( turnaround, 0 );
counter=0;

Odpowiedzi:

0 dla odpowiedzi № 1

ten algorytm jest również znany jako SRTF i możesz pobrać kod źródłowy z poniższego linku: http://www.dreamincode.net/forums/topic/241938-shortest-remaining-time-first/