博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
银行家算法(Banker's Algorithm)
阅读量:5037 次
发布时间:2019-06-12

本文共 2852 字,大约阅读时间需要 9 分钟。

•Multiple instances.
•Each process must a priori claim maximum use.
•When a process requests a resource it may have to wait.  
•When a process gets all its resources it must return them in a finite amount of time.

n为进程的数目,m为资源类型的数目

•Available:  Vector of length m.
–If available [j] = k, there are k instances of resource type Rj available.(如果available[j]=k,那么资源Rj有k个实例可用)
•Max: n x m matrix. 
–If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj.(如果Max[i,j]=k,那么进程Pi可以最多请求资源Rj的k个实例)

Data Structures for the Banker’s Algorithm (Cont.):

•Allocation:  n x m matrix. 
–If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.(如果Allocation[i,j]=k,那么进程Pi当前分配了k个资源Rj的实例)
•Need:  n x m matrix.
–If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.(如果Need[i,j]=k,那么进程Pi 还需要k个资源Rj的实例)

  Need [i,j] = Max[i,j] – Allocation [i,j].

 

Safety Algorithm(安全算法):

1.  Let Work and Finish be vectors of length m and n, respectively.  Initialize(让Work和Finish作为长度为m和n的向量)

Work := Available

Finish [i] = false fori - 1,3, …, n.

2.  Find an isuch that both: (找到i)

(a) Finish [i] = false

(b) Needi£Work

If no such iexists, go to step 4.

3.  Work := Work + Allocationi

Finish[i] := true
go to step 2.

If Finish [i] = true for all i, then the system is in a safe state.

Resource-Request Algorithm for Process Pi

•Requesti = request vector for process Pi. 

1.  If Requesti £ Needi go to step 2.  Otherwise, raise error condition, since process has exceeded its maximum claim.

2.  If Requesti £ Available, go to step 3.  Otherwise Pi  must wait, since resources are not available.

3.  Pretend to allocate requested resources to Pi by modifying the state as follows:假定给Pi 分配资源

  Available := Available - Requesti;

  Allocationi := Allocationi + Requesti;

  Needi := Needi – Requesti

4.用安全算法进行检查,看系统是否处于安全状态

•If safe Þ the resources are allocated to Pi.
•If unsafe Þ Pi must wait, and the old resource-allocation state is restored
 
Example of Banker’s Algorithm:
•5 processes P0 through P4; 3 resource types A (10 instances),
B (5instances), and C (7 instances).(5个进程P0-P4,3类资源A(10个实例),B(5个实例),C(7个实例))
•Snapshot at time T0:(时刻T0的片段)

   Allocation       Max       Available

     A B C          A B C        A B C

P0  0 1 0          7 5 3         3 3 2

P1  2 0 0          3 2 2 

P2  3 0 2          9 0 2

P3  2 1 1          2 2 2

P4  0 0 2          4 3 3 

•The content of the matrix. Need is defined to be Max – Allocation. ( Need矩阵的内容可以由Max – Allocation 计算出来)

  Need

        A B C

   P0  7 4 3

   P1  1 2 2

   P2  6 0 0

   P3  0 1 1

   P4  4 3 1 

•用安全检测算法看能否找到一个安全序列
–Work[]=available=(3,3,2)
–Finish[i]=false  (i=0..4)
–      
–       Work    need   allocation   finish
– P1   3 3 2    1 2 2    2 0 0       T
– P3   5 3 2    0 1 1    2 1 1       T
– P4   7 4 3    4 3 1    0 0 2       T
– P2   7 4 5    6 0 0    3 0 2       T
– P0  10 4 7    7 4 3    0 1 0       T
–存在安全序列:(P1,P3,P4,P2,P0)
 

注意两点:

1、通常,安全序列不唯一;

2、安全序列中的选取,总是将work中的资源首先分给need(Pi)<=work且max{allocation(Pi)}的进程,剩下的依次类推分配

 

 

转载于:https://www.cnblogs.com/cpoint/archive/2012/11/18/2776539.html

你可能感兴趣的文章
Django学习笔记
查看>>
03-THREE.JS GUI使用
查看>>
Python os.path.join 双斜杠的解决方法
查看>>
高并发下线程安全的单例模式
查看>>
Windows下修改Git bash的HOME路径(转)
查看>>
第三章 TCP/IP
查看>>
【cocos2d-x制作别踩白块儿】第一期:游戏介绍
查看>>
发现的最大数量
查看>>
Ubuntu12.04环境搭建遇到的问题和建议(一个)
查看>>
19.最经济app发短信的方法
查看>>
从零開始学android&lt;SeekBar滑动组件.二十二.&gt;
查看>>
教你用笔记本破解无线路由器password
查看>>
网络编程学习小结
查看>>
JS面向对象
查看>>
excel VLOOKUP函数的用法
查看>>
设计模式
查看>>
orm介绍
查看>>
一个简单程序快速入门JDBC
查看>>
DBA_Oracle基本体系内存和进程结构(概念)
查看>>
unisynedit 在Delphi 2010下的编译问题
查看>>