Banker's algorithm


Banker's algorithm

The Banker's algorithm is a resource allocation & deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of pre-determined maximum possible amounts of all resources, and then makes a "safe-state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue.

The algorithm was developed in the design process for the THE operating system and originally described (in Dutch) in EWD108 [E. W. Dijkstra " [http://www.cs.utexas.edu/users/EWD/ewd01xx/EWD108.PDF EWD108: Een algorithme ter voorkoming van de dodelijke omarming] " (in Dutch; "An algorithm for the prevention of the deadly embrace")] . The name is by analogy with the way that bankers account for liquidity constraints.

Algorithm

The Banker's algorithm is run by the operating system whenever a process requests resources. [cite book
last = Lubomir
first = F. Bic
coauthor = Alan C. Shaw
title = Operating System Principles
publisher = Prentice Hall
date = 2003
url = http://vig.prenhall.com/catalog/academic/product/0,1144,0130266116,00.html
ID = ISBN 0-13-026611-6
] The algorithm prevents deadlock by denying or postponing the request if it determines that accepting the request could put the system in an unsafe state (one where deadlock could occur).

Resources

For the Banker's algorithm to work, it needs to know three things:
*How much of each resource each process could possibly request
*How much of each resource each process is currently holding
*How much of each resource the system has available

Some of the resources that are tracked in real systems are memory, semaphores and interface access.

Example

Assuming that the system distinguishes between four types of resources, (A, B, C and D), the following is an example of how those resources could be distributed. "Note that this example shows the system at an instant before a new request for resources arrives. Also, the types and number of resources are abstracted. Real systems, for example, would deal with much larger quantities of each resource."

Safe and Unsafe States

A state (as in the above example) is considered safe if it is possible for all processes to finish executing (terminate). Since the system cannot know when a process will terminate, or how many resources it will have requested by then, the system assumes that all processes will eventually attempt to acquire their stated maximum resources and terminate soon afterward. This is a reasonable assumption in most cases since the system is not particularly concerned with how long each process runs (at least not from a deadlock avoidance perspective). Also, if a process terminates without acquiring its maximum resources, it only makes it easier on the system.

Given that assumption, the algorithm determines if a state is safe by trying to find a hypothetical set of requests by the processes that would allow each to acquire its maximum resources and then terminate (returning its resources to the system). Any state where no such set exists is an unsafe state.

Pseudo-Code [ [http://www.cs.huji.ac.il/course/2006/os/notes/notes4.pdf Concurrency] ]

P - set of processes

Mp - maximal requirement of resources for process p

Cp - current resources allocation process p

A - currently available resources

while (P != ∅) { found = FALSE; foreach (p ∈ P) { if (Mp − Cp ≤ A) { /* p can obtain all it needs. */ /* assume it does so, terminates, and */ /* releases what it already has. */ A = A + Cp ; P = P − {p}; found = TRUE; } } if (! found) return FAIL; } return OK;

Example C Program To Implement Banker's Algorithm

The following Link Contains a C Program to implement the above pseudocode. It is written under GNU/Linux environment. [http://thamirabarani.org/Resources/bankers.c Download: 'C Program for Bankers algorithm']

Example

We can show that the state given in the previous example is a safe state by showing that it is possible for each process to acquire its maximum resources and then terminate.
#P1 acquires 2 A, 1 B and 1 D more resources, achieving its maximum
#*The system now still has 1 A, no B, 1 C and 1 D resource available
#P1 terminates, returning 3 A, 3 B, 2 C and 2 D resources to the system
#*The system now has 4 A, 3 B, 3 C and 3 D resources available
#P2 acquires 2 B and 1 D extra resources, then terminates, returning all its resources
#*The system now has 5 A, 3 B, 6 C and 6 D resources
#P3 acquires 4 C resources and terminates
#*The system now has all resources: 6 A, 4 B, 7 C and 6 D
#Because all processes were able to terminate, this state is safeNote that these requests and acquisitions are "hypothetical". The algorithm generates them to check the safety of the state, but no resources are actually given and no processes actually terminate. Also note that the order in which these requests are generated – if several can be fulfilled – doesn't matter, because all hypothetical requests let a process terminate, thereby increasing the system's free resources.

For an example of an unsafe state, consider what would happen if process 2 were holding 1 more unit of resource B at the beginning.

Requests

When the system receives a request for resources, it runs the Banker's algorithm to determine if it is safe to grant the request. The algorithm is fairly straight forward once the distinction between safe and unsafe states is understood.
#Can the request be granted?
#*If not, the request is impossible and must either be denied or put on a waiting list
#Assume that the request is granted
#Is the new state safe?
#*If so grant the request
#*If not, either deny the request or put it on a waiting list"Whether the system denies or postpones an impossible or unsafe request is a decision specific to the operating system."

Example

Continuing the previous examples, assume process 3 requests 2 units of resource C.
#There is not enough of resource C available to grant the request
#The request is denied On the other hand, assume process 3 requests 1 unit of resource C.
#There are enough resources to grant the request
#Assume the request is granted
#*The new state of the system would be: Available system resources A B C D Free 3 1 0 2

Processes (currently allocated resources): A B C D P1 1 2 2 1 P2 1 0 3 3 P3 1 1 2 0

Processes (maximum resources): A B C D P1 3 3 2 2 P2 1 2 3 4 P3 1 1 5 0

#Determine if this new state is safe
##P1 can acquire 2 A, 1 B and 1 D resources and terminate
##Then, P2 can acquire 2 B and 1 D resources and terminate
##Finally, P3 can acquire 3 C resources and terminate
##Therefore, this new state is safe
#Since the new state is safe, grant the request Finally, assume that process 2 requests 1 unit of resource B.
#There are enough resources
#Assuming the request is granted, the new state would be: Available system resources: A B C D Free 3 0 1 2

Processes (currently allocated resources): A B C D P1 1 2 2 1 P2 1 1 3 3 P3 1 1 1 0

Processes (maximum resources): A B C D P1 3 3 2 2 P2 1 2 3 4 P3 1 1 5 0

#Is this state safe? Assuming P1, P2, and P3 request more of resource B and C.
#*P1 is unable to acquire enough B resources
#*P2 is unable to acquire enough B resources
#*P3 is unable to acquire enough C resources
#*No process can acquire enough resources to terminate, so this state is not safe
#Since the state is unsafe, deny the request"Note that in this example, no process was able to terminate. It is possible that some processes will be able to terminate, but not all of them. That would still be an unsafe state."

Trade-offs

Like most algorithms, the Banker's algorithm involves some trade-offs. Specifically, it needs to know how much of each resource a process could possibly request. In most systems, this information is unavailable, making the Banker's algorithm useless. Besides, it is unrealisticto assume that the number of processes is static. In most systems the number of processes varies dynamically. Moreover, the requirement that a process will eventually release all its resources (when the process terminates) is sufficient for the correctness of the algorithm, however it is not sufficient for a practical system. Waiting for hours (or evendays) for resources to be released is usually not acceptable.

References

Further reading

* " [http://codex.cs.yale.edu/avi/os-book/os7 Operating System Concepts] " by Silberschatz, Galvin, and Gagne (pages 259-261 of the 7th edition)
* " [http://www.cs.utexas.edu/users/EWD/ewd06xx/EWD623.PDF EWD623: The mathematics behind the Banker’s Algorithm] " (1977) by E. W. Dijkstra, published as pages 308–312 of Edsger W. Dijkstra, "Selected Writings on Computing: A Personal Perspective", Springer-Verlag, 1982. ISBN 0-387-90652-5

External links

* [http://www.isi.edu/~faber/cs402/notes/lecture9.html Deadlock Recovery, Avoidance and Prevention]
* [http://algoritmo-del-banquero.veer.com.ar Algoritmo del Banquero] (In Spanish)


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Banker's Algorithmus — Der Bankieralgorithmus (englisch Banker s algorithm) geht auf Edsger W. Dijkstra (1965) zurück und wird zur Vermeidung von Verklemmungen (deadlock) genutzt. Dazu werden die verfügbaren Ressourcen und die Prozesse aufgelistet. Die Ressourcen… …   Deutsch Wikipedia

  • Ostrich algorithm — For other uses, see ostrich (disambiguation). In computer science, the ostrich algorithm is a strategy of ignoring potential problems on the basis that they may be exceedingly rare to stick your head in the sand and pretend that there is no… …   Wikipedia

  • Deadlock — This article is about the computer science concept. For other uses, see Deadlock (disambiguation). A deadlock is a situation where in two or more competing actions are each waiting for the other to finish, and thus neither ever does. It is often… …   Wikipedia

  • List of algorithms — The following is a list of the algorithms described in Wikipedia. See also the list of data structures, list of algorithm general topics and list of terms relating to algorithms and data structures.If you intend to describe a new algorithm,… …   Wikipedia

  • Список алгоритмов — Эта страница информационный список. Основная статья: Алгоритм Ниже приводится список алгоритмов, группированный по категориям. Более детальные сведения приводятся в списке структур данных и …   Википедия

  • Edsger W. Dijkstra — Edsger Wybe Dijkstra Born May 11, 1930(1930 05 11) Rotterdam, Netherl …   Wikipedia

  • Strict two-phase locking — In computer science, strict two phase locking (Strict 2PL) is a locking method used in concurrent systems.The two rules of Strict 2PL are:# If a transaction T wants to read/write an object, it must request a shared/exclusive lock on the object. # …   Wikipedia

  • THE multiprogramming system — Infobox OS name = THE multiprogramming system caption = developer = Technische Hogeschool Eindhoven / Edsger Dijkstra (et al.) source model = kernel type = supported platforms = Electrologica X8 ui = family = released = 1968 latest release… …   Wikipedia

  • Bankier-Algorithmus — Der Bankieralgorithmus (englisch Banker s algorithm) geht auf Edsger W. Dijkstra (1965) zurück und wird zur Vermeidung von Verklemmungen (deadlock) genutzt. Dazu werden die verfügbaren Ressourcen und die Prozesse aufgelistet. Die Ressourcen… …   Deutsch Wikipedia

  • Bankieralgorithmus — Der Bankieralgorithmus (englisch Banker s algorithm) geht auf Edsger W. Dijkstra (1965) zurück und wird zur Vermeidung von Verklemmungen (deadlock) genutzt. Dazu werden die verfügbaren Ressourcen und die Prozesse aufgelistet. Die Ressourcen… …   Deutsch Wikipedia