银行家算法之JavaScript实现
上学期有个课程叫做操作系统,期末的时候这课程还有个课程设计,其中有个题目叫做银行家算法。
什么是银行家算法我就不解释了!
看着同学们的设计,大同小异甚至前篇一律。
清一色的控制台程序,清一色的蛋疼输入,甚至提示都是式样的!
然后我的看的淡定了,于是....
银行家算法的JavaScript版出现了...
借助html和css表现,javascript实现逻辑功能
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>银行家算法模拟系统</title>
<script src="http://lib.sinaapp.com/js/jquery/1.7/jquery.min.js"></script>
<script>
var maxn = 101;
var num_process;
var resource;
var bmax=new Array();//最大资源数
var need=new Array();//资源需求数
var work=new Array();//资源可用数
var available=new Array();//总资源数
var allocation=new Array();//已分配资源
var finish=new Array();//是否已完成
var bp=new Array();//安全序列
var l=0; function GetWork()
{
for(j=0;j<num_resource;j++)
{
work[j]=available[j];//=available[j]-allocation[i][j];
}
for(i=0;i<num_process;i++)
{
for(j=0;j<num_resource;j++)
{
work[j]-=allocation[i][j];
}
}
/*for(j=0;j<num_resource;j++)
{
alert(work[j]);//=available[j]-allocation[i][j];
}*/
}
//求安全序列
function GetSequence()
{
l=0;
//alert(l);
GetNeed();//
GetWork();
for(i = 0; i < num_process; i ++)
finish[i] = false;//初始化各进程的分配标志 while(l < num_process)
{
flag = false;
for(i = 0; i < num_process; i ++)
{
if(finish[i])continue;
//alert(need.length);
for( j = 0; j < num_resource; j ++)
{
if(need[i][j] > work[j])break;
}
if(j == num_resource)
{
flag = true;
bp[l] = i;
l++;
finish[i] = true;
for(k = 0; k < num_resource; k ++)
work[k] += allocation[i][k];
}
}
if(!flag) break;
}
} function CreateTable()
{
table="<br/>系统资源量数目依次是:";
for(i=0;i<num_resource;i++)
{
table+=" "+available[i]+" ";
};
table+="<p><p/><hr/>";
table+="请输入个进程的最大资源需求数(MAX)和已分配数(Allocation):(Need会自动计算)<br/>";
table+="<table border=0 cellspacing=1 width=100% style='text-align:center'><tr><td> </td><td colspan="+$("#num_resource").val()+">Max</td><td colspan="+$("#num_resource").val()+">Allocation</td colspan="+$("#num_resource").val()+"><td colspan="+$("#num_resource").val()+">Need</td></tr>";
for(i=0;i<$("#num_process").val();i++)
{
table+="<tr><td>进程"+i+"</td>";
for(j=0;j<3;j++)
{
for(x=0;x<$("#num_resource").val();x++)
{
table+="<td><input type=text id=e"+i+j+x;
if(j==2){table+=" readonly=\"readonly\" "}
table+="></td>";
}
}
table+="</tr>";
}
table+="</table>";
$("#table").html(table)
}
function GetInfo()
{
//获取最大资源数
for(i=0;i<num_process;i++)
{
bmax[i]=new Array();
for(x=0;x<num_resource;x++)
{
bmax[i][x]=$("#e"+i+"0"+x).val()
}
} //获取已分配资源数
for(i=0;i<num_process;i++)
{
allocation[i]=new Array();
for(x=0;x<num_resource;x++)
{
allocation[i][x]=$("#e"+i+"1"+x).val()
}
} } function GetNeed()
{
//计算各进程对个资源的需求量
for(i = 0; i < num_process; i ++)
{
need[i]=new Array();
for(j = 0; j < num_resource; j ++)
{
need[i][j] = bmax[i][j] - allocation[i][j];
}
}
for(i=0;i<num_process;i++)
{
//need[i]=new Array();
for(x=0;x<num_resource;x++)
{
//$("#e"+i+"2"+x).val(need[i][x]);
//alert($("#e"+i+"2"+x).val());
$("#e"+i+"2"+x).val(need[i][x]);
}
}
}
function PrintSequence()
{
if(l == num_process)
{
lhtml="该资源是安全的;安全序列为:";
for(i = 0; i < l; i ++)
{
if(i) lhtml+="=>";
lhtml+=bp[i];
} }
else lhtml="对不起,该资源状态不安全!";
lhtml+="<br/>亲,如果有新的进程请求可以直接在上表中修改数据并确定哦!";
$("#l").html(lhtml);
} $(document).ready(function(e) {
$("#ok1").click(function(e) {
num_process=$("#num_process").val();
num_resource=$("#num_resource").val();
alert(num_process+"个进程,"+num_resource+"个资源");
for(i=0;i<num_resource;i++)
{
available[i]= window.prompt("请输入系统总的资源数量:","请输入资源"+i+"的数量:");
}
CreateTable();
$("#h").show();
}); $("#ok2").click(function(e) {
GetInfo();
GetSequence();
PrintSequence();
});
$("#come").click(function(e) {
$(".cover").fadeOut();
});
$("input").change(function(e) {
var reg=new RegExp("^[0-9]*$");
if(!reg.test($(this).val())==true){
alert("对不起,您必须输入数字!");
$(this).addClass("border");
$(this).focus();
}
else{
$(this).removeClass("border");
}
}); });
</script>
<style>
body{ margin:0; text-align:center;}
input{ width:20px;}
table{ background:#999999;}
td{ background:#FFFFFF;}
.cover{ background:#00FFFF; position:absolute; width:100%; height:100%; padding-top:20px; text-align:center;}
.border{ border-bottom:1px solid #F00;}
</style>
</head> <body>
<div class="cover">
<h1>欢迎使用银行家算法模拟系统!</h1>
<input type="button" id="come" value="进入系统" style="height:50px; width:200px;" />
</div>
请输入进程数:
<input type="text" id="num_process" name="num_process" /><br/>
请输入资源数:
<input type="text" id="num_resource" name="num_resource" /><br/>
<input type="button" id="ok1" value="确定" style="width:50px;"/>
<hr/>
<div style="display:none" id="h">
<div class="table" id="table">
s
</div>
<input type="button" id="ok2" value="确定" style="width:50px;" /><br/>
<div class="l" id="l">
</div>
</div> </body>
</html>
银行家算法之JavaScript实现的更多相关文章
- c/c++多线程模拟系统资源分配(并通过银行家算法避免死锁产生)
银行家算法数据结构 (1)可利用资源向量Available 是个含有m个元素的数组,其中的每一个元素代表一类可利用的资源数目.如果Available[j]=K,则表示系统中现有Rj类资源K个. (2) ...
- Round() 四舍五入 js银行家算法(转)
首先问一下round(0.825,2) 返回的结果,大家猜一猜, 首先SQL server 返回的是 0.83 js的返回结果 是0.83,code 如下: var b = 0.825; ...
- 转载部长一篇大作:常用排序算法之JavaScript实现
转载部长一篇大作:常用排序算法之JavaScript实现 注:本文是转载实验室同门王部长的大作,找实习找工作在即,本文颇有用处!原文出处:http://www.cnblogs.com/ywang172 ...
- 银行家算法java实现
关于银行家算法的理论知识,课本或者百度上有好多资料,我就不再多说了,这里把我最近写的银行家算法的实现带码贴出来. 由于这是我们的一个实验,对系统资源数和进程数都指定了,所以这里也将其指定了,其中系统资 ...
- 操作系统,银行家算法模拟实现(Windows 环境 C++)
计算机操作系统课设需要,写了两个下午的银行家算法(陷在bug里出不来耽误了很多时间),参考计算机操作系统(汤子瀛) 实现过程中不涉及难度较大的算法,仅根据银行家算法的思想和步骤进行实现.以下为详细步骤 ...
- JavaScript 排序算法(JavaScript sorting algorithms)
JavaScrip 排序算法(JavaScript Sorting Algorithms) 基础构造函数 以下几种排序算法做为方法放在构造函数里. function ArrayList () { va ...
- c++银行家算法
#include <iostream> #include<string> #define False 0 #define True 1 using namespace std; ...
- 银行家算法C++程序
此程序在Windows10 CodeBlocks17.12环境下测试运行,其他编程环境未经测试! 作业需求↓↓↓↓↓↓ 运行效果图如下 (codeblocks下载地址http://www.cod ...
- python模拟银行家算法
前言: 大二第一学期学习了操作系统,期末实验课题要求模拟算法.遂根据自己学习的python写下此文.以此锻炼自己编码能力.虽说是重复造轮子,但还是自己的思路体现 代码及注释如下(银行家算法不再赘述): ...
随机推荐
- not
x = [] print(x) print(not x) print(x is None) print(not x is None) print(x is not None)
- Android4.0系统接收不到广播的问题解析
在3.1之后,系统的package manager增加了对处于“stopped state”应用的管理,这个stopped和Activity生命周期中的stop状态是完全两码事,指的是安装后从来没有启 ...
- HihoCoder 1183 : 连通性一·割边与割点(模板)
连通性一·割边与割点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 还记得上次小Hi和小Ho学校被黑客攻击的事情么,那一次攻击最后造成了学校网络数据的丢失.为了避免再次 ...
- bzoj 4472 salesman
Written with StackEdit. Description 某售货员小\(T\) 要到若干城镇去推销商品,由于该地区是交通不便的山区,任意两个城镇 之间都只有唯一的可能经过其它城镇的路线. ...
- 一起来看CORE源码(一) ConcurrentDictionary
先贴源码地址 https://github.com/dotnet/corefx/blob/master/src/System.Collections.Concurrent/src/System/Col ...
- C# 在类文件自动添加文件注释的方法
对于vs2013来讲, 步骤: 1.VS2013 中找到(安装盘符以C盘为例)C:\ProgramFiles(x86)\Microsoft VisualStudio12.0\Common7\IDE\I ...
- 关于ip层的作用网址链接
http://rabbit.xttc.edu.cn/rabbit/htm/artical/201091113054.shtml
- button和input type=button的区别及注意事项
<button>标签 定义和用法 <button>标签定义一个按钮. 在button元素内部,您可以放置内容,比如文本或图像.这是该元素与使用input元素创建的按钮之间的不同 ...
- gitlab pipelines 使用
1. 安装runner # For Debian/Ubuntu curl -L https://packages.gitlab.com/install/repositories/runner/gi ...
- PHP Tools for VS2017 key/破解 [搬运]
看看结果 搬运地址 : (自己敲吧...) 这里面破解的只有一年 时间可以在文中提供的 ------------------------------------------------------- ...