【正文】
algorithm can be used for the banking system because of its cash loans. Bankers algorithm is advancing in the premise of ensuring the system security. The first is that security check to process requests, to determine the allocation of resources or not, so as to ensure the safety of the system, to avoid deadlock. This paper on the understanding and analysis of the essential meaning of bankers algorithm as well as the state of the core idea, the algorithm is implemented in the overall design, including the module design of algorithm, and the algorithm of each module through a flow chart, block coding, and testing, the final program in the test, the design ideas on strictly according to the thought of software engineering implementation, to ensure that the design and implementation of feasible, credible. Keywords: bankers algorithm。 銀行家算法避免死鎖的研究與實(shí)現(xiàn) 12 6 結(jié)論 銀行家算法避免死鎖的研究與實(shí)現(xiàn)作為我的畢業(yè)設(shè)計(jì)。 圖 10 安全模塊 Safety_Algorithm 的調(diào)試的安全狀態(tài) 圖 11 安全模塊 Safety_Algorithm 的 調(diào)試的不安全狀態(tài) ?試分配后的安全算法,結(jié)果如果輸出一個(gè)安全性序列,并且經(jīng)過人工檢查該安全性序列,確實(shí)有效,則該模塊正確,如圖 12 所示;否則,之前的試分配作廢,恢復(fù)試分配前的資源狀態(tài)。 開始 Int i。 Y N Y N 開始 結(jié)束 初始化 work, finish。如圖 5 所示。 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì) typedef struct my_process { int num; int Max[M]; int Allocation[M]; int Need[M]; struct my_process* next; }process; int Available[M]={0}; int Request[M]={0}; int Record_work[N][M]={0}; int Safety[N]={0}; 算法整體設(shè)計(jì)與調(diào)用 主函數(shù) void main() 主要分四大塊: ( 1)首先需要初始化 Init_process( process **head, int m, int* count), 存儲系統(tǒng)當(dāng)前狀態(tài)信息; ( 2)調(diào)用安全算法 Safety_Algorithm, 檢測當(dāng)前系統(tǒng)安全狀態(tài), 若安全則進(jìn)行下一步,否則打印相關(guān)信息,程序退出; ( 3)調(diào)用試分配函數(shù) Attempt_Allocation, 進(jìn)行試分配,若試分配成功,修改相關(guān)數(shù)據(jù)結(jié)構(gòu),打印當(dāng)前系統(tǒng)資源分布圖,轉(zhuǎn)下一步。因?yàn)樗枰馁Y源數(shù)已經(jīng)超得過最大值,否則判斷 Request[j], Available[j]。 圖 1 數(shù)據(jù)流模型 試分配 安全性檢查 輸入信息 輸出結(jié)果 安全性檢查 初始化 銀行家算法避免死鎖的研究與實(shí)現(xiàn) 4 3 概要設(shè)計(jì) 模塊的劃分 由于該算法規(guī)模較小,所以選用結(jié)構(gòu)化的設(shè)計(jì)方法,將該系統(tǒng)劃為四塊,分別是: ( 1)主模塊,處在整個(gè)系統(tǒng)的最高層,負(fù)責(zé)組織調(diào)用其他模塊; ( 2)初始化模塊,負(fù)責(zé)從鍵盤讀入系統(tǒng)資源和進(jìn)程狀態(tài),并將系統(tǒng)初識資源分配狀態(tài)打?。? ( 3)試分配模塊,負(fù)責(zé)處理進(jìn)程請求和相應(yīng)的數(shù)據(jù)結(jié)構(gòu)的修改,以及特殊情況的處理; ( 4)安全性檢查,負(fù)責(zé)試分配后的安全性檢查,以及系統(tǒng)不安全時(shí)的資源恢復(fù)。若超過則拒絕分配資源,若沒有超過則再測試系統(tǒng)現(xiàn)存的資源能否滿足該進(jìn)程尚需的最大資源量,若能滿足則按當(dāng)前的申請量分配資源,否則也要推遲分配。 要想避免死鎖,就必須考慮進(jìn)程是否處于安全狀態(tài),只要處于安全狀態(tài)就可以避免死鎖。 ( 3)檢測死鎖。很顯然,如果沒有外力的作用,那么死鎖涉及到的各個(gè)進(jìn) 程都將永遠(yuǎn)處于封鎖狀態(tài)。對進(jìn)程請求先進(jìn)行安全性檢查,來決定資源分配與否,從而確保系統(tǒng)的安全,有效的避免了死鎖的發(fā)生。如此,尋求一種避免死鎖的方法便顯得很重要。目前,預(yù)防死鎖的方法可歸結(jié)為以下兩種: ( 1)預(yù)防死鎖。然后,采取適當(dāng)?shù)氖侄?,將死鎖清除掉。 銀行家算法 銀行家算法是最具有代表性的避免死鎖的算法,是由于該算法能用于銀行系統(tǒng)現(xiàn)金貸款的發(fā)放而得名。 問題的關(guān)鍵在于安全性算法,即查找安全性序列。它們的功能介紹如下:Flag1: 試分配模塊 Attempt_Allocation 與安全性檢查 Safety_Algorithm 之間接口 Attempt_Allocation 通過檢查 flag 的真假了判斷是否執(zhí)行。檢查此次分配后系統(tǒng)是否處于安全狀態(tài)。 長治學(xué)院學(xué)士學(xué)位論文(設(shè)計(jì)) 7 程序流圖 (1)系統(tǒng)以及進(jìn)程資源初始化 Init_process 的程序流程圖 首先,讀入當(dāng)前系統(tǒng)可用資源;然后,讀入進(jìn)程資源,建立進(jìn)程鏈表,輸入1 結(jié)束初始化;最后,打印當(dāng)前系統(tǒng)資源分配表。 輸入數(shù)據(jù) Break; [0]!=1 1 銀行家算法避免死鎖的研究與實(shí)現(xiàn) 8 圖 6 試分配的程序流程圖 圖 5 安全性算法的程序流程圖 (3)接受進(jìn)程請求,試分配的程序流程圖 首先, p 指針指向弧的結(jié)點(diǎn)。 i=0。如圖 8 所示。 ( 1) 06: 結(jié)果: 無此進(jìn)程。首先需要初識化系統(tǒng)資源;其次,安全性檢查;再者,試分配;最后是試分配后的安全性檢查。 secure sequence 銀行家算法避免死鎖的研究與實(shí)現(xiàn) 14 致謝 在論文的寫作過程中遇到了無數(shù)的困難和障礙,都在同學(xué)和老師的幫助下度過了。 struct my_process* next。 } else { last=*head。 printf(最大需求矩陣 : )。 for(i=0。[i])。 p=head。A39。jm。jm。 } puts()。 return p。 銀行家算法避免死鎖的研究與實(shí)現(xiàn) 20 if(p==NULL) { printf(無此進(jìn)程 !\n)。im。 return NULL。 process* p=NULL。 } } } else { i++。 } int Safety_Algorithm(process* head,int *avail,int *safety,int Record[][M],int m,int n) { int *work=NULL。 p=head。 } i=0。 break。 finish=NULL。 pNeed[i]+=request[i]。 printf(\t)。 } printf(\t)。j++) { printf(%4d,work[pnum][j])。j++) { printf(%4d,pAllocation[j])。 } } puts()。 process *head=NULL。Available[i])。 Print(head,Available,M)。N39。)。 scanf(%c,amp。ch