OS作业,记录防丢
package Bank;
public class Banker
{
public static void main(String[] args)
{
OS os = new OS(3, 3, 2);
MyThread p0 = new MyThread(os, 0, 7, 5, 3, 0, 1, 0);
MyThread p1 = new MyThread(os, 1, 3, 2, 2, 2, 0, 0);
MyThread p2 = new MyThread(os, 2, 9, 0, 2, 3, 0, 2);
MyThread p3 = new MyThread(os, 3, 2, 2, 2, 2, 1, 1);
MyThread p4 = new MyThread(os, 4, 4, 3, 3, 0, 0, 2);
System.out.println(" Max |" + " Allocation|" + " Need |" + "Available");
System.out.println(" A B C| A B C | A B C |A B C");
p0.start();
p1.start();
p2.start();
p3.start();
p4.start();
}
}
package Bank;
import java.lang.Math;
public class MyThread extends Thread {
int maxA = 0;
int maxB = 0;
int maxC = 0;
int num = 0;
int[] abc_num =new int[3];// 接受request的数组
int reqA = 0;
int reqB = 0;
int reqC = 0;
private OS os;
MyThread(OS os,int num,int maxA, int maxB, int maxC,int allocation_a,int allocation_b,int allocation_c)
{
this.maxA = maxA;
this.maxB = maxB;
this.maxC = maxC;
this.os = os;
os.Allocation[num][0] += allocation_a;
os.Allocation[num][1] += allocation_b;
os.Allocation[num][2] += allocation_c;
os.Need[num][0] += (maxA - allocation_a);
os.Need[num][1] += (maxB - allocation_b);
os.Need[num][2] += (maxC - allocation_c);
this.num = num;
}
public void run()// request
{
while (os.Need[num][0]!=0||os.Need[num][1]!=0||os.Need[num][2]!=0)// 循环一次请求一次
{
reqA = (int) (Math.random() * os.Need[num][0] + 0.1);
reqB = (int) (Math.random() * os.Need[num][1] + 0.1);
reqC = (int) (Math.random() * os.Need[num][2] + 0.1);
try {
Thread.sleep((int) (Math.random() * 100));
if (reqA!=0||reqB!=0||reqC!=0)
{
System.out.println("p"+num+" request A:"+reqA+" request B:"+reqB+" request C:"+reqC);
os.Request(this,os,reqA, reqB, reqC);
System.out.println("p"+num+" "+maxA+" "+maxB+" "+maxC+"|"+" "+os.Allocation[num][0]+" "+os.Allocation[num][1]+" "+os.Allocation[num][2]+" |"+" "+os.Need[num][0]+" "+os.Need[num][1]+" "+os.Need[num][2]+" |"+os.Available[0]+" "+os.Available[1]+" "+os.Available[2]+"|");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
os.Available[0] += maxA;
os.Available[1] += maxB;
os.Available[2] += maxC;
os.Allocation[num][0] -= maxA;
os.Allocation[num][1] -= maxB;
os.Allocation[num][2] -= maxC;
System.out.println("p"+num+"运行完了");
//notifyAll();
}
}
package Bank;
public class OS
{
public int[] Available = { 0, 0, 0 };
public int[][] Max = new int[5][3];
public int[][] Allocation = new int[5][3];
public int[][] Need = new int[5][3];
OS(int a, int b, int c)
{
Available[0] += a;
Available[1] += b;
Available[2] += c;
}
public synchronized void Request(MyThread th, OS os, int a, int b, int c)
{
while (a > os.Available[0] || b > os.Available[1] || c > os.Available[2]||(!Safe(th, os, a, b, c)))
{
try
{
//System.out.println("w1等");
wait(100);
//System.out.println("w2等");
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
os.Available[0] -= a;
os.Available[1] -= b;
os.Available[2] -= c;
os.Allocation[th.num][0] += a;
os.Allocation[th.num][1] += b;
os.Allocation[th.num][2] += c;
os.Need[th.num][0] -= a;
os.Need[th.num][1] -= b;
os.Need[th.num][2] -= c;
notifyAll();
}
private boolean Safe(MyThread th, OS os, int a, int b, int c)
{
boolean IsSafe = false;
int[] work = new int[3];
os.Available[0] -= a;
os.Available[1] -= b;
os.Available[2] -= c;
work[0] = os.Available[0];
work[1] = os.Available[1];
work[2] = os.Available[2];
os.Allocation[th.num][0] += a;
os.Allocation[th.num][1] += b;
os.Allocation[th.num][2] += c;
os.Need[th.num][0] -= a;
os.Need[th.num][1] -= b;
os.Need[th.num][2] -= c;
boolean[] Finish = new boolean[5];
int i = 0;
while(i!=6)
{
i = Find_i(Finish,work);
if (i!=6)
{
work[0] += os.Allocation[i][0];
work[1] += os.Allocation[i][1];
work[2] += os.Allocation[i][2];
Finish[i] = true;
if (IsAllFinish(Finish))
break;
}
}
if (IsAllFinish(Finish))//4
IsSafe = true;
else
IsSafe = false;
os.Available[0] += a;
os.Available[1] += b;
os.Available[2] += c;
os.Allocation[th.num][0] -= a;
os.Allocation[th.num][1] -= b;
os.Allocation[th.num][2] -= c;
os.Need[th.num][0] += a;
os.Need[th.num][1] += b;
os.Need[th.num][2] += c;
return IsSafe;
}
private int Find_i(boolean[] Finish,int[] work)
{
for(int i=0;i<5;i++)
{
if (!Finish[i]&&Need[i][0] <= work[0] && Need[i][1] <= work[1] && Need[i][2] <= work[2])
{
return i;
}
}
return 6;//6就是没有true
}
private static boolean IsAllFinish(boolean[] finish)
{
boolean IsAllFinish = true;
for (int i = 0; i < 5; i++)
{
if (finish[i] == false)
{
IsAllFinish = false;
}
}
return IsAllFinish;
}
}