package lvkai;
import java.util.ArrayList;
import java.util.List;
public class Process implements Runnable {
public int id; //进程的编号
int[] need = new int[Main.sourceCount]; //定义进程还需要的资源
int[] allocation = new int[Main.sourceCount]; //定义进程当前已经分配的资源数
int[] max = new int[Main.sourceCount]; //定义进程需要的最大资源数
boolean finish = false; //判断进程是否完成的进程
boolean block = false; //判断进程是否阻塞
List<int[]> list = new ArrayList<int[]>(); //定义进程的请求队列
private int listIndex = 0; //定义可以读取的元素位置
private static boolean[] finishOfBank = new boolean[Main.processCount]; //变量是在银行家算法的安全性检查中使用
private static int[] work = new int[Main.sourceCount];
/**
* 进程进行初始化
*/
public void init(){
for(int i = 0 ; i < Main.sourceCount; i++){
need[i] = max[i];
this.allocation[i] = 0;
}
}
public void run() {
init();
while(this.list.size() != 0){
if(true == Main.safe){
bank();
} else{
this.random();
}
}
}
/**
* 银行家算法
*/
public void bank(){
try {
synchronized(Class.forName("lvkai.Process")){
if(!finish && listIndex <= this.list.size()){
//输出请求的资源向量
String source = "{";
for(int j = 0; j < Main.sourceCount; j++){
source = source + this.list.get(listIndex)[j] + ",";
}
source = source.substring(0, source.length() - 1) + "}";
this.print("进程" + this.id + "开始请求资源" + source);
if(checkGetSourceByBank()){
this.print("进程"+ id + "申请资源" + source + "成功");
this.block = false;
listIndex++;
}else{
this.print("进程"+ id + "申请资源" + source + "失败");
this.block = true;
}
//allocationSource();
//listIndex ++;
//this.block = false;
if(listIndex == this.list.size()){
this.finish = true;
releaseSource();
}
try {
Thread.sleep((long) (Math.random() * 1000 * 2));
} catch (InterruptedException e) {
e.printStackTrace();
}
this.print("");
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 银行家算法的执行安全行检查的方法,并且如果找到一个安全序列就分配资源,否则不分配资源
* @return
*/
private boolean checkGetSourceByBank() {
//初始化work[]、finishOfBank[]
for(int i = 0; i < Main.sourceCount; i++){
work[i] = Main.available[i];
}
for(int i = 0; i < Main.processCount; i++){
finishOfBank[i] = false;
}
//试探着分配资源
if(false == tryAllocationSource()){
return false;
}
finishOfBank[id] = true;
//判断是否该成功的分配资源,如果不可以则释放分配的资源
if(findASafeOrder() == true){
return true;
} else{
this.print("未找到一个安全序列");
giveBackallocationSource();
}
return false;
}
/**
* 系统试探着分配资源给进程
* 如果不能分配资源返回false,能分配返回true
* @return
*/
private boolean tryAllocationSource() {
for(int i = 0; i < Main.sourceCount; i++){
if(this.list.get(listIndex)[i] > Main.available[i]){
this.print("系统不能试探着分配给进程资源");
return false;
}
}
String getSource = "{";
String systemHaveSource = "{";
for(int i = 0; i < Main.sourceCount; i++){
getSource = getSource + this.list.get(this.listIndex)[i] + ",";
systemHaveSource += Main.available[i] + ",";
Main.available[i] -= this.list.get(this.listIndex)[i];
this.allocation[i] += this.list.get(this.listIndex)[i];
this.need[i] -= this.list.get(this.listIndex)[i];
}
getSource = getSource.substring(0, getSource.length() - 1);
getSource += "}";
systemHaveSource = systemHaveSource.substring(0, systemHaveSource.length() - 1);
systemHaveSource += "}";
this.print("当前系统拥有资源数为" + systemHaveSource);
this.print("系统尝试着分配给进程" + id + getSource + "的资源");
return true;
}
/**
* 由于未找到一个安全序列
* 返回试探着分配的资源,并打印当前系统拥有的资源数和进程返回资源成功
*/
private void giveBackallocationSource() {
String getSource = "{";
String systemHaveSource = "{";
for(int i = 0; i < Main.sourceCount; i++){
getSource = getSource + this.list.get(this.listIndex)[i] + ",";
systemHaveSource += Main.available[i] + ",";
Main.available[i] += this.list.get(this.listIndex)[i];
this.allocation[i] -= this.list.get(this.listIndex)[i];
this.need[i] += this.list.get(this.listIndex)[i];
}
finishOfBank[id] = false;
getSource = getSource.substring(0, getSource.length() - 1);
getSource += "}";
systemHaveSource = systemHaveSource.substring(0, systemHaveSource.length() - 1);
systemHaveSource += "}";
this.print("当前系统拥有资源数为" + systemHaveSource);
this.print("进程" + id + "返回试探着分配的资源" + getSource + "成功...");
}
/**
*找到一个进程安全序列
*打印所找到的安全序列
*如果找不到返回false,找到返回true
* @return
*/
private boolean findASafeOrder() {
int processId;
boolean b;
String order = "{" + this.id + ",";
do{
processId = findAProcess();
if(processId == -1){
break;
}
order += processId + ",";
for(int j = 0; j < Main.sourceCount; j++){
work[j] += Main.processes[processId].allocation[j];
}
finishOfBank[processId] = true;
}while(processId != -1);
if(true == isAllfinishOfBankTrue()){
b = true;
order = order.substring(0, order.length() - 1) + "}";
this.print("找到一个安全序列" + order);
} else{
b = false;
}
return b;
}
/**
* 从进程集合中找到一个满足条件的进程
* 若找到返回进程的序列号,否则返回-1
* @return
*/
private int findAProcess() {
//表示如果还有一个进程的finishOfBank为false,则b1为false
for(int i = 0; i < Main.processCount; i++){
boolean b1 = false, b2 = true;
if(false == finishOfBank[i]){
b1 = true;
}
for(int j = 0; j < Main.sourceCount; j++){
if(this.need[j] <= work[j] ){
} else{
b2 = false;
}
}
if(true == b1 && true == b2){
return i;
}
}
return -1;
}
/**
* 检查是否所有的finishOfBank都为true
* @return
*/
private boolean isAllfinishOfBankTrue() {
for(int i = 0; i < Main.processCount; i++){
if(false == finishOfBank[i]){
return false;
}
}
return true;
}
/**
* 随机分配算法
*/
public void random(){
try {
synchronized(Class.forName("lvkai.Process")){
if(!finish){
if(checkGetSourceByRandom()){
this.block = false;
allocationSource();
listIndex ++;
} else{
this.block = true;
}
if(listIndex == this.list.size()){
this.finish = true;
releaseSource();
}
try {
Thread.sleep((long) (Math.random() * 1000 * 2));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 当进程执行完毕时,释放系统的资源
*/
private void releaseSource() {
String source = "{";
for(int i = 0 ; i < Main.sourceCount; i++){
Main.available[i] += this.max[i];
source += this.max[i] + ",";
}
source = source.substring(0, source.length() - 1) + "}";
this.print("进程执行完毕,释放资源" + source);
}
/*
*分配资源函数
*功能包括系统可用资源向量减少
*当前以分配矩阵增大
*需求矩阵减少
*输出进程分配资源的相关信息
*/
private void allocationSource() {
String getSource = "{";
String systemHaveSource = "{";
for(int i = 0; i < Main.sourceCount; i++){
getSource = getSource + this.list.get(this.listIndex)[i] + ",";
systemHaveSource += Main.available[i] + ",";
Main.available[i] -= this.list.get(this.listIndex)[i];
this.allocation[i] += this
史上最强大的java版的银行家算法
4星 · 超过85%的资源 需积分: 10 51 浏览量
2009-04-18
14:21:49
上传
评论 5
收藏 14KB RAR 举报
poweruser5956
- 粉丝: 3
- 资源: 6
最新资源
- 海尔H42E10 6i981BTA DH1PY600000 MBOOT usb.bin 程序.zip
- 网盘的功能实现之数据库搭建笔记
- 软件工程与软件测试阶段作业三.doc
- 软件工程中的人工智能辅助设计研究.pptx
- 海尔智能电视刷机数据 H42E07 机编DH1QR000H07 务必确认机编一致 强制刷机 整机USB升级主程序
- 4444444444444444444
- 基于PHP实现响应式创业园区管委会网站.zip
- 安卓手机用作电脑网络摄像头的工具文件
- 海尔智能电视刷机数据 H32E10 机编DH1QX000805 务必确认机编一致 强制刷机 整机USB升级主程序
- ComfyUI AI老照片修复上色工作流
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页