用Java实现循环调度算法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26563522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:49:03  来源:igfitidea点击:

Implementing round robin scheduling algorithm in Java

javaalgorithmschedulinground-robin

提问by charen

After hours of braining I've finally crashed and have come to result that I have no clue how to implement round robin into java. I've tried different approaches and the closest I've got.. well i explain with an example..

经过几个小时的思考,我终于崩溃了,结果是我不知道如何将轮循机制实现到java中。我尝试了不同的方法,最接近我的方法..我用一个例子来解释..

AT = Arrival Time BT = Burst Time (Execution Time)

AT = 到达时间 BT = 突发时间(执行时间)

At first i have this row of numbers (0,5;6,9;6,5;15,10)where elements in position 0-2-4represent arrival times and elements in position 1-3-5represent burst times. My code is so far that this input is turned into a class, called Process which comes with a constructor: Process(String name, int AT, int BT). I've separated processes with in the ArrayList. So now i have an 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),其中位置元素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..

I created a method which will return me a list of processes which have been cut with a quantum of time - For example in case of (0,5;6,9;6,5;15,10)i will get a result: [P0,P0,P1,P1,P1,P2,P2,P3,P3,P3,P3]

我创建了一个方法,该方法将返回一个经过一定时间切割的进程列表 - 例如,如果(0,5;6,9;6,5;15,10)我会得到一个结果:[P0,P0,P1,P1,P1,P2,P2,P3,P3,P3,P3]

So round robin method is a method where every process gets quantum time for execution which I've chosen 3.

所以循环法是一种方法,其中每个进程都获得执行的量子时间,我选择了 3。

  1. P0 with AT 0 & BT 3 comes in - added to the final list (time passed = 3)
  2. P0 with AT 0 & BT 2 comes in - added to the final list (time passed = 5)
  3. P0 finished
  4. P1 with AT 6 & BT 3 comes in - added to the final list (time passed = 9)
  5. next P1 is added to the queue
  6. P2 with AT 6 & BT 3 comes in - added to the final list (time passed = 12)
  7. next P2 is added to the queue
  8. P1 with AT 6 & BT 3 comes in from queue - added to the final list (time passed = 15)
  9. next P1 goes to the queue
  10. P3 arrives, added to the final list (time passed = 18)
  11. P1 comes from the queue - added to the final list
  1. 带有 AT 0 和 BT 3 的 P0 进来 - 添加到最终列表中(时间过去 = 3)
  2. 带有 AT 0 和 BT 2 的 P0 进来 - 添加到最终列表中(时间过去 = 5)
  3. P0 完成
  4. 带有 AT 6 和 BT 3 的 P1 进入 - 添加到最终列表中(时间过去 = 9)
  5. 下一个 P1 被添加到队列中
  6. 带有 AT 6 和 BT 3 的 P2 出现 - 添加到最终列表中(时间过去 = 12)
  7. 下一个 P2 被添加到队列中
  8. 带有 AT 6 和 BT 3 的 P1 来自队列 - 添加到最终列表中(时间过去 = 15)
  9. 下一个 P1 进入队列
  10. P3 到达,添加到最终列表(时间过去 = 18)
  11. P1 来自队列 - 添加到最终列表

And that's the point where I feel like my mind has crashed and i have no idea how to queue them.

这就是我觉得我的大脑崩溃了,我不知道如何将它们排队的地方。

The result should look like: [P0,P0,P1,P2,P1,P3,P2,P1,P3,P3,P3]

结果应如下所示: [P0,P0,P1,P2,P1,P3,P2,P1,P3,P3,P3]

EDIT: what I coded based on the first answer given. Still doesn't work..

编辑:我根据给出的第一个答案编码的内容。还是不行。。

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() < time){ //if next arrival time is lower than time passed
            queue.add(pstlst.get(i+1));
        }

        if (pworkTime-kvant > 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;
}

回答by kraskevich

You can maintain a queue of waiting processes and use the following algorithm:

您可以维护等待进程的队列并使用以下算法:

  1. Pick the first process in the queue(if it is not empty). Add it to the output list.

  2. Execute it for a given quantum of time(or less if its remaining time is less then one quantum) and subtract this quantum from the remaining time of this process.

  3. If new processes have arrived, add them to the end of the queue.

  4. If the last executed process is not finished(that is, its remaining time is not 0), add it to the end of the waiting queue.

  5. Go to step 1 if there are any processes left.

  1. 选择队列中的第一个进程(如果它不为空)。将其添加到输出列表中。

  2. 在给定的时间量程内执行它(如果剩余时间少于一个量程,则更短)并从该过程的剩余时间中减去该量程。

  3. 如果有新进程到达,则将它们添加到队列的末尾。

  4. 如果上次执行的进程没有完成(即剩余时间不为0),则将其添加到等待队列的末尾。

  5. 如果还有剩余进程,请转到步骤 1。

回答by Mohammad Abu Jawad

Example Code

示例代码

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<n;i++)
{
System.out.println("Enter BT for process "+(i+1));
BT[i]=Integer.parseInt(br.readLine());
}
System.out.println("***********************************************");
for(int i=0;i<n;i++)
{
System.out.println("Enter AT for process"+i);
AT[i]=Integer.parseInt(br.readLine());
}
System.out.println("***********************************************");
WT[0]=0;
for(int i=1;i<n;i++)
{
WT[i]=WT[i-1]+BT[i-1];
WT[i]=WT[i]-AT[i];
}  
for(int i=0;i<n;i++)
{
TAT[i]=WT[i]+BT[i];
AWT=AWT+WT[i];
}
System.out.println("  PROCESS   BT      WT      TAT     ");
for(int i=0;i<n;i++)
{System.out.println("    "+ i + "       "+BT[i]+"       "+WT[i]+"       "+TAT[i]);}
AWT=AWT/n;
System.out.println("***********************************************");
System.out.println("Avg waiting time="+AWT+"\n***********************************************");

}
}

回答by Mohammad Abu Jawad

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<n;i++)
        {
            System.out.println("Enter BT for process "+(i+1));
        BT[i]=Integer.parseInt(br.readLine());
        }
        System.out.println("______________________________");
    for(int i=0;i<n;i++)
    {
        System.out.println("Enter AT for process"+i);
    AT[i]=Integer.parseInt(br.readLine());
    }
        System.out.println("______________________________");
    WT[0]=0;

    for(int i=1;i<n;i++)
    {
        WT[i]=WT[i-1]+BT[i-1];
        WT[i]=WT[i]-AT[i];
    }

    for(int i=0;i<n;i++)
    {
        TAT[i]=WT[i]+BT[i];
        AWT=AWT+WT[i];
    }
        System.out.println("  PROCESS   BT      WT      TAT     ");

    for(int i=0;i<n;i++)
    {
        System.out.println("    "+ i + "       "+BT[i]+"       "+WT[i]+"       "+TAT[i]);}
        AWT=AWT/n;
        System.out.println("___________________________________________");
        System.out.println("Average WT="+AWT+"\n___________________________________________");
    }
 }