在Java中实现循环调度算法
经过数小时的脑筋训练后,我终于崩溃了,结果我不知道如何在java中实现循环法。 我尝试了不同的方法,而且我得到的最接近……我用一个例子来解释..
AT =到达时间BT =突发时间(执行时间)
首先,我有这一行数字(0,5;6,9;6,5;15,10)
,其中位置0-2-4
元素表示到达时间,位置1-3-5
元素表示突发时间。 我的代码到目前为止,这个输入变成了一个类,名为Process,它带有一个构造函数: Process(String name, int AT, int BT)
。 我在ArrayList
分离了进程。 所以现在我有一个ArrayList alst = [P0,P1,P2,P3] where P0 has AT 0 and BT 5 and so on
。
我创建了一个方法,它将返回一个已经被削减了一段时间的进程列表 – 例如在(0,5;6,9;6,5;15,10)
情况下,我将得到一个结果: [P0,P0,P1,P1,P1,P2,P2,P3,P3,P3,P3]
因此循环方法是一种方法,其中每个进程都获得了我选择的量子执行时间3。
- 带有AT 0和BT 3的P0进入 – 添加到最终列表(时间过去= 3)
- 带有AT 0和BT 2的P0进入 – 添加到最终列表(时间过去= 5)
- P0完了
- 带有AT 6和BT 3的P1进入 – 添加到最终列表(时间过去= 9)
- 下一个P1被添加到队列中
- 带有AT 6和BT 3的P2进入 – 添加到最终列表中(时间过去= 12)
- 下一个P2被添加到队列中
- 带有AT 6和BT 3的P1从队列进入 – 添加到最终列表(时间过去= 15)
- 下一个P1进入队列
- P3到达,添加到最终列表(时间过去= 18)
- P1来自队列 – 添加到最终列表中
这就是我觉得我的思绪崩溃而我不知道如何排队的地步。
结果应如下所示: [P0,P0,P1,P2,P1,P3,P2,P1,P3,P3,P3]
编辑:我根据给出的第一个答案编码。 仍然无法正常工作..
public ArrayList roundRobinJarjestus(ArrayList pstlst){ ArrayList queue = new ArrayList();// järjekord, alguses tühi ArrayList uuspst = new ArrayList(); // queue.add(pstlst.get(0)); int i = 0; double time = 0; double pworkTime = 0; int kvant = 3; while (i < pstlst.size()){ Protsess p = (Protsess) queue.get(i); //first process is taken pworkTime = p.getTooaeg(); //execute time time = time + pworkTime; if (((Protsess) pstlst.get(i+1)).getSaabumisaeg() 0){ //if worktime - quantum is higher than zero and still left something to execute p.setTooaeg(pworkTime-kvant); queue.add(p); } uuspst.add(queue.get(i)); i = i+1; } return uuspst; }
您可以维护等待进程的队列并使用以下算法:
-
选择队列中的第一个进程(如果它不为空)。 将其添加到输出列表中。
-
在给定的时间内执行它(如果剩余时间少于一个量子,则执行更少的时间),并从该过程的剩余时间中减去该量程。
-
如果新进程已到达,请将它们添加到队列的末尾。
-
如果最后执行的进程未完成(即,其剩余时间不为0),请将其添加到等待队列的末尾。
-
如果还有任何进程,请转到步骤1。
package cpuSch; import java.io.*; class fcfs { public static void main(String args[]) throws Exception { int n,AT[],BT[],WT[],TAT[]; float AWT=0; InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter no of process"); n=Integer.parseInt(br.readLine()); BT=new int[n]; WT=new int[n]; TAT=new int[n]; AT=new int[n]; System.out.println("Enter Burst time for each process\n______________________________"); for(int i=0;i
示例代码
import java.io.*; class fcfs { public static void main(String args[]) throws Exception { int n,AT[],BT[],WT[],TAT[]; float AWT=0; InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter no of process"); n=Integer.parseInt(br.readLine()); BT=new int[n]; WT=new int[n]; TAT=new int[n]; AT=new int[n]; System.out.println("Enter Burst time for each process\n******************************"); for(int i=0;i