gpt4 book ai didi

java - 循环法 Java : Execution Process Not Occur Based on roundrobin algorithm

转载 作者:行者123 更新时间:2023-11-29 09:09:40 24 4
gpt4 key购买 nike

我在这里遇到了一个问题:当上一个进程执行时间完全结束时执行下一个进程。我需要这个进程基于循环算法执行。 谁能给我提示如何为双处理器编写代码

    import java.util.Scanner;
public class RoundRobin {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of processes");
int noProcess= keyboard.nextInt();
int[][] process = new int[noProcess][3]; //1st column: process name, 2nd column: arrival time, 3rd column: execution time
System.out.println("Input the Arrival and Execution Time for each process\n"); //in seconds, arrival for each process
for(int i = 0; i < process.length ;i++)
{
process[i][0] = i+1;
System.out.print("Process " +process[i][0]+ " :\t");
System.out.print("Arrival Time : ");
process[i][1] = keyboard.nextInt();
System.out.print("\t\tExec. Time : ");
process[i][2] = keyboard.nextInt();
}
//sorting the processes based on arrival time
for(int index = 0; index< process.length; index++ )
{
int indexOfNextSmallest = RoundRobin.getIndexOfSmallest(index, process);
interchange(index, indexOfNextSmallest, process);

}
System.out.println("ProName\tArrTime\tExecTime"); //table after scheduled.
for(int row = 0; row < noProcess; row++)
{
for(int column = 0; column < 3; column++)
{
System.out.print("" +process[row][column]+ "\t");
}
System.out.println("\n");
}
System.out.print("Enter the Quantum time:"); //in seconds
int qTime = keyboard.nextInt();
// boolean counter = true;//false if all process is completely execute
/**while(counter)
{


}**/
int current =0;
int counter = qTime;
int arrTime = 0;
int index = 0;
while(true)
{

if(process[index][2] >= qTime)
{
process[index][2] = process[index][2]- qTime;
counter = counter + qTime;
System.out.println("Process" +process[index][0]);
}
if(process[index][2] != 0 && process[index][2] < qTime )
{
process[index][2] = process[index][2] - process[index][2];
counter = counter + process[index][2];
System.out.println("Process" +process[index][0]);
}

boolean condition = true;
int i = 0;
while(condition)//check any uncompleted process
{
if(i == process.length){
System.out.println("All Process Executions are completed");
System.exit(0);}
else if(process[i][2] == 0)
{
i++;
}
else
{
if(index < process.length-1)
{
if(counter >= process[index+1][1] )
{ index = index + 1;}
else
{ index = 0;}
}
else
{ index = 0;}

condition = false;
}
}

}


// TODO code application logic here
}

private static int getIndexOfSmallest(int startIndex, int[][] a) {
int min = a[startIndex][1];
int indexOfMin = startIndex;
for(int index = startIndex+1; index <a.length; index++)
{
if(a[index][1] < min)
{
min = a[index][1];
indexOfMin = index;
}
}
return indexOfMin;
}
private static void interchange(int i, int j, int[][] a)
{
int temp0 = a[i][0];
int temp1 = a[i][1];
int temp2 = a[i][2];
a[i][0] = a[j][0];
a[i][1] = a[j][1];
a[i][2] = a[j][2];
a[j][0] = temp0;
a[j][1] = temp1;
a[j][2] = temp2;
}
}

JAva 代码文件:https://skydrive.live.com/redir?resid=45E9B19710622F21!107执行过程中的图像:https://skydrive.live.com/redir?resid=45E9B19710622F21!108

最佳答案

您需要的是一个简单的列表。循环很容易实现为列表。只需从列表的前面删除,然后将其放到后面。

ArrayList<Task> tasksToExecute = new ArrayList<Task>();
// some code here that populates the list the first time

while (needToWork)
{
Task nextTask = tasksToExecute.remove(0); // get the first object
// process the task here
tasksToExecute.add(nextTask); // put the task back on the list
}

这将平均处理列表中的所有任务。

关于java - 循环法 Java : Execution Process Not Occur Based on roundrobin algorithm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com