日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

操作系统--用JavaScript实现银行家算法

發(fā)布時(shí)間:2023/12/2 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 操作系统--用JavaScript实现银行家算法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

銀行家算法產(chǎn)生背景

? ?銀行家算法(Banker's Algorithm)是一個(gè)避免死鎖(Deadlock)的著名算法,是由艾茲格·迪杰斯特拉在1965年為T.H.E系統(tǒng)設(shè)計(jì)的一種避免死鎖產(chǎn)生的算法。它以銀行借貸系統(tǒng)的分配策略為基礎(chǔ),判斷并保證系統(tǒng)的安全運(yùn)行。

? ?在多道程序系統(tǒng)中,雖然借助于多個(gè)進(jìn)程的并發(fā)執(zhí)行來改善系統(tǒng)的利用率,提高系統(tǒng)的吞吐量,但可能發(fā)生一種危險(xiǎn)—死鎖。死鎖就是多個(gè)進(jìn)程在運(yùn)行過程中因爭(zhēng)奪資源而造成的一種僵局,當(dāng)進(jìn)程處于這種僵局狀態(tài)時(shí),如無外力作用,他們將無法再向前進(jìn)行,如再把信號(hào)量作為同步工具時(shí),多個(gè)Wait和Signal操作順序不當(dāng),會(huì)產(chǎn)生進(jìn)程死鎖。?

? ?然而產(chǎn)生死鎖的必要條件有互斥條件,請(qǐng)求和保持條件,不剝奪條件和環(huán)路等待條件。在預(yù)防死鎖的幾種方法中,都施加了較強(qiáng)的限制條件,在避免死鎖的方法中,所施加的條件較弱,有可能獲得令人滿意的系統(tǒng)性能。在該方法中把系統(tǒng)的狀態(tài)分為安全狀態(tài)和不安全狀態(tài),只要能使系統(tǒng)都處于安全狀態(tài),便可避免死鎖。?

銀行家算法數(shù)據(jù)結(jié)構(gòu)

假設(shè)有n個(gè)進(jìn)程m類資源,則有如下數(shù)據(jù)結(jié)構(gòu):

????可利用資源向量Available。這是一個(gè)含有m個(gè) 元素的數(shù)組,其中的每一個(gè)元素代表一類可利用的資源數(shù)目,其初始值是系統(tǒng)中所配置的該類全部可用資源的數(shù)目,其數(shù)值隨該類資源的分配和回收而動(dòng)態(tài)地改變。Available[j]=K,則表示系統(tǒng)中現(xiàn)有Rj 類資源K個(gè)。

????最大需求矩陣Max。這是一個(gè)n*m的矩陣,它定義了系統(tǒng)中n個(gè)進(jìn)程中的每一個(gè)進(jìn)程對(duì)m類資源的最大需求。如果Max[ij]=K,則表示進(jìn)程i需要Rj類資源的最大數(shù)目為K

???分配矩陣Allocation。這也是一個(gè)n*m的矩陣,它定義了系統(tǒng)中每一類資源 當(dāng)前已分配給每一進(jìn)程的資源數(shù)。如果Allocation[ij]=K,則表示 進(jìn)程i當(dāng)前已分得Rj類資源的數(shù)目為K

????需求矩陣Need。這也是一個(gè)n*m的矩陣,用以表示每一個(gè)進(jìn)程尚需的各類資源數(shù)。如果Need[i,j]=K,則表示進(jìn)程i還需要Rj類資源K個(gè),方能完成其任務(wù)。

????上述三個(gè)矩陣存在如下關(guān)系:Need[i,j]= Max[ij]- Allocation[ij]

結(jié)構(gòu)框圖


源代碼

1.HTML部分

<!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 charset="utf-8" /><link rel="stylesheet" href="css/banker.css" type="text/css"/><title>BankerAlgorithmSystem</title></head><body><center><h1>銀行家算法</h1><div id="input">請(qǐng)輸入進(jìn)程數(shù):<input type="text" id="t_process" name="t_process"/><br />請(qǐng)輸入資源數(shù):<input type="text" id="t_resource" name="t_resource" /><br /><br /> <br /><input type="button" id="b_ok" value="確定" name="b_ok" onClick="onClickOK()" /><br /></div><div id="d_display" style="display: none;"><div class="d_table" id="d_table"></div><br /><br /><input type="button" id="b_ok2" value="提交檢查" name="b_ok2" οnclick="onClickOK2()"/><input type="button" id="b_ok3" value="請(qǐng)求資源" name="b_ok3" οnclick="Banker()" /><div class="output" id="output"></div><div class="output" id="outputlist"></div><div class="output" id="output2"></div></div> </center><script src="js/banker.js" type="text/javascript"></script></body> </html>

2.JavaScript部分

var num_process; //記錄進(jìn)程數(shù) var num_resource;//記錄資源數(shù) var max = new Array();//最大資源數(shù) var need = new Array();//資源需求數(shù) var work = new Array();//資源可用數(shù) var work2 = new Array();//用于記錄每次進(jìn)程調(diào)用的Work數(shù) var available = new Array();//可利用資源數(shù) var allocation = new Array();//已分配資源 var request = new Array();//請(qǐng)求資源數(shù) var finish = new Array();//是否已完成 var safe = new Array();//安全序列 var fg = false; //更新Available標(biāo)志 var o = 0;//動(dòng)態(tài)創(chuàng)建表格(第一個(gè)表格) function CreateTable(){var tabletext = "";tabletext = "</br>系統(tǒng)資源的總數(shù)依次是:";for(i=0;i<num_resource;i++){tabletext += " " + available[i] + "????";}tabletext += "<p><p/><hr/>";tabletext += "請(qǐng)輸入各個(gè)進(jìn)程的最大需求數(shù)(Max)和已分配數(shù)(Allocation)</br>";tabletext += "<table border=1 cellspacing=1 width=80% style='text-align:center;border-collapse:collapse;border-width:thin;border-style:solid;margin:0;'><tr><td>資源</td><td colspan="+num_resource+">Max</td><td colspan="+num_resource+">Allocation</td colspan="+num_resource+"><td colspan="+num_resource+">Need</td colspan="+num_resource+"><td colspan="+num_resource+">Available</td></tr>";tabletext += "<tr>"+"<td>進(jìn)程</td>";for(i=0;i<4;i++){for(j=0;j<num_resource;j++){tabletext += "<td>"+String.fromCharCode((65+j))+"</td>";}}tabletext += "</tr>";for(i=0;i<num_process;i++){tabletext += "<tr><td>P"+i+"</td>";for(j=0;j<4;j++){for(x=0;x<num_resource;x++){tabletext += "<td class='numtd'><input type=text id=e"+i+j+x+" class= 'numtext'"; if(j==2||j==3){tabletext += " readonly=\"readonly\" "}tabletext += "></td>";} }tabletext += "</tr>";}tabletext += "</table>";document.getElementById("d_table").innerHTML += tabletext; }//創(chuàng)建安全表格 function chickSafeTable(){var tabletext = "";tabletext = "<table border=1 cellspacing=1 width=80% style='text-align:center;border-collapse:collapse;border-width:thin;border-style:solid;margin:0;'><tr><td>資源</td><td colspan="+num_resource+">Work</td><td colspan="+num_resource+">Need</td colspan="+num_resource+"><td colspan="+num_resource+">Allocation</td colspan="+num_resource+"><td colspan="+num_resource+">Work+Allocation</td colspan="+num_resource+"><td>Finish</td></tr>";tabletext += "<tr>"+"<td>進(jìn)程</td>";for(i=0;i<4;i++){for(j=0;j<num_resource;j++){tabletext += "<td>"+String.fromCharCode((65+j))+"</td>";}}tabletext += "</tr>";for(i=0;i<num_process;i++){tabletext += "<tr><td>P"+safe[i]+"</td>";for(j=0;j<5;j++){for(x=0;x<num_resource;x++){if(j==4&&x==0){tabletext += "<td id=t"+i+j+x+" class='outtable'></td>"; break;}else{tabletext += "<td id=t"+i+j+x+" class='outtable'></td>"; }} }tabletext += "</tr>";}tabletext += "</table>";document.getElementById("output2").innerHTML += tabletext;updataOfSafeList(); }//更新安全表格(第二個(gè)表格) function updataOfSafeList(){//Workfor(i=0;i<num_process;i++){for(j=0;j<num_resource;j++){document.getElementById("t"+i+"0"+j).innerHTML = work2[i][j]; } }//Needfor(i=0;i<num_process;i++){for(j=0;j<num_resource;j++){document.getElementById("t"+i+"1"+j).innerHTML = need[parseInt(safe[i])][j];} }//Allocationfor(i=0;i<num_process;i++){for(j=0;j<num_resource;j++){document.getElementById("t"+i+"2"+j).innerHTML = allocation[parseInt(safe[i])][j];} }//Work+Allocationfor(i=0;i<num_process;i++){for(j=0;j<num_resource;j++){document.getElementById("t"+i+"3"+j).innerHTML = work2[i][j]+allocation[safe[i]][j];} }//Finishfor(i=0;i<num_process;i++){document.getElementById("t"+i+"4"+"0").innerHTML = finish[safe[i]];} }//點(diǎn)擊第一個(gè)按鈕 function onClickOK(){document.getElementById("input").style.display = "none";num_process = parseInt(document.getElementById("t_process").value);num_resource = parseInt(document.getElementById("t_resource").value);ChickNull(num_process,"請(qǐng)輸入進(jìn)程數(shù):");ChickNull(num_resource,"請(qǐng)輸入資源數(shù):");if(isNaN(num_process&&num_resource)){alert("請(qǐng)輸入數(shù)字!");return;}alert(num_process+"個(gè)進(jìn)程"+num_resource+"個(gè)資源");for(i=0;i<num_resource;i++){available[i] = window.prompt("第"+(i+1)+"個(gè)資源總數(shù):");ChickNull(available[i],"請(qǐng)輸入資源總數(shù):");if(isNaN(available[i])){alert("請(qǐng)輸入數(shù)字!");return;}}CreateTable();document.getElementById("d_display").style.display = ""; }//點(diǎn)擊第二個(gè)按鈕 function onClickOK2() {GetInfo();ChickSequence();PrintSequence("outputlist"); }//獲得填充數(shù)據(jù) function GetInfo() {//獲取最大資源數(shù)for(i=0;i<num_process;i++){max[i]=new Array();for(j=0;j<num_resource;j++){max[i][j]=parseInt(document.getElementById("e"+i+"0"+j).value);ChickNull(max[i][j],"請(qǐng)輸入最大資源數(shù):");if(isNaN(max[i][j])){alert("請(qǐng)輸入數(shù)字!");return;}} }//獲取已分配資源數(shù)for(i=0;i<num_process;i++){allocation[i]=new Array();for(j=0;j<num_resource;j++){allocation[i][j]=parseInt(document.getElementById("e"+i+"1"+j).value);ChickNull(allocation[i][j],"請(qǐng)輸入已分配資源數(shù):");if(isNaN(allocation[i][j])){alert("請(qǐng)輸入數(shù)字!");return;}} } }//得到并填充Need function GetNeed() {//計(jì)算各進(jìn)程對(duì)個(gè)資源的需求量for(i = 0; i < num_process; i ++){need[i]=new Array();for(j = 0; j < num_resource; j ++){need[i][j] = max[i][j] - allocation[i][j];}}//填充Needfor(i=0;i<num_process;i++){for(j=0;j<num_resource;j++){document.getElementById("e"+i+"2"+j).value = need[i][j];} } }//得到Work function GetWork() {for(j=0;j<num_resource;j++){work[j]=available[j];} }//得到并填充Available function GetAvailable(fg) {//計(jì)算Availableif(!fg){for(i=0;i<num_resource;i++){for(j=0;j<num_process;j++){available[i] -= allocation[j][i];if(available[i]<0){alert("請(qǐng)求失敗!無可利用資源");return false;}}}}else{if(available[i]<0){alert("請(qǐng)求失敗!無可利用資源");return false;}else{}}//填充Availablefor(i=0;i<num_resource;i++){document.getElementById("e"+0+"3"+i).value = available[i]; }return true; }//新請(qǐng)求資源 function Banker() {fg = true;var v1 = parseInt(window.prompt("請(qǐng)輸入第幾個(gè)進(jìn)程請(qǐng)求資源"));for(i=0;i<num_process;i++){request[i] = new Array();}for(j=0;j<num_resource;j++){request[v1-1][j] = window.prompt("進(jìn)程P"+(v1-1)+"請(qǐng)求資源"+String.fromCharCode((65+j))+"數(shù)量:");ChickNull(request[v1-1][j],"請(qǐng)輸入進(jìn)程所請(qǐng)求資源數(shù):");if(isNaN(request[v1-1][j])){alert("請(qǐng)輸入數(shù)字!");return;}}for(j=0;j<num_resource;j++){if(request[v1-1][j]>need[v1-1][j]){alert("請(qǐng)求資源數(shù)大于所需最大值,失敗!");return;}else if(request[v1-1][j]>available[j]){alert("請(qǐng)求資源數(shù)大于可利用資源量,請(qǐng)等待!");return;}else{available[j] -= request[v1-1][j];var v2 = parseInt(allocation[v1-1][j]);var v3 = parseInt(request[v1-1][j]);allocation[v1-1][j] = v2+v3;need[v1-1][j] -= request[v1-1][j];}}ChickSequence();PrintSequence("output2"); }//獲得安全序列 function ChickSequence() {GetNeed();GetAvailable(fg);GetWork();//初始化work2for(i=0;i<(num_process+1);i++){work2[i] = new Array();}for(i=0;i<num_resource;i++){work2[0][i] = work[i];}//初始化finishfor(i=0;i<num_process;i++){finish[i] = false;}o = 0;//算法核心!!!while(o < num_process){flag = false;for(i = 0; i < num_process; i ++){if(finish[i])continue;for( j = 0; j < num_resource; j ++){if(need[i][j] > work[j])break;}if(j == num_resource){flag = true;safe[o] = i;o++;finish[i] = true;for(k = 0; k < num_resource; k ++){work[k] += allocation[i][k];work2[o][k] = work[k];} }}if(!flag)break;} }//輸出安全序列 function PrintSequence(id) {if(o == num_process){html="<hr/>該資源是安全的;安全序列為:";for(i=0;i<o;i ++){html+="P"+safe[i];if(i<o-1)html+="->";} }else {html="<hr/>對(duì)不起,該資源狀態(tài)不安全!";document.getElementById(id).innerHTML = html;return;}document.getElementById(id).innerHTML = html;chickSafeTable(); }//判斷輸入是否為空 function ChickNull(text,warning) {if(text.length==0){alert(warning);return false; }else if (/\s/.test(text)){alert("輸入不能為空格!");return false; }return true; }

3.css部分

body{background-image: url(../img/bg2.jpg);background-size:100%;background-color:#000000;font-family:微軟雅黑;font-size: 20px;color: #FFFFFF; }h1{font-family:微軟雅黑;font-size:50px;color:#FFFFFF; }.numtext{border: 0px;width: 90%;height: 100%;text-align: center;background: none;color: #FFFFFF; }.numtd{}#input{margin-top: 0px;margin-right: auto;margin-bottom: 0px;margin-left: auto; }#b_ok{background-color: #87CEEB;color:#FFFFFF;font-family:微軟雅黑;font-weight: 900;font-size: 15px;padding:3px 5px;border-radius:100% ;cursor:pointer;width:100px;height:100px;}#b_ok2,#b_ok3{background-color: #87CEEB;color:#FFFFFF;font-family:微軟雅黑;font-weight: 900;font-size: 15px;padding:3px 5px;border-radius:15px ;cursor:pointer;width:100px;height:30px;}#t_process,#t_resource{text-align: center;background: none;color: #FFFFFF; }



轉(zhuǎn)載于:https://www.cnblogs.com/Toring/p/6628304.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的操作系统--用JavaScript实现银行家算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。