Banker.java
import java.util.LinkedList;
public class Banker {
private int[] available; // 拥有各类资源的数目
private int[] work;
Banker() {
}
Banker(int[] available, int type) {
this.available = new int[type];
this.work = new int[type];
for (int i = 0; i < type; i++) {
this.available[i] = available[i];
this.work[i] = available[i];
}
}
/*
* 初步检查客户请求资源是否能给予满足,是return true
*
* @param available 可用资源量
*
* @param request 客户请求资源量
*
* @param need 客户达到最大值所需要的资源量
*/
public boolean checkAvailable(int[] available, int[] request, int[] need) {
for (int i = 0; i < request.length; i++) {
if (request[i] > available[i] || request[i] > need[i]) {
return false;
}
}
return true;
}
/*
* 检查银行家的available是否大于等于客户的need,是return true
*
* @param available 可用资源量
*
* @param need 客户达到最大值所需要的资源量
*/
public boolean checkAvailable(int[] available, int[] need) {
for (int i = 0; i < need.length; i++) {
if (available[i] < need[i]) {
return false;
}
}
return true;
}
/*
*
* @ 系统安全性检查算法 尝试给系统分配资源 每次获得链表中的第一个元素, 符合条件则进行修改,并将该元素放到队尾,然后将flag置为0;
* 否则将该元素放到队尾,并将flag++
*/
public boolean safeCheck(LinkedList<Customer> customers, Banker banker) {
int count = customers.size();
int[] needTemp;
int[] available = banker.getAvailable();
int[] work = available;
int type = available.length;
int flag = 0;
Customer cusTemp;
System.out
.println("进程名" + "\t" + " work" + "\t\t" + " need" + " \t"
+ " Allocation" + "\t" + " Work+Allocation" + "\t "
+ " finish");
while (flag < count) {
cusTemp = customers.getFirst();
needTemp = cusTemp.getNeed();
if (checkAvailable(work, needTemp) && cusTemp.isFinish() == false) {
int[] ava;
int[] work1 = ava = banker.getAvailable();
int[] n;
int[] al;
n = cusTemp.getNeed();
al = cusTemp.getAllocation();
System.out.print(" " + cusTemp.getName() + "\t");
/* work = ava */
for (int j = 0; j < ava.length; j++) {
// work[j] += al[j];
System.out.print(work1[j] + " ");
}
System.out.print("\t");
/* need */
for (int j = 0; j < ava.length; j++) {
System.out.print(n[j] + " ");
}
System.out.print("\t");
/* allocation */
for (int j = 0; j < ava.length; j++) {
System.out.print(al[j] + " ");
}
customers.remove(0);
cusTemp.setFinish(true);
customers.addLast(cusTemp);
int[] allocT = cusTemp.getAllocation();
for (int k = 0; k < type; k++) {
work[k] += allocT[k];
}
banker.setWork(work);
System.out.print(" \t");
for (int j = 0; j < ava.length; j++) {
System.out.print(work1[j] + " ");
}
System.out.print("\t");
System.out.println(cusTemp.isFinish() + " ");
flag = 0;
} else {
customers.removeFirst();
customers.addLast(cusTemp);
flag++;
}
}
for (int i = 0; i < count; i++) {
if (customers.get(i).isFinish() == false) {
return false;
}
}
return true;
}
/*
* 客户请求之后,经过初步判断通过之后,假设给予某个客户所要求的量,然后进行判断
*
* @param customers 客户链表
*
* @param banker 银行家
*
* @param request 请求量
*
* @param name 客户名
*
* @return 通过的话返回true
*/
public boolean checkSafe(LinkedList<Customer> customers, Banker banker,
int[] request, String name) {
for (int k = 0; k < customers.size(); k++) {
if (customers.get(k).getName().equals(name)) {
if (!checkAvailable(available, request, customers.get(k)
.getNeed())) {
return false;
}
int[] alloc = customers.get(k).getAllocation();
for (int c = 0; c < alloc.length; c++) {
alloc[c] += request[c];
}
customers.get(k).setAllocation(alloc);
}
}
return safeCheck(customers, banker);
}
public int[] getAvailable() {
return available;
}
public void setAvailable(int[] available) {
this.available = available;
}
public int[] getWork() {
return work;
}
public void setWork(int[] work) {
this.work = work;
}
}
Customer.java
public class Customer {
private String name;
private boolean finish; //true表示该请求已经被完成
private int type; //需求类型的种类
private int[] need; //尚需的给类资源数
private int[] max; //最大需求
private int[] allocation; //已经分得的给类资源数
Customer(){
}
Customer(String name,int type, int[] max, int[] allocation){
this.name = name;
this.max = new int[type];
this.allocation = new int[type];
this.need = new int[type];
for(int i=0; i<type; i++)
{
this.allocation[i] = allocation[i];
this.max[i] = max[i];
this.need[i] = max[i] - allocation[i];
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFinish() {
return finish;
}
public void setFinish(boolean finish) {
this.finish = finish;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int[] getNeed() {
return need;
}
public void setNeed(int[] need) {
this.need = need;
}
public int[] getMax() {
return max;
}
public void setMax(int[] max) {
this.max = max;
}
public int[] getAllocation() {
return allocation;
}
public void setAllocation(int[] allocation) {
this.allocation = allocation;
}
}
MyOS.java
import java.util.LinkedList;
import java.util.Scanner;
public class MyOS {
static LinkedList<Customer> customers = new LinkedList<Customer>();
static Banker banker = new Banker();
public static void main(String[] args) {
String name = null;
int myCase = 0;
int type ;
int cno;
int[] max ;
int[] allocation;
int[] available;
Scanner input = new Scanner(System.in);
System.out.println("请输入资源种类 :");
type = input.nextInt();
max = new int[type];
allocation = new int[type];
available = new int[type];
System.out.println("请输入客户个数:");
cno = input.nextInt();
for(int k=0; k<type; k++){
System.out.println("请输入银行家的available");
available[k] = input.nextInt();
}
banker = new Banker(available,type);
for(int i=0; i<cno; i++)
{
for(int c=0; c<type; c++){
System.out.println("请输入第"+i+"位客户的max");
max[c] = input.nextInt();
}
for(int j=0; j<type; j++){
System.out.println("请输入第"+i+"位客户的allocation");
allocation[j] = input.nextInt();
}
name = i+1+"号";
Customer cus = new Customer(name, type, max, allocation);
customers.add(i, cus);
}
showCustomer(customers);
System.out.println("输入1检查系统的安全性,输入2向系统发出请求,其他退出");
myCase = input.nextInt();
switch(myCase){
case 1: //检查系统的安全性
if(banker.safeCheck(customers, banker)){
System.out.println("safe!");
}else{
System.out.println("not safe!");
}
break;
case 2: //
System.out.println("Please input a name:");
String pName = input.next();
System.out.println("Please input the request(Press enter key after input a numer):");
int[] request = new int[type];
for(int dd=0; dd<type; dd++)
{
request[dd] = input.nextInt();
}
if(banker.checkSafe(customers, banker, request, pName)){
System.out.println("the request is available!");
}
break;
default:
System.out.println("bye!");
break;
}
}
public static void showCustomer(LinkedList<Customer> customers){
int[] n;
int[] al;
System.out.println("进程名"+"\t\t"+" need"+" \t"+" Allocation"+"\t "+" finish");
for(int i=0; i<customers.size(); i++)
{
Customer cus = customers.get(i);
n = cus.getNeed();
al = cus.getAllocation();
System.out.print(" "+cus.getName()+"\t");
System.out.print("\t");
/*need*/
for(int j=0; j<al.length; j++){
System.out.print(n[j]+" ");
}
System.out.print("\t");
/*allocation*/
for(int j=0; j<al.length; j++){
System.out.print(al[j]+" ");
}
System.out.print("\t");
System.out.println(cus.isFinish()+" ");
}
}
}
输入的格式控制不好看,有什么办法可以改善一下吗?