我一直在尝试用java语言编写死锁检测算法的程序,但是当我输入应该产生死锁的数据时,程序总是输出“没有发生死锁”。我是初学者,如果有人能告诉我我的代码有什么问题,我会很感激。
这是我的代码:
import pandas as pd
from fuzzywuzzy import fuzz
def get_similarities(x):
l = []
for rname in x.index:
print "Getting ratio for %s and %s" % (rname, x.name)
score = fuzz.partial_ratio(rname,x.name)
print "Score %s" % score
l.append(score)
print len(l)
print
return l
a = pd.DataFrame([[1,2],[3,4]],index=['apple','banana'], columns=['aple','banada'])
c = a.apply(get_similarities,axis=0)
print c
print type(c)
跑步:
DataFrame.apply
结果应为:
package org.me.deadlockdetection;
import java.applet.Applet;
import java.awt.*;
import java.util.Scanner;
public class DeadlockDetection{
private int processes, resources, max[][], allocate[][],need[][], available[];
public static void main(String[] args) {
System.out.println("******* Deadlock detection algorithm *******");
new DeadlockDetection().input();
new DeadlockDetection().show();
new DeadlockDetection().cal();
}
public void input(){
Scanner sc=new Scanner(System.in);
System.out.print("Enter no. of processes: ");
processes=sc.nextInt(); //no. of process
System.out.print("Enter no. of resources: ");
resources=sc.nextInt(); //no. of resources
need=new int[processes][resources]; //initializing arrays
max=new int[processes][resources];
allocate=new int[processes][resources];
available=new int[resources];
System.out.println("Enter allocation matrix -->");
for(int i=0;i<processes;i++)
for(int j=0;j<resources;j++)
allocate[i][j]=sc.nextInt(); //allocation matrix
System.out.println("Enter max matrix -->");
for(int i=0;i<processes;i++)
for(int j=0;j<resources;j++)
max[i][j]=sc.nextInt(); //max matrix
System.out.println("Enter available matrix -->");
for(int j=0;j<resources;j++)
available[j]=sc.nextInt(); //available matrix
sc.close();
}
public void show(){
//int i,j;
System.out.println("Processes Allocation Max Available");
for(int i=0;i<processes;i++){
System.out.println("P"+(i+1));
System.out.println(" ");
for(int j=0;j<resources;j++){
System.out.println(" "+allocate[i][j]);
}
System.out.println(" ");
for(int j=0;j<resources;j++){
System.out.println(" "+max[i][j]);
}
System.out.println(" ");
if(i==0){
for(int j=0;j<resources;j++){
System.out.println(" "+available[j]);
}
}
}
}
public void cal(){
int finish[],temp,flag=1, k,c1=0;
int dead[];
int safe[];
int i,j;
finish=new int[100];
dead=new int[100];
safe=new int[100];
for(i=0;i<processes;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<processes;i++)
{
for(j=0;j<resources;j++)
{
need[i][j]=max[i][j]-allocate[i][j];
}
}
while(flag==1)
{
flag=0;
for(i=0;i<processes;i++)
{
int c=0;
for(j=0;j<resources;j++)
{
if((finish[i]==0)&&(need[i][j]<=available[j]))
{
c++;
if(c==resources)
{
for(k=0;k<resources;k++)
{
available[k]+=allocate[i][j];
finish[i]=1;
flag=1;
}
if(finish[i]==1)
{
i=processes;
}
}
}
}
}
}
j=0;
flag=0;
for(i=0;i<processes;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
System.out.println("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<processes;i++)
{
System.out.println("P"+dead[i]);
}
}
else
{
System.out.println("No deadlock occure");
}
}
}
答案 0 :(得分:0)
在main
中,您可以创建3个单独的实例:
new DeadlockDetection().input();
new DeadlockDetection().show();
new DeadlockDetection().cal();
这意味着当您致电show()
和cal()
时,processes
始终为0,因为创建新实例会丢弃您在input()
中所做的事情。你应该改为:
DeadlockDetection dd = new DeadlockDetection();
dd.input();
dd.show();
dd.cal();