Lab10 21052024 115257pm
Lab10 21052024 115257pm
LAB # 10
Inter Process Communication II
Deadlock
In a multiprogramming environment, several processes may compete for a finite number of resources. A
process requests resources; if the resources are not available at that time, the process enters a waiting state.
Sometimes, a waiting process is never again able to change state, because the resources it has requested are
held by other waiting processes. This situation is called a deadlock. Deadlock can be defined as the
permanent blocking of a set of processes that either compete for system resources, or communicate with
each other. A set of processes is deadlocked when each process in the set is blocked awaiting an event
(typically the freeing up of some requested resource) that can only be triggered by another blocked process
in the set. Deadlock is permanent because none of the events is ever triggered. Unlike other problems in
concurrent process management, there is no efficient solution in the general case.
Deadlock Avoidance
Deadlock avoidance is one of the techniques for handling deadlocks. This approach requires that the
operating system be given in advance additional information concerning which resources a process will
request and use during its lifetime. With this additional knowledge, it can decide for each request whether
the process should wait, or not. To decide whether the current request can be satisfied or must be delayed,
the system must consider the resources currently available, the resources currently allocated to each process,
and the future requests and releases of each process.
Banker’s Algorithm
Banker’s algorithm is a deadlock avoidance algorithm that is applicable to a system with multiple instances
of each resource type. The banker’s algorithm is a resource allocation and deadlock avoidance algorithm
that tests for safety by simulating the allocation for predetermined maximum possible amounts of all
resources, then makes a “safe state” check to test for possible activities, before deciding whether allocation
should be allowed to continue.
Several data structures must be maintained to implement the banker’s algorithm. These data structures
encode the state of the resource-allocation system. We need the following data structures, where n is the
number of processes in the system and m is the number of resource types:
• Available. A vector of length m indicates the number of available resources of each type. If Available[j]
equals k, then k instances of resource type Rj are available.
• Max. An n × m matrix defines the maximum demand of each process. If Max[i][j] equals k, then process
Pi may request at most k instances or resource type Rj.
• Allocation. An n × m matrix defines the number of resources of each type currently allocated to each
process. If Allocation[i][j] equals k, then process Pi is currently allocated k instances of resource type Rj.
• Need. An n × m matrix indicates the remaining resource need of each process. If Need[i][j] equals k, then
process Pi may need k more instances of resource type Rj to complete its task. Note that Need[i][j] equals
Max[i][j] − Allocation[i][j].
We can treat each row in the matrices Allocation and Need as vectors and refer to them as Allocationi and
Needi. The vector Allocationi specifies the resources currently allocated to process Pi; the vector Needi
specifies the additional resources that process Pi may still request to complete its task.
61
Department of Software Engineering
Bahria University (Karachi Campus)
Lab Manual Operating System
Safety Algorithm
We can now present the algorithm for finding out whether a system is in a safe state or not. This algorithm can
be described as follows:
1. Let Work and Finish be vectors of length m and n, respectively. Initialize
Work = Available and Finish[i] = false for i = 0, 1, ..., n − 1.
2. Find an index i such that both
a. Finish[i] == false
b. Needi ≤Work
If no such i exists, go to step 4.
3. Work =Work + Allocationi
Finish[i] = true
Go to step 2.
4. If Finish[i] == true for all i, then the system is in a safe state.
This algorithm may require an order of m × n2 operations to determine whether a state is safe.
Resource-Request Algorithm
Next, we describe the algorithm for determining whether requests can be safely granted.
Let Requesti be the request vector for process Pi. If Requesti [ j] == k, then process Pi wants k instances of
resource type Rj. When a request for resources is made by process Pi, the following actions are taken:
1. If Requesti ≤Needi, go to step 2. Otherwise, raise an error condition, since the process has exceeded its
maximum claim.
2. If Requesti ≤ Available, go to step 3. Otherwise, Pi must wait, since the resources are not available.
3. Have the system pretend to have allocated the requested resources to process Pi by modifying the state
as follows:
Available = Available–Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi –Requesti;
If the resulting resource-allocation state is safe, the transaction is completed, and process Pi is allocated its
resources. However, if the new state is unsafe, then Pi must wait for Requesti, and the old resource-allocation
state is restored.
Time Boxing
Activity Name Activity Time Total Time
Login Systems + Setting up Linux Environment 3 mints + 7 mints 10 mints
Walk through Theory & Tasks 60 mints 60 mints
Implement Tasks 80 mints 80 mints
Evaluation Time 30 mints 30 mints
Total Duration 180 mints
Objectives/Outcomes
• To learn a inter process communication and learn how to avoid deadlocks using Banker’s
algorithm.