Banker Algorithm Example

Source: Internet
Author: User

  On the Internet, I found a few blog posts about the Banker's algorithm, Mark down. The author is still good, easy to understand, more practical than simply speaking meaning.

Turn from:

Mushang's Blog

Recently began to prepare for the computer four, basically did not encounter any problems, the feeling of computer four is the examination of the understanding of memory, the problem of the banker algorithm is in the computer four a few computational problems.

Example 1. There are three types of resources (A,B,C) and five processes (P1,P2,P3,P4,P5) in the system, the number of a resources is the number of 17,B resources is the number of 6,c resources is 19. The state of the system at the T0 moment is as follows:

Maximum resource demand

Amount of allocated resources

A,b,c

A,b,c

P1

4,0,11

4,0,5

P2

5,3,6

4,0,2

P3

4,2,5

2,1,4

P4

5,5,9

2,1,2

P5

4,2,4

3,1,3

The system uses the banker algorithm to implement the deadlock avoidance strategy, if the current system remaining resources (A,B,C) are (2,3,3), which of the following sequence is a safe sequence?

A.p3,p1,p4,p2,p5

B.p1,p3,p5,p2,p4

C.p4,p2,p3,p5,p1

D.p2,p3,p1,p4,p5

Almost all the data on the single-choice question of the Banker's algorithm is similar, before solving the problem, to explain what a safe sequence is. A security sequence is a process sequence {P1,...,PN} is secure, that is, for each process pi (1≤i≤n), it does not need to exceed the amount of resources currently remaining in the system with all processes PJ (J < i) currently occupies the same amount of resources. This sentence is more abstract, in the process of interpretation we understand.

Analysis: We first calculate the amount of resources required for each process, the amount of resources required = The maximum resource demand-the amount of allocated resources , thus the amount of resources required for each process to be

P1 P2 P3 P4 P5
A,b,c 0,0,6 1,3,4 2,1,1 3,4,7 1,1,1

Next, we analyze the option a,p3 the amount of resources required (2,1,1), and the remaining resources of the system are (2,3,3), so the process P3 process can be completed, and release the amount of resources it occupies, that is, the amount of resources allocated, at this time the system remaining resources is (2+2,3+1,3+4) = (4,4,7), (4,4,7) is greater than (or equal to) the amount of resources that are required for all other processes (that is, P1,P1,P2,P4,P5) (note that "greater than" here refers to the A,b,c three categories are greater than), so the sequence of option A is a safe sequence .

Option B:P1 requires a resource of (0,0,6), obviously the remaining resources of the system do not meet the conditions, B is the restless whole sequence.

Option C:P4 requires a resource of (3,4,7), apparently the remaining resources of the system do not meet the conditions, C is the restless full sequence.

The option D:P2 requires a resource of (1,3,4), apparently the system's remaining resources meet the conditions, and D is the restless full sequence.

Almost all of the banker's algorithm single-choice can use the above method of a second shot, of course, this is only a prelude to the computer four, in a single choice, we can easily choose the answer by the elimination method, but the computer four is a multi-choice, multi-choice banker algorithm may have to spend more than a minute to determine the correct options, Be careful and careful, but your head may go wrong. Here is a multi-choice about the banker algorithm, feel it, is so easy.

Example 2. The current resource allocation for an operating system is shown in the following table.

Process

Maximum Resource requirements

Number of allocated resources

R1 R2 R3

R1 R2 R3

P1

7 5 3

0 1 0

P2

3 2 2

2 0 0

P3

9 0 2

3 0 2

P4

2 2 2

2 1 1

P5

4 3 3

0 0 2

Assuming that the current system available resources R1, R2, and R3 are in (3,3,2), and that the system is currently in a secure state, which of the following are safe sequences?

A.p2p4p5p1p3

B.p2p4p5p3p1

C.p4p1p2p3p5

D.p3p2p5p4p1

E.p4p2p3p5p1

Parsing: see Example 1 for a method.

Answer: ABE

On the development of the Banker's algorithm: (The following is the "National computer level test four-level tutorial-Operating system Principle" original)

The banker algorithm determines how to allocate resources by dynamically detecting the resource allocation situation and the demand of the process in the system, and allocates the resources to the applicant when it can ensure the system is in a safe state, thus avoiding the deadlock of the system. Because the banker algorithm is implemented during the system operation, it takes a considerable amount of time, the algorithm needs MXN2 operation. The banker algorithm requires that the number of resources per class be fixed, and that the maximum demand for resources must be known beforehand, which is difficult to achieve. The insecure state is not necessarily a deadlock state, and if the resource requested by a process is currently available, the process must wait for the resource utilization to decrease.

Banker algorithm Example (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.