我一直在寻找过去3个小时,我找不到这段代码的问题。用户输入进程数和资源类型数,算法确定是否存在死锁。我确定问题出现在stepTwo()方法中,因为我已经检查了第一个代码块中的所有内容,并且一切正常。
这是伪代码:https://www.dropbox.com/s/dt9cvk5h3ij7wqz/deadlockdetection.jpg?dl=0
这是主要的,变量等:
public class AssignmentIII
{
public static int numProcesses; // Represents the number of processes
public static int numResources; // Represents the number of different types of resources
public static int[] available; // Create matrix for available processes
public static int[][] allocation; // Create allocation matrix
public static int[][] request; // Create request matrix
public static int[] work; // Create work matrix
public static Boolean[] finish; // Create finish matrix
public static void main(String[] args) throws FileNotFoundException
{
try
{
Scanner scan = new Scanner(System.in);
Scanner fileScan = new Scanner(new File("input3.txt")); // Create file scanner
System.out.println("Please enter the total number of processes: ");
numProcesses = scan.nextInt();
System.out.println("Please enter the number of different types of resources: ");
numResources = scan.nextInt();
// Initalize matrices sizes
available = new int[numResources];
allocation = new int[numProcesses][numResources];
request = new int[numProcesses][numResources];
work = new int[numResources];
finish = new Boolean[numProcesses];
// Initialize the available matrix contents
for(int i = 0; i < numResources; i++)
available[i]=fileScan.nextInt();
// Initialize the allocation matrix contents
for(int j = 0; j < numProcesses; j++)
for(int k = 0; k < numResources; k++)
allocation[j][k]=fileScan.nextInt();
// Initialize the request matrix contents
for(int m = 0; m < numProcesses; m++)
for(int n = 0; n < numResources; n++)
request[m][n]=fileScan.nextInt();
// Begin deadlock detection algorithm
for(int i = 0; i < numResources; i++) // Intialize Work := Available
work[i]=available[i];
for(int j = 0; j < numProcesses; j++) // Check for Allocation != 0 and initialize Finish accordingly
{
// the Allocation matrix = 0 if the sum of all its elements = 0
// which is the same as if every element were = 0
int sumAllocation = 0;
for(int i = 0; i < numResources; i++)
{
sumAllocation += allocation[j][i];
}
if (sumAllocation != 0)
finish[j] = false;
else finish[j] = true;
}
stepTwo();
}
catch(FileNotFoundException ex)
{
System.out.println("An error has occured. The file cannot be found.");
}
}
错误发生在这里的某个地方,但我无法弄明白:
public static void stepTwo()
{
// Step 2: Find an index j where Finish[j] = false & Request[j] <= Work
for(int j = 0; j < numProcesses; j++)
{
if (finish[j] == true && j == numProcesses)
{
System.out.println("No deadlocks.");
}
for(int i = 0; i < numResources; i++)
{
if (request[j][i] <= work[i] && finish[j] == false)
{
if (i == (numResources-1))
{
finish[j] = true;
for(int m = 0; m < numResources; m++)
{
work[m] += allocation[j][m];
}
j = -1; // Reset counter
break;
}
}
else if (request[j][i] > work[i] && finish[j] == false)
{
System.out.println("P" + j + " is deadlocked.");
}
}
}
}
}
我的意见是:
0 0 0
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
我得到的输出是:
P1陷入僵局。 P1陷入僵局。
正确的输出应该是“没有死锁。”
任何帮助都将不胜感激。