首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java中意外的程序终止

问java中意外的程序终止
EN

Stack Overflow用户
提问于 2020-06-02 19:01:35
回答 1查看 34关注 0票数 0

在这个内部的main方法中,当我输入arrivalTime为0时,它可以正常工作,但当我输入更多的输入值时,它就会异常地失败。

这个程序正在实现RR调度,其中我创建了一个方法私有静态无效findWaitingTime(int n,int[] bTime,int[] arrivalTime,int quantum,int[] wTime)

此函数将获取进程数、突发时间、到达时间和分配给每个通用进程的量子时隙。

代码语言:javascript
复制
    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]);
            }


        }

    }
EN

回答 1

Stack Overflow用户

发布于 2020-06-02 20:24:04

只需添加以下内容即可

代码语言:javascript
复制
    // get the processes by thier arrival time
            for( int i = 0; i < n; i++ ){
                if( arrivalTime[i] == cpu_time){
                    pQueue.add(i);
                }
            }

在第一次只有

代码语言:javascript
复制
// 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++;
            }
        }

就好像在begin中,cpu_time是0,但是他们的arrivalTime是0的没有进程,甚至其他的cpu_time也不会更新,直到任何进程添加到队列中。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62150516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档