死锁
两个或者两个以上的线程在执行过程中由于竞争资源而造成的阻塞问题,若无外力作用,他们将无法推进下去,此时系统处于死锁状态。
安全序列
安全序列是指对当前申请资源的进程排出一个序列,保证按照序列分配资源完成进程,不会发生死锁问题
我们假设有进程P1,P2,…Pn
则安全序列要求满足:Pi(1<=i<=n)需要资源<=剩余资源 + 分配给Pj(1 <= j < i)资源

示例

Process Allocation Need Available
P0 0 0 3 2 0 0 1 2 1 6 2 2
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 3 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6

采用银行家算法

Process Work Allocation Need Available
P0 1 6 2 2 0 0 3 2 0 0 1 2 1 6 5 4
P3 1 6 5 4 0 3 3 2 0 6 5 2 1 9 8 6
P4 1 9 8 6 0 0 1 4 0 6 5 6 1 9 9 10
P1 1 9 9 10 1 0 0 0 1 7 5 0 2 9 9 10
P2 2 9 9 10 1 3 5 4 2 3 5 6 3 12 14 14

我们来分析一下银行家算法
现有资源为 1 6 5 4,P0 线程需要0 0 1 2,足够分配给它,我们分配给线程P0,等到P0 运行完回收资源,Avaliable 为1 6 5 4。
然后线程P1,需要资源1 7 5 0,Avaliable < Need,不分配
线程P2 需要资源2 3 5 6,Avaliable < Need,不分配
线程P3 需要资源0 6 5 2,分配给它然后回收资源,Avaliable 为1 9 8 6
线程P4 需要资源0 6 5 6,分配给它然后回收资源,Avaliable 为1 9 9 10
返回继续判断P1,需要资源1 7 5 0,分配给它然后回收资源,Avaliable 为2 9 9 10
然后P2,需要资源2 3 5 6,分配给它然后回收资源,Avaliable 为3 12 12 14

用java来实现一下银行家算法

package com.os;
import java.util.Scanner;/*** Description:v银行家算法** @author:仙女萌* @Date 2018/12/29 16:32*/
public class BankerAlgorithm {private static Scanner scanner = new Scanner(System.in);//资源数量private int resource;//线程数量private int thread;//资源总数量private int max[];//剩余资源数量private int avilable[];//最大需求矩阵private int[][] applymax;//已分配资源数量private int[][] allocation;//还需要资源是数量private int need[][];boolean F[];/*** 一维数组的输入* @param n* @return*/private int[] inputone(int n){int[] number = new int[n];for(int i = 0;i < resource;i++){number[i] = scanner.nextInt();}return number;}/*** 二维数组的输入* @param m* @param n* @return*/private int[][] inputtwo(int m,int n){int[][] number = new int[m][n];for(int i = 0;i < m;i++){System.out.print("进程p"+i+":");for(int j = 0;j < n;j++){number[i][j] = scanner.nextInt();}}return number;}/*** 数组的初始化*/public void init(){System.out.print("请输入资源数量:");resource = scanner.nextInt();System.out.print("请输入线程数量:");thread = scanner.nextInt();System.out.print("请输入最大资源数量:");max = inputone(resource);System.out.println("请输入每个线程最大申请资源总数");applymax = inputtwo(thread,resource);System.out.println("请输入资源分配情况");allocation = inputtwo(thread,resource);//剩余资源avilable = max.clone();for(int i = 0;i < resource;i++){//资源个数for(int j = 0;j < thread;j++){//线程个数avilable[i] -= allocation[j][i];}}need = new int[thread][resource];//还需要资源need = apply-allocationfor(int i = 0;i < thread;i++){for(int j = 0;j < resource;j++){need[i][j] = applymax[i][j] - allocation[i][j];}}F = new boolean[thread];show();System.out.println("安全性检查");int[] work = avilable.clone();safe(work,allocation,need);}/*** 输出*/private void show(){int[] work = avilable.clone();//最大资源数量System.out.print("资源总数:");for(int i = 0; i < resource; i++){System.out.print(max[i]+"  ");}System.out.println();System.out.print("可利用资源向量:");for(int j = 0;j < resource;j++){System.out.print(work[j]+" ");}System.out.println();System.out.println("NAME"+"\t\t"+"MAX"+"\t\t"+"ALLOCATION1"+"\t\t"+"NEED");for(int i = 0 ;i < thread;i++){//线程System.out.print("p"+i+"\t\t\t");for(int j = 0;j <resource;j++){System.out.print(applymax[i][j]+" ");}System.out.print("\t\t");for(int j = 0;j < resource;j++){System.out.print(allocation[i][j]+" ");}System.out.print("\t\t");for(int j = 0;j <resource;j++){System.out.print(need[i][j]+" ");}System.out.println();}}/*** 安全性算法* @return*/public boolean safe(int[] work,int[][] allocation,int[][] need){boolean s = true;int count = 0;int count1 = -1;for(int i = 0;i <thread;i++){F[i] = false;}// System.out.println("还原finish");System.out.println("name"+"\t\t"+"WORK"+"\t\t"+"NEED"+"\t\t"+"ALLOCATION"+"\t\t\t"+"WORK+ALLOCATION"+"\t\t"+"FINISH");int way[] = new int[thread];for(int i = 0;i < thread;i++) {s = true;//判断资源是否足够分配for (int j = 0; j < resource; j++) {if (work[j] < need[i][j]) {//avaliable <needs = false;break;}}// System.out.println(i+"检查完毕  s =="+s+" "+"F[i] ==" +F[i]);//资源足够分配,那么就分配资源if (s == true && F[i] == false) {F[i] = true;way[count++] = i;System.out.print("p"+i+"\t\t\t");for (int j = 0; j < resource;j++) {System.out.print(work[j]+" ");}System.out.print("\t\t");for (int j = 0; j < resource; j++) {System.out.print(need[i][j]+" ");}System.out.print("\t\t");for (int j = 0; j < resource; j++) {System.out.print(allocation[i][j]+" ");}System.out.print("\t\t\t\t");for (int j = 0; j < resource; j++) {work[j] = work[j] + allocation[i][j];System.out.print(work[j]+" ");}System.out.print("\t\t\t\t");System.out.println(F[i]);}/*** 如果循环一遍没有任何的线程被分配资源,那么这个线程是不安全的*/if (i == thread - 1 && count == count1) {System.out.println("该线程不安全");return false;}/*** 从头再继续循环*/if (count < thread && i == thread - 1) {// System.out.println("count < thread && i == thread - 1");count1 = count;i = -1;}if (count == thread) {System.out.print("该线程的安全序列为:");for (int j = 0; j < thread; j++) {System.out.print(way[j]+"  ");}System.out.println();return true;}}return false;}public boolean update(int p,int[] request){for(int i = 0;i < resource;i++){if(request[i] > need[p][i]){System.out.println("线程不安全");return false;}if(request[i] > avilable[i]){System.out.println("线程不安全");return false;}}return true;}/*** 预分配*/public void preAll(){boolean b = true;// 预分配资源System.out.print("请选择进程申请资源: p");int p = scanner.nextInt();int[] request = new int[resource];System.out.print("申请资源数:");request = inputone(resource);//需要请求的资源int[] work1 = avilable.clone();int [][] all = allocation.clone();int n[][] = need.clone();int[] work2 = avilable.clone();/*** 输入合法*/if(update(p,request) == true){//预分配for(int i = 0;i < resource;i++){work1[i] = work1[i] - request[i];all[p][i] = all[p][i] + request[i];n[p][i] =  n[p][i] - request[i];if(n[p][i] != 0){//数据不为0b = false;}}if(b == true){//进程可以运行完毕for(int i = 0;i < resource;i++){work1[i] = all[p][i]+work1[i];all[p][i] = 0;}}}work2 = work1.clone();//public boolean safe(int[] work,int[][] allocation,int[][] need){if(safe(work2,all,n)){//安全性检查//线程安全2avilable= work1.clone();allocation = all;need = n;}show();}public void menu(){while(true){System.out.print("是否预分配(Y/N):");char ch = scanner.next().charAt(0);switch (ch){case 'Y':preAll();//预分配break;case 'N':System.out.println("退出系统");System.exit(0);break;default:System.out.print("输入不合法,重新输入:");break;}}}public static void main(String[] args) {BankerAlgorithm banker = new BankerAlgorithm();banker.init();banker.menu();}}

运行结果:

死锁避免——银行家算法相关推荐

  1. 【避免进程死锁】银行家算法

    一.概述 银行家算法(Banker's Algorithm)是一个避免进程死锁的著名算法,由 Dijkstra 于 1965 年提出.本文为笔者的读书笔记,结构如下: 死锁 银行家算法 例子展示 补充 ...

  2. 皮卡丘忠实粉丝之Web实现操作系统实验(进程调度+存储管理+死锁避免银行家算法)

    **皮卡皮卡丘~~~~~~** 目录 进程调度 目的和要求 内容与步骤 运行结果 问题及心得 C语言实现代码 存储管理 目的和要求 内容与步骤 运行结果 问题及心得 C语言实现代码 死锁避免银行家算法 ...

  3. 操作系统-进程死锁:银行家算法

    文章目录 进程死锁:银行家算法 问题描述 实验环境 输入 输出 测试数据 实验设计 数据结构 主要函数功能和参数 系统框架图 流程图 实验结果与分析 结果展示与描述 结果分析 总结 源代码 进程死锁: ...

  4. 避免死锁: 银行家算法

    避免死锁: 银行家算法 1.背景 在银行中,客户申请贷款的数量是有限的,每个客户在第一次申请贷款时要声明完成该项目所需的最大资金量,在满足所有贷款要求时,客户应及时归还.银行家在客户申请的贷款数量不超 ...

  5. 避免死锁的银行家算法

    死锁的定义> 如果一组进程中的每一个进程都在等待仅由该组进程中的其他进程才能引发的事件,那仫该组进程就是死锁的. 产生死锁的必要条件> 1).互斥条件:进程对所分配到的资源进行排它性使用, ...

  6. 《操作系统》实验四:预防进程死锁的银行家算法

    [实验题目]:预防进程死锁的银行家算法 [实验学时]:4学时 [实验目的] 通过这次实验,加深对进程死锁的理解,进一步掌握进程资源的分配.死锁的检测和安全序列的生成方法. [实验内容] 问题描述: 设 ...

  7. python银行家算法代码_避免死锁的银行家算法C++程序实现

     本篇博文为追忆以前写过的算法系列第二篇(20081021) 温故知新 目的:具有代表性的死锁避免算法是Dijskstra给出的银行家算法.本实验是基于银行家算法的思想通过编写C++程序实现银行家 ...

  8. 二十三、死锁的处理策略---避免死锁(银行家算法)

    一.知识总览 二.什么是安全序列 **所谓安全序列:**就是指如果系统按照这种序列分配资源,则每个进程都能顺利完成,只要能找出一个安全序列,系统就是安全状态.当然,安全序列可能有多个. 如果分配了资源 ...

  9. 操作系统中避免死锁的银行家算法【表面C++实际C语言】一学就废的菜鸡代码

    文章目录 银行家算法 实验原理 数据结构 初始化 输出资源分配量 安全性算法 银行家算法 完整代码 测试数据 测试结果 第一题 第二题 银行家算法 银行家算法是一种最有代表性的避免死锁的算法.在避免死 ...

  10. 操作系统之进程管理:19、死锁的处理策略:避免死锁(银行家算法)

    19.死锁的处理策略:避免死锁 思维导图 安全序列 例 银行家算法实现步骤 银行家算法的实现 思维导图 安全序列 来看这样的情况: 由上述表格可知,B仍需50,A仍需30,T仍需20,我手中剩余40 ...

最新文章

  1. 2022-2028年全球与中国青苔清洗剂市场研究及前瞻分析报告
  2. Expression Tree 上手指南 (二)
  3. 协程的概念及Python中利用第三方库gevent使用协程
  4. 用前序和中序重建二叉树 python
  5. 【Leetcode】组合、排列、子集、切割(回溯模板和去重方法)
  6. 《数据库系统实训》实验报告——子查询与组合查询
  7. notepad++插件实现json、xml格式化
  8. MYSQL jdbc autoReconnect
  9. 栈的顺序存储及实现(一)
  10. Arcgis for android 100.4 getFieldType ()
  11. openCV2.4.13+VS2015+Cmake开发环境配置,解决nonfree问题
  12. 最近邻插值算法 python实现
  13. Tensor flow 实战Google深度学习框架 笔记摘要Pfour
  14. github 思维导图开元软件_最强大脑!这 7 款开源思维导图工具真的很神奇
  15. nand flash 读写测试
  16. 用WPF做报表控件(一)
  17. 微积分公式与运算法则
  18. python高级教程_Python高级进阶教程
  19. 【Java】Java基础
  20. dll注入失败原因总结

热门文章

  1. 使用ping测试MTU值
  2. 56 案例淘宝焦点图布局 网页布局总结
  3. HTML+CSS+JS网页设计期末课程大作业 web前端开发技术 web课程设计 html网页规划与设计
  4. 用Javascript实现关闭广告案例
  5. 渐变色按钮功能按钮绘制C语言示例
  6. [教程资源] HTC Vive UI Guideline
  7. 【51单片机】外部中断
  8. HTML5期末大作业:网站——美丽家乡(武汉汉口)
  9. 青海行--(7月28日)凯旋归程
  10. 工作经费的开支范围_经费开支范围