首页 > 解决方案 > java中的程序意外终止

问题描述

在这个内部主要方法中,当我将到达时间 [0] 输入为 0 时,它可以正常工作,但是当我输入的输入值超过 0 时,它就会毫无例外地失败。

Thsi 程序正在实现 RR 调度,我在其中创建了一个方法 private static void findWaitingTime(int n, int[] bTime, int[]arrivalTime ,intquantum, int[] wTime)

此函数将获取进程数、突发时间、到达时间以及分配给每个一般进程的量子槽。

    package Others;

    import java.io.BufferedReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.io.InputStreamReader;
    import java.io.IOException;

    /**
     * RR scheduling implememtation with the process queue as a list
     *
     * @author Badal
     * @data 2 june 2020
     */



    public class RR_Scheduling {

        /**
this function will take number of processes , burst time, arrival time, and the quantum slot alotted to each generall processs. */

        private static void findWaitingTime(int n, int[] bTime, int[] arrivalTime ,int quantum, int[] wTime){
            // array to store remaing time of respective process
            int[] rem_bt = new int[n];

            int cpu_time = 0;

            // initially it will be equal to the burst time of all processes respectively
            for( int i = 0; i < n; i++ ) rem_bt[i] = bTime[i];



            // get the processes queue
            List<Integer> pQueue = new ArrayList<Integer>();

            // get the processes by thier arrival time
            for( int i = 0; i < n; i++ ){
                if( arrivalTime[i] == cpu_time){
                    pQueue.add(i);
                }
            }



            // executed process
            int executedProcess = 0;
            while(executedProcess != n){

                // process all pQueue processes
                for( int p = 0; p < pQueue.size(); p++){

                    // if not done 
                    if( rem_bt[ pQueue.get(p) ] > 0 ){

                        // partail execution
                        if( rem_bt[pQueue.get(p)] > quantum){

                            // collect all the processes that arrive under the execution 
                            for( int i = 0; i < n; i++){
                                if(  arrivalTime[i] > cpu_time && arrivalTime[i] < cpu_time + quantum ){
                                    // add to pQueue
                                    pQueue.add(i);
                                }
                            }

                            // update cpu_time
                            cpu_time += quantum;

                            // update rem_bt time
                            rem_bt[pQueue.get(p)] -= quantum;
                        }
                        else{



                            // collect all the processes that arrive under the execution 
                            for( int i = 0; i < n; i++){
                                if(  arrivalTime[i] > cpu_time && arrivalTime[i] < cpu_time + rem_bt[pQueue.get(p)]){
                                    // add to pQueue
                                    pQueue.add(i);
                                }
                            }

                            // udpate cpu_time
                            cpu_time += rem_bt[pQueue.get(p)];

                            // completely executed
                            rem_bt[pQueue.get(p)] = 0;

                            // fetch completion time and waiting time
                            int completionTime = cpu_time - arrivalTime[pQueue.get(p)];
                            wTime[pQueue.get(p)] = completionTime - bTime[pQueue.get(p)];

                            //update execute process
                            executedProcess++;

                        }
                    }



                    // get the processes by thier arrival time
                    for( int i = 0; i < n; i++ ){
                        if( arrivalTime[i] == cpu_time){
                            pQueue.add(i);
                        }
                    }
                }

            }
        }


        // take number of processes via command line
        public static void main(String[] args) throws IOException{
            BufferedReader br = new BufferedReader( new InputStreamReader( System.in ));

            int processes = Integer.parseInt(br.readLine());
            int[] arrivalTime = new int[processes];
            int[] burstTime = new int[processes];
            for( int i = 0; i < processes; i++ ){
                arrivalTime[i] = Integer.parseInt(br.readLine());
                burstTime[i] = Integer.parseInt(br.readLine());
            }

            int[] waitingTime = new int[processes];
            int quantum = Integer.parseInt(br.readLine());

            findWaitingTime(processes, burstTime, arrivalTime, quantum, waitingTime);// ??? if arrivalTime[0] >= 1 then it terminating the program.. but working if arrivalTime[0] is 0

            for( int i = 0; i < processes; i++){
                System.out.println("Waiting time : " + waitingTime[i]);
            }


        }

    }

标签: java

解决方案


只需添加这个代替

    // get the processes by thier arrival time
            for( int i = 0; i < n; i++ ){
                if( arrivalTime[i] == cpu_time){
                    pQueue.add(i);
                }
            }

在第一次只与

// get the processes by thier arrival time
        while( pQueue.isEmpty() ){
            for( int i = 0; i < n; i++){
                if( arrivalTime[i] == cpu_time){
                    pQueue.add(i);
                }
            }

            if( pQueue.isEmpty()){
                cpu_time++;
            }
        }

好像在开始时 cpu_time 是 0 但它们不是到达时间为 0 的进程,甚至其他 cpu_time 在任何进程添加到队列之前都不会更新。


推荐阅读