代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
OS(操作系统原理)的银行家算法
作者:
sscust
/ 发布于
2013/5/28
/
601
系统可用资源=(5,3, 8, 2,10)(资源名分别是A,B,C,D,E) p1进程 3 3 5 0 5 p2进程 5 3 8 1 2 p3进程 2 1 2 0 4 p4进程 4 0 7 0 5 p5进程 1 2 3 2 5 p6进程 3 2 6 2 9 public class Resource { /** 资源的名称*/ public String name; public Resource(String name){ this.name=name; } } -----------------------------Work.java import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /**Work是系统中所有进程和所有资源一个状态*/ public class Work { /**进程队列*/ private List<Process> proc; /**资源总和*/ private Map<Resource,Integer> res; /**可利用资源总和*/ private Map<Resource,Integer> availRes; public Work(List<Process> proc, Map<Resource,Integer> res) { super(); this.proc = proc; this.res = res; } public void setPros(){ } /** * */ public void doWork(){ for (int i = 0; i < this.proc.size(); i++) { if(proc.get(i).Request(this.availRes)){ System.out.println(proc.get(i).getName()+"进程已完毕,下个进程正在请求"); } } } /** * * @param allocation */ public void initialized(Map<Resource,Integer> allocation){ for (int i = 0; i < proc.size(); i++) { this.proc.get(i).setAllocation(allocation); } } /** * * @param availRes */ public void setAvailRes(Map<Resource,Integer> availRes){ this.availRes=new HashMap<Resource, Integer>(); Set<Resource> allSet=availRes.keySet(); Object[] allRes=allSet.toArray(); for (int i = 0; i < allRes.length; i++) { this.availRes.put((Resource)allRes[i], availRes.get(allRes[i])); } } } -----------------------Process.java import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; public class Process { /**进程名*/ private String name; /**最大需求的资源*/ private Map<Resource,Integer> max; /** 还需要的资源数=max-allocation*/ private Map<Resource,Integer> need; /**已分配给该进程的资源数*/ private Map<Resource,Integer>allocation; /**系统是否有足够的资源分配给进程,初始值为设置成false*/ private boolean finish=false; /** * * @return */ public List<Map<Resource, Integer>> getNeed() { return null; } /** * need=max-allocation */ private void setNeed() { this.need=new HashMap<Resource, Integer>(); Set<Resource> allResSet=max.keySet(); Object[] all=allResSet.toArray(); for (int i = 0; i < all.length; i++) { int r=max.get((Resource)all[i]); need.put((Resource)all[i], r-(int)allocation.get(all[i])); } } /** * Constructor * @param name * @param max */ public Process(String name,Map<Resource,Integer> max) { this.name=name; this.max=max; // this.setNeed(); } /** * 向系统请求资源 * @param available * @return */ public boolean Request(Map<Resource,Integer> available){ Set<Resource> allResSet=available.keySet(); Object[] all= allResSet.toArray(); for (int i = 0; i < all.length; i++) { int r=available.get((Resource)all[i]); /**如果当前可分配的资源数矩阵元素出现小于需求量,则分配不成功*/ if(need.get((Resource)all[i])>r){ return false; } } System.out.println(this.getName()+"分配资源成功,已开始执行,几个时钟周期后将执行完"); this.clear(available); return true; } /** * 进程执行完后,释放占用的资源<BR> * (即系统中可利用资源数目会相应增加)<BR> * Set中的toArray()方法不能立即强制类型转化不能:(Resource)all.toArray() * @param available */ private void clear(Map<Resource,Integer> available){ Set<Resource> all=this.max.keySet(); Object [] allResSet=all.toArray(); for (int i = 0; i < allResSet.length; i++) { int sum=max.get((Resource)allResSet[i]); /*在这强制转化一下 String是一种类型, String[]是另一种类型,这是不同的概念。 Object可以强转为String(只要可以)不代表Object[]类型可以强转为String[]类型。*/ available.put((Resource)allResSet[i], available.get((Resource)allResSet[i])+sum); } this.max=null; System.out.println(this.getName()+"进程占用的资源释放了"); } public Map<Resource, Integer> getAllocation() { return allocation; } public void setAllocation(Map<Resource, Integer> allocation) { this.allocation=new HashMap<Resource,Integer>(); Set<Resource> resSet=allocation.keySet(); Object[]resArray=resSet.toArray(); for (int i = 0; i < resArray.length; i++) { int sum=allocation.get((Resource)resArray[i]); this.allocation.put((Resource)resArray[i], sum); } setNeed(); } public String getName() { return name; } public Map<Resource, Integer> getMax() { return max; } } //-----------------------------Test.java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; public class Test { public static void main(String args[]){ Resource A=new Resource("A"); Resource B=new Resource("B"); Resource C=new Resource("C"); Resource D=new Resource("D"); Resource E=new Resource("E"); Map<Resource,Integer> allRes=new HashMap<Resource,Integer>(); allRes.put(A, 5);//系统可用资源=(5,3, 8, 2,10) allRes.put(B, 3);// 3 3 5 0 5 allRes.put(C, 8);// //5 3 8 1 2 allRes.put(D, 2); //2 1 2 0 4 allRes.put(E, 10); //4 0 7 0 5 //Work w //1 2 3 2 5 //3 2 6 2 9 Map<Resource,Integer> mapP1=new HashMap<Resource,Integer>(); Map<Resource,Integer> mapP2=new HashMap<Resource,Integer>(); Map<Resource,Integer> mapP3=new HashMap<Resource,Integer>(); Map<Resource,Integer> mapP4=new HashMap<Resource,Integer>(); Map<Resource,Integer> mapP5=new HashMap<Resource,Integer>(); Map<Resource,Integer> mapP6=new HashMap<Resource,Integer>(); //List mapP1.put(A, 3);//p1进程分别需要资源数目3 3 5 0 5 mapP1.put(B, 3); mapP1.put(C, 5); mapP1.put(D, 0); mapP1.put(E, 5); mapP2.put(A, 5);//p2进程分别需要资源数目5 3 8 1 2 mapP2.put(B, 3); mapP2.put(C, 8); mapP2.put(D, 1); mapP2.put(E, 2); mapP3.put(A, 2);//p3进程分别需要资源数目2 1 2 0 4 mapP3.put(B, 1); mapP3.put(C, 2); mapP3.put(D, 0); mapP3.put(E, 4); mapP4.put(A, 4);//p4进程分别需要资源数目4 0 7 0 5 mapP4.put(B, 0); mapP4.put(C, 7); mapP4.put(D, 0); mapP4.put(E, 5); mapP5.put(A, 1);//p5进程分别需要资源数目1 2 3 2 5 mapP5.put(B, 2); mapP5.put(C, 3); mapP5.put(D, 2); mapP5.put(E, 5); mapP6.put(A, 3);//p6进程分别需要资源数目3 2 6 2 9 mapP6.put(B, 2); mapP6.put(C, 6); mapP6.put(D, 2); mapP6.put(E, 9); Process p1=new Process("p1",mapP1); Process p2=new Process("p2",mapP2); Process p3=new Process("p3",mapP3); Process p4=new Process("p4",mapP4); Process p5=new Process("p5",mapP5); Process p6=new Process("p6",mapP6); List<Process> procList=new ArrayList<Process>(); procList.add(p1); procList.add(p2); procList.add(p3); procList.add(p4); procList.add(p5); procList.add(p6); Work w=new Work(procList, allRes);//设置工作过完 //简单初始设置,初始分配的资源都是空 Map<Resource,Integer> allocation=new HashMap<Resource,Integer>(); allocation.put(A, 0); allocation.put(B, 0); allocation.put(C, 0); allocation.put(D, 0); allocation.put(E, 0); w.initialized(allocation); w.setAvailRes(allRes); w.doWork(); } } -------------------------Process.java import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; public class Process { /**进程名*/ private String name; /**最大需求的资源*/ private Map<Resource,Integer> max; /** 还需要的资源数=max-allocation*/ private Map<Resource,Integer> need; /**已分配给该进程的资源数*/ private Map<Resource,Integer>allocation; /**系统是否有足够的资源分配给进程,初始值为设置成false*/ private boolean finish=false; /** * * @return */ public List<Map<Resource, Integer>> getNeed() { return null; } /** * need=max-allocation */ private void setNeed() { this.need=new HashMap<Resource, Integer>(); Set<Resource> allResSet=max.keySet(); Object[] all=allResSet.toArray(); for (int i = 0; i < all.length; i++) { int r=max.get((Resource)all[i]); need.put((Resource)all[i], r-(int)allocation.get(all[i])); } } /** * Constructor * @param name * @param max */ public Process(String name,Map<Resource,Integer> max) { this.name=name; this.max=max; // this.setNeed(); } /** * 向系统请求资源 * @param available * @return */ public boolean Request(Map<Resource,Integer> available){ Set<Resource> allResSet=available.keySet(); Object[] all= allResSet.toArray(); for (int i = 0; i < all.length; i++) { int r=available.get((Resource)all[i]); /**如果当前可分配的资源数矩阵元素出现小于需求量,则分配不成功*/ if(need.get((Resource)all[i])>r){ return false; } } System.out.println(this.getName()+"分配资源成功,已开始执行,几个时钟周期后将执行完"); this.clear(available); return true; } /** * 进程执行完后,释放占用的资源<BR> * (即系统中可利用资源数目会相应增加)<BR> * Set中的toArray()方法不能立即强制类型转化不能:(Resource)all.toArray() * @param available */ private void clear(Map<Resource,Integer> available){ Set<Resource> all=this.max.keySet(); Object [] allResSet=all.toArray(); for (int i = 0; i < allResSet.length; i++) { int sum=max.get((Resource)allResSet[i]); /*在这强制转化一下 String是一种类型, String[]是另一种类型,这是不同的概念。 Object可以强转为String(只要可以)不代表Object[]类型可以强转为String[]类型。*/ available.put((Resource)allResSet[i], available.get((Resource)allResSet[i])+sum); } this.max=null; System.out.println(this.getName()+"进程占用的资源释放了"); } public Map<Resource, Integer> getAllocation() { return allocation; } public void setAllocation(Map<Resource, Integer> allocation) { this.allocation=new HashMap<Resource,Integer>(); Set<Resource> resSet=allocation.keySet(); Object[]resArray=resSet.toArray(); for (int i = 0; i < resArray.length; i++) { int sum=allocation.get((Resource)resArray[i]); this.allocation.put((Resource)resArray[i], sum); } setNeed(); } public String getName() { return name; } public Map<Resource, Integer> getMax() { return max; } }
试试其它关键字
银行家算法
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
实现测量程序运行时间及cpu使用时间
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
sscust
贡献的其它代码
(
1
)
.
OS(操作系统原理)的银行家算法
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3