最短剩余时间算法

时间:2015-04-15 07:20:21

标签: java arrays algorithm process scheduled-tasks

我根据到达时间按照突发时间排序这个数组:

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

我试图在这里完成的是拥有一个包含所有有序数组的数组,如下所示:

Array Shortest remaining time algorithm

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

Shortest Time remaining Shortest Remaining Time Scheduling

P2到达P1的时间需要9毫秒才能完成。因为B的cpu在5毫秒内爆发<因此,P1的执行将被抢占,P2将被执行,但是当P3到达时,P2的执行需要3毫秒,而P3只需要2毫秒来执行,因此P3接管P2,依此类推。

这是我的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;

1 个答案:

答案 0 :(得分:0)

此算法也称为SRTF,您可以从以下链接下载源代码: http://www.dreamincode.net/forums/topic/241938-shortest-remaining-time-first/