银行家算法之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]+"&nbsp;&nbsp;&nbsp;&nbsp;";    
    };
    table+="<p><p/><hr/>";
    table+="请输入个进程的最大资源需求数(MAX)和已分配数(Allocation):(Need会自动计算)<br/>";
    table+="<table border=0 cellspacing=1 width=100% style='text-align:center'><tr><td>&nbsp;</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>

 

    原文作者:DragonDean
    原文地址: https://www.cnblogs.com/dragondean/p/js-banking.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞