日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2023/12/2 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 操作系统--用JavaScript实现银行家算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

銀行家算法產生背景

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

? ?在多道程序系統中,雖然借助于多個進程的并發執行來改善系統的利用率,提高系統的吞吐量,但可能發生一種危險—死鎖。死鎖就是多個進程在運行過程中因爭奪資源而造成的一種僵局,當進程處于這種僵局狀態時,如無外力作用,他們將無法再向前進行,如再把信號量作為同步工具時,多個Wait和Signal操作順序不當,會產生進程死鎖。?

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

銀行家算法數據結構

假設有n個進程m類資源,則有如下數據結構:

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

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

???分配矩陣Allocation。這也是一個n*m的矩陣,它定義了系統中每一類資源 當前已分配給每一進程的資源數。如果Allocation[ij]=K,則表示 進程i當前已分得Rj類資源的數目為K

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

????上述三個矩陣存在如下關系:Need[i,j]= Max[ij]- Allocation[ij]

結構框圖


源代碼

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">請輸入進程數:<input type="text" id="t_process" name="t_process"/><br />請輸入資源數:<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="請求資源" 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; //記錄進程數 var num_resource;//記錄資源數 var max = new Array();//最大資源數 var need = new Array();//資源需求數 var work = new Array();//資源可用數 var work2 = new Array();//用于記錄每次進程調用的Work數 var available = new Array();//可利用資源數 var allocation = new Array();//已分配資源 var request = new Array();//請求資源數 var finish = new Array();//是否已完成 var safe = new Array();//安全序列 var fg = false; //更新Available標志 var o = 0;//動態創建表格(第一個表格) function CreateTable(){var tabletext = "";tabletext = "</br>系統資源的總數依次是:";for(i=0;i<num_resource;i++){tabletext += " " + available[i] + "????";}tabletext += "<p><p/><hr/>";tabletext += "請輸入各個進程的最大需求數(Max)和已分配數(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>進程</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; }//創建安全表格 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>進程</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(); }//更新安全表格(第二個表格) 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]];} }//點擊第一個按鈕 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,"請輸入進程數:");ChickNull(num_resource,"請輸入資源數:");if(isNaN(num_process&&num_resource)){alert("請輸入數字!");return;}alert(num_process+"個進程"+num_resource+"個資源");for(i=0;i<num_resource;i++){available[i] = window.prompt("第"+(i+1)+"個資源總數:");ChickNull(available[i],"請輸入資源總數:");if(isNaN(available[i])){alert("請輸入數字!");return;}}CreateTable();document.getElementById("d_display").style.display = ""; }//點擊第二個按鈕 function onClickOK2() {GetInfo();ChickSequence();PrintSequence("outputlist"); }//獲得填充數據 function GetInfo() {//獲取最大資源數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],"請輸入最大資源數:");if(isNaN(max[i][j])){alert("請輸入數字!");return;}} }//獲取已分配資源數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],"請輸入已分配資源數:");if(isNaN(allocation[i][j])){alert("請輸入數字!");return;}} } }//得到并填充Need function GetNeed() {//計算各進程對個資源的需求量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) {//計算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("請求失敗!無可利用資源");return false;}}}}else{if(available[i]<0){alert("請求失敗!無可利用資源");return false;}else{}}//填充Availablefor(i=0;i<num_resource;i++){document.getElementById("e"+0+"3"+i).value = available[i]; }return true; }//新請求資源 function Banker() {fg = true;var v1 = parseInt(window.prompt("請輸入第幾個進程請求資源"));for(i=0;i<num_process;i++){request[i] = new Array();}for(j=0;j<num_resource;j++){request[v1-1][j] = window.prompt("進程P"+(v1-1)+"請求資源"+String.fromCharCode((65+j))+"數量:");ChickNull(request[v1-1][j],"請輸入進程所請求資源數:");if(isNaN(request[v1-1][j])){alert("請輸入數字!");return;}}for(j=0;j<num_resource;j++){if(request[v1-1][j]>need[v1-1][j]){alert("請求資源數大于所需最大值,失敗!");return;}else if(request[v1-1][j]>available[j]){alert("請求資源數大于可利用資源量,請等待!");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/>對不起,該資源狀態不安全!";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; }



轉載于:https://www.cnblogs.com/Toring/p/6628304.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。