1. Java语言期末课程设计“操作系统中银行家算法的实现”
有电子版可以参考。
2. 求一个有界面设计的银行家算法。完整版。java。急!!!!
JTextField 的组件定义在类变量 ,取值 就是 组件名.getText()
3. 求:用JAVA语言编写的银行家算法的源代码
import java.util.*;
class ThreadTest {
static int type = 4, num = 10; //定义资源数目和线程数目
static int[] resource = new int[type]; //系统资源总数
//static int[] copyResource = new int[type]; //副本
static Random rand = new Random();
static Bank[] bank = new Bank[num]; //线程组
Bank temp = new Bank();
public void init() {
//初始化组中每个线程,随机填充系统资源总数
for(int i = 0; i < type; i++)
resource[i] = rand.nextInt(10) + 80;
System.out.print("Resource:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
for(int i = 0; i < bank.length; i++)
bank[i] = new Bank("#" + i);
}
public ThreadTest4() {
init();
}
class Bank extends Thread {
//银行家算法避免死锁
public int[]
max = new int[type], //总共需求量
need = new int[type], //尚需资源量
allocation = new int[type]; //已分配量
private int[]
request = new int[type], //申请资源量
copyResource = new int[type]; //资源副本
private boolean isFinish = false; //线程是否完成
int[][] table = new int[bank.length][type*4]; //二维资源分配表
private void init() {
// 随机填充总共、尚需、已分配量
synchronized(resource) {
for(int i = 0; i < type; i++) {
max[i] = rand.nextInt(5) + 10;
need[i] = rand.nextInt(10);
allocation[i] = max[i] - need[i];
resource[i] -= allocation[i]; //从系统资源中减去已分配的
}
printer();
for(int i = 0; i < type; i++) {
if(resource[i] < 0) {
//若出现已分配量超出系统资源总数的错误则退出
System.out.println("The summation of Threads' allocations is out of range!");
System.exit(1);
}
}
}
}
public Bank(String s) {
setName(s);
init();
start();
}
public Bank() {
//none
}
public void run() {
try {
sleep(rand.nextInt(2000));
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
while(true) {
//程序没有完成时一直不断申请资源
if(askFor() == false) {
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
else
tryRequest();
if(noNeed() == true)
break;
}
//休眠一段时间模拟程序运行
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(getName() + " finish!");
synchronized(resource) {
//运行结束释放占有资源
for(int i = 0; i < type; i++) {
resource[i] += allocation[i];
need[i] = allocation[i] = max[i] = 0;
}
}
}
private void printer() {
//打印当前资源信息
System.out.print(getName() + " Max:");
for(int i = 0; i < type; i++)
System.out.print(" " + max[i]);
System.out.print(" Allocation:");
for(int i = 0; i < type; i++)
System.out.print(" " + allocation[i]);
System.out.print(" Need:");
for(int i = 0; i < type; i++)
System.out.print(" " + need[i]);
System.out.print(" Available:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
}
private boolean askFor() {
//随机产生申请资源量并检测是否超标
boolean canAsk = false;
for(int i = 0; i < type; i++) {
request[i] = rand.nextInt(20);
//防止申请量超过所需量
if(request[i] > need[i])
request[i] = need[i];
}
for(int i = 0; i < type; i++) //防止随机申请资源全为0
if(request[i] > 0)
canAsk = true;
synchronized(resource) {
//锁住可供资源检查是否超标
for(int i = 0; i < type; i++) {
if(request[i] > resource[i])
//如果申请资源超过可供资源则等待一段时间后重新申请
return false;
}
}
return canAsk;
}
private void tryRequest() {
//创建副本尝试分配请求
synchronized(resource) {
for(int i = 0; i < type; i++)
//依然要防止请求量超出范围
if(request[i] > resource[i])
return;
for(int i = 0; i < type; i++) {
//复制资源量并减去需求量到一个副本上
copyResource[i] = resource[i];
copyResource[i] -= request[i];
}
System.out.print(getName() + " ask for:");
for(int i = 0; i < type; i++)
System.out.print(" " + request[i]);
System.out.println("");
if(checkSafe() == true) {
//如果检查安全则将副本值赋给资源量并修改占有量和需求量
for(int i = 0; i < type; i++) {
resource[i] = copyResource[i];
allocation[i] += request[i];
need[i] -= request[i];
}
System.out.println(getName() + " request succeed!");
}
else
System.out.println(getName() + " request fail!");
}
}
private boolean checkSafe() {
//银行家算法检查安全性
synchronized(bank) {
//将线程资源信息放入二维资源分配表检查安全性,0~type可用资源/type~type*2所需资源/type*2~type*3占有资源/type*3~-1可用+占用资源
for(int i = 0; i < bank.length; i++) {
for(int j = type; j < type*2; j++) {
table[i][j] = bank[i].need[j%type];
}
for(int j = type*2; j < type*3; j++) {
table[i][j] = bank[i].allocation[j%type];
}
}
//冒泡排序按需求资源从小到大排
for(int i = 0; i < bank.length; i++) {
for(int j = i; j < bank.length-1; j++) {
sort(j, 4);
}
}
//进行此时刻的安全性检查
for(int i = 0; i < type; i++) {
table[0][i] = copyResource[i];
table[0][i+type*3] = table[0][i] + table[0][i+type*2];
if(table[0][i+type*3] < table[1][i+type])
return false;
}
for(int j = 1; j < bank.length-1; j++) {
for(int k = 0; k < type; k++) {
table[j][k] = table[j-1][k+type*3];
table[j][k+type*3] = table[j][k] + table[j][k+type*2];
if(table[j][k+type*3] < table[j+1][k+type])
return false;
}
}
}
return true;
}
private void sort(int j, int k) {
//递归冒泡排序
int tempNum;
if(table[j][k] > table[j+1][k]) {
for(int i = type; i < type*2; i++) {
tempNum = table[j][i];
table[j][i] = table[j+1][i];
table[j+1][i] = tempNum;
}
/*temp = bank[j];
bank[j] = bank[j+1];
bank[j+1] = temp;*/
}
else if(table[j][k] == table[j+1][k] && k < type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围
sort(j, k+1);
}
private boolean noNeed() {
//是否还需要资源
boolean finish = true;
for(int i = 0; i < type; i++) {
if(need[i] != 0) {
finish = false;
break;
}
}
return finish;
}
}
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
//后台线程,设定程序运行多长时间后自动结束
new Timeout(30000, "---Stop!!!---");
}
}
4. java实现银行家算法中的一个 +=不理解 请求大神解答
Http协议Java Web发servlet/jsp些基础理解面内容先要解Http协议
Http协议基本request/response模型请求/响应模型通俗讲问答模式:
浏览器向服务器发起request请求问;
服务器收请求返response响应答
说Servlet/JSPrequestresonse两象清楚吧其实Java WebHttp协议两东西抽象Java类型已
接说Java Web发session
Http没状态协议说原始Http协议浏览器request请求间没关系通俗说说句忘句实际要想发Java Web应用应该让些请求间关系需要request请求间创建些联系session其实"文翻译错通俗讲:要想顺利交谈需要说句想起句
所些建立联系request请求属于某session题目问:前请求意思
面说说session技术实现细节吧:(致应问题三面)
(1)实际Java Web应用session占用服务器段内存空间保存联系request请求间需要保存共享变量;
(2)部session实现同客户相同浏览器段间(称作session超间)内请求作状态共享保持打同浏览器比IE及Chrome启同session且关闭浏览器session随销毁;
(3)session通getAttribute()setAttribute()进行共享变量获取设置说要想保存状态需要用
5. 求用JAVA语言编写的银行家算法的源代码
import java.util.*; class ThreadTest { static int type = 4, num = 10; //定义资源数目和线程数目 static int[] resource = new int[type]; //系统资源总数 //static int[] copyResource = new int[type]; //副本 static Random rand = new Random(); static Bank[] bank = new Bank[num]; //线程组 Bank temp = new Bank(); public void init() { //初始化组中每个线程,随机填充系统资源总数 for(int i = 0; i need[i]) request[i] = need[i]; } for(int i = 0; i 0) canAsk = true; synchronized(resource) { //锁住可供资源检查是否超标 for(int i = 0; i resource[i]) //如果申请资源超过可供资源则等待一段时间后重新申请 return false; } } return canAsk; } private void tryRequest() { //创建副本尝试分配请求 synchronized(resource) { for(int i = 0; i resource[i]) return; for(int i = 0; i table[j+1][k]) { for(int i = type; i < type*2; i++) { tempNum = table[j][i]; table[j][i] = table[j+1][i]; table[j+1][i] = tempNum; } /*temp = bank[j]; bank[j] = bank[j+1]; bank[j+1] = temp;*/ } else if(table[j][k] == table[j+1][k] && k < type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围 sort(j, k+1); } private boolean noNeed() { //是否还需要资源 boolean finish = true; for(int i = 0; i < type; i++) { if(need[i] != 0) { finish = false; break; } } return finish; } } public static void main(String[] args) { ThreadTest t = new ThreadTest(); //后台线程,设定程序运行多长时间后自动结束 new Timeout(30000, "---Stop!!!---"); } }
6. 操作系统课程设计
最近没什么写日记,想写,但是对于感情方面的写多了没意思,变得庸俗。于是,我决定来一个科学的日志。下面就写写我在操作系统那门课中的一个实验:银行家算法。自从学了java,在我对游戏和网络情有独钟的基础上,我抛弃了c语言,因此,这个实验我是用java来编写的。线程我没学,所以就简单地写几个输入,然后得出结果。
银行家算法大家懂吗?我想,我们学院的都应该懂的。简单地来讲,就是假设系统有n种资源,每种资源有m个数量。又假设有k个进程。如果某进程要取得一些资源则系统首先测试是否满足资源请求要求,如果满足,则尝试分配,接着就判断分配后系统有没有发生死锁,有,就还原,没有就继续。如果某进程要求进入内存,则系统要判断所有进程的请求资源数有没有超过可用资源,有则不许建立,没有就可以建立该进程。
花了几个小时(其实大概一个小时就能搞定,不过可能困的原因,有两个错误没有看出来。我在此要提醒大家,编写程序的格式非常重要,不然检查错误是在很难。),终于写完程序和报告。下面就贴上我的代码吧。
/*
* 操作系统实验:
* 《银行家算法》本程序参考课本的例子,资源种数为3.如果要求更多可作相应更改。
*/
/**
*
* @author Kevin 华南农业大学
*/
//银行家算法,此为Banker类。
import java.util.ArrayList;
import java.util.Random;
public class Banker {
static int[] available = {10,5,7}; //各个资源可用的数量。
static ArrayList processM = new ArrayList(); //线性表,里面装的是进程。
public static void main(String[] args){ //主函数,调试用。
Process p1 = new Process(7,5,3);
Process p2 = new Process(3,2,2);
Process p3 = new Process(9,0,2);
Process p4 = new Process(2,2,2);
Process p5 = new Process(4,3,3);
processM.add(p1);
processM.add(p2);
processM.add(p3);
processM.add(p4);
processM.add(p5);
while(!p1.isOK() || !p2.isOK() || !p3.isOK() || !p4.isOK() || !p5.isOK()){ //进程都还有没满足的就继续申请。
p1 = (Process)processM.get(0);
p2 = (Process)processM.get(1);
p3 = (Process)processM.get(2);
p4 = (Process)processM.get(3);
p5 = (Process)processM.get(4);
if(!p1.isOK())
allocation(p1.request(),p1,0); //申请资源,以下同。
if(!p2.isOK())
allocation(p2.request(),p2,1);
if(!p3.isOK())
allocation(p3.request(),p3,2);
if(!p4.isOK())
allocation(p4.request(),p4,3);
if(!p5.isOK())
allocation(p5.request(),p5,4);
}
}
public static boolean allocation(int[] rq ,Process process,int n){ //进程请求分配函数,
if(process.have[0] + rq[0] > process.claim[0] || process.have[1] + rq[1] > process.claim[1] || process.have[2] + rq[2] > process.claim[2]){
System.out.println("申请失败。"+ (n+1)); //如果请求的资源比最大需求大,则申请失败。
return false;
}
else{
if(rq[0] > available[0] || rq[0] > available[0] || rq[0] > available[0]){
//如果要求的资源暂时不够,则先挂起。
}
else{
process.have[0] = process.have[0] + rq[0];
process.have[1] = process.have[1] + rq[1];
process.have[2] = process.have[2] + rq[2];
available[0] = available[0]-rq[0];
available[1] = available[1]-rq[1];
available[2] = available[2]-rq[2];
processM.add(n, process);
processM.remove(n+1);
}
if(safe()){ //如果安全,则分配成功。
System.out.println("申请成功。"+"进程"+ (n+1)+"已获得资源分别为:"+ process.have[0]+" "+process.have[1]+" "+process.have[2]);
return true;
//如果安全,那资源被该进程利用。
}
else{
process.have[0] = process.have[0] - rq[0];
process.have[1] = process.have[1] - rq[1];
process.have[2] = process.have[2] - rq[2];
available[0] = available[0]+ rq[0];
available[1] = available[1]+ rq[1];
available[2] = available[2]+ rq[2];
processM.add(n, process);
processM.remove(n+1);
System.out.println("申请失败。" + (n+1)); //不安全,则申请失败.
return false;
//如果不安全,则还原,并且挂起该进程。
}
}
}
public static boolean safe(){ //判断分配后是否安全。
ArrayList rest = new ArrayList(processM);
Process test ;
int num = rest.size();
int found = num*num;
while(found > 0 && !rest.isEmpty()){
test = (Process)rest.remove(0);
if(test.claim[0] - test.have[0] <= available[0] && test.claim[1] - test.have[1] <= available[1] && test.claim[2] - test.have[2] <= available[2] ){
available[0] = available[0] + test.have[0];
available[1] = available[1] + test.have[1];
available[2] = available[2] + test.have[2];
}
else {
rest.add(test);
}
found--;
}
if(rest.isEmpty()){
return true;
}
else
return false;
}
}
class Process{ //此类为进程类,描述的是一个进程。
int[] claim =new int[3]; //这个进程需要的资源数。
int[] have = new int[3];
public Process(int n1,int n2,int n3){ //初始化进程
claim[0] = n1;
claim[1] = n2;
claim[2] = n3;
have[0] = 0;
have[1] = 0;
have[2] = 0;
}
public boolean isOK(){ //判断这个进程得到满足没有。
if(have[0] == claim[0] && have[1] == claim[1] && have[2] == claim[2]){
return true;
}
else return false;
}
public int[] request(){ //这个函数随机生成3个数,作为某个进程的请求。
Random random = new Random(); //实例化随机对象。
int[] num = new int[3];
num[0] = random.nextInt(10);
System.out.println(num[0]);
num[1] = random.nextInt(10);
System.out.println(num[1]);
num[2] = random.nextInt(10);
System.out.println(num[2]);
return num;
}
}