You are on page 1of 17

1

BANKER’S
ALGORITHM
Group Members:

1. Talha Siddique (460)

2. Talha Sajjad Butt (471)

3. Muhammad Faisal Bashir (490)

4. Ifra Munir (488)

About us Session: Fa-18

Section: K

Subject: Operating System (LAB)

Topic: Banker’s Algorithm


Definition

The banker’s algorithm is a resource


allocation and deadlock avoidance
algorithm that tests for safety by
simulating the allocation for
predetermined maximum possible
Banker’s
amounts of all resources, then makes Algorithm
an “s-state” check to test for possible
activities, before deciding whether
allocation should be allowed to
continue.
• Banker's Algorithm is used majorly in the
banking system to avoid deadlock.

• This algorithm is used to test for safely


simulating the allocation for determining the
maximum amount available for all resources.
Why it is used • It also checks for all the possible activities before
determining whether allocation should be
continued or not.

• It handles multiples instances of same resource.


5

1. How many instances of


each resource each process
can maximum request.
Requirements 2. How many resources are
allocated to each process.
3. How many instances of
each resource is available
in the system.
• Available: Vector of length m. If available [j] =
k, there are k instances of resource type Rj
available

• Max: n x m matrix. If Max [i,j] = k, then process


Pi may request at most k instances of resource
type Rj

Data Structures used • Allocation: n x m matrix. If Allocation[i,j] = k


then Pi is currently allocated k instances of Rj

• Need: n x m matrix. If Need[i,j] = k, then Pi may


need k more instances of Rj to complete its task
• n = number of processes, and m = number of
resources types.
Allocation Max Available Need

ABCD AB CD AB CD AB CD
P0 0012 0012 1520 0000

Algorithm P1 1000 1750 1532 0750


P2 1354 2356 2886 1002
P3 0632 0652 2 14 11 8 0020
P4 0014 0656 2 14 12 12 0642
• Total resources in the system (Before
Execution) = Available resources + Total
allocated resources.
• Total resources in the system (After
Execution) = Sum of available resources.
• Need Matrix = Maximum resources –
Allocated resources
Key Points • The order of process termination s called Safe
Sequence.
• In the previous example the safe sequence
was:
1. P0, P2, P3, P4, P1
2. P0, P2, P3, P1, P4
• There can be more than one safe sequence.
• A, B, C, D are resource types.
• Input processes (P0, P1, P2, …)
• Max, Allocation, Need any of
What should be given them. In the previous example
Allocation and Max were given.
• Total number of resources.
10

1. Processes request only 1 resource at a


time.

2. Algorithm can be used for multiple


resource types.
Advantages 3. Avoids deadlock and it is less restrictive
than deadlock prevention.

4. System guarantees that processes will


be allocated resources within finite
time.
11

• It requires the number of processes to be


fixed; no additional processes can start
while it is executing.
• No resource may go down for any reason
Disadvantages without the possibility of deadlock
occurring.
• All processes must know and state their
maximum resource need in advance.
Code
Code
Code
Code
System is in safe state.
Output
Safe sequence is: 1, 3, 4, 0, 2
17

You might also like