java - 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]);
}
}
}
解决方案
只需添加这个代替
// 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 在任何进程添加到队列之前都不会更新。
推荐阅读
- c# - 将 Excel 文件上传到 GridView 时出错
- c# - 在 SSIS 中处理具有多种格式和不同记录集的平面文件
- android - 为什么 BufferedReader 当它什么也没找到时给我异常
- python - 如何获取在熊猫函数中创建的动态数据框
- php - 在邮件服务器 PHPMailer 中请求 SMTP 身份验证
- python - 如何删除对象以增加python中的可用内存?
- java - 使用 Batik (Java) 对 CMYK TIFF 文件进行转码
- elastic-ip - 将弹性 IP 与计划的 AWS 数据管道关联
- ios - 在没有构建应用程序的情况下运行 UITests
- java - 如何在没有子报表的情况下使用多个 ResultSet 填充报表?