0% found this document useful (0 votes)
3 views15 pages

Module 3 Problemsa

The document discusses various synchronization problems in operating systems, including Peterson's solution for the critical-section problem, the bounded-buffer problem, the readers-writers problem, the dining-philosophers problem, and the banker's algorithm for resource allocation. Each problem is explained with its respective algorithms and data structures, highlighting the importance of mutual exclusion, progress, and bounded waiting. Additionally, the document outlines potential deadlock scenarios and solutions for the dining-philosophers problem.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Module 3 Problemsa

The document discusses various synchronization problems in operating systems, including Peterson's solution for the critical-section problem, the bounded-buffer problem, the readers-writers problem, the dining-philosophers problem, and the banker's algorithm for resource allocation. Each problem is explained with its respective algorithms and data structures, highlighting the importance of mutual exclusion, progress, and bounded waiting. Additionally, the document outlines potential deadlock scenarios and solutions for the dining-philosophers problem.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Operating Systems (BCS303) III-ISE, MODULE-III

1. Peterson’s solution

• A classic software-based solution to the critical-section problem is known as Peterson's


solution.
• Peterson's solution is restricted to two processes that alternate execution between their critical
sections and remainder sections.
• The processes are numbered P0 and P1 or Pi and Pj where j=1-i.
• Peterson's solution requires two data items to be shared between the two processes,
• int turn;
• boolean flag[2];

• The variable turn indicates whose turn it is to enter its critical section. That is, if turn = = i,
then process Pi, is allowed to execute in its critical section.
• The flag array is used to indicate if a process is ready to enter its critical section. For
example, if flag[i] is true, this value indicates that Pi is ready to enter its critical section.
• To enter the critical section, process Pi first sets flag[i] to be true and then sets turn to the
value j, thereby asserting that if the other process wishes to enter the critical section, it can do
so.
• If both processes try to enter at the same time, turn will be set to both i and j at roughly the
same time. Only one of these assignments will last; the other will occur but will be overwritten
immediately.
• The eventual value of turn decides which of the two processes is allowed to enter its critical
section first.
• The following algorithm describes the structure of Pi in Peterson’s solution.

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 1


Operating Systems (BCS303) III-ISE, MODULE-III

• To prove that this solution is correct, we need to show that,


1. Mutual exclusion is preserved.
2. The progress requirement is satisfied.
3. The bounded-waiting requirement is met.

• To prove property 1, we note that each Pi enters its critical section only if either flag[j] = =
false or turn = = i. For P0 to enter, turn must be equal to 0 and for P1 to enter, turn must be
equal to 1 because flag[0] = = flag[1] = = true. Since the value of turn can be either 0 or 1 but
cannot be both, hence P0 and P1 cannot enter into critical section simultaneously.
• To prove properties 2 and 3, we note that a process Pi can be prevented from entering the
critical section only if it is stuck in the while loop with the condition flag[j] = = true and turn
= = j. If Pj is not ready to enter the critical section, then flag [j] = = false, and P i can enter its
critical section. If turn = = j, then Pj will enter the critical section. However, once Pj exits its
critical section, it will reset flag[j] to false, allowing Pi to enter its critical section. If Pj resets
flag[j] to true, it must also set turn to i. Thus, Pi will enter the critical section (progress) after
at most one entry by Pi (bounded waiting).

2. Classic problems of Synchronization


• Bounded-buffer problem
• The Readers-Writers Problem
• The Dining-Philosophers Problem

• The Bounded-Buffer Problem

We assume that the pool consists of n buffers, each capable of holding one item.
1. The mutex semaphore provides mutual exclusion for accesses to the buffer pool and
is initialized to the value 1.
2. The empty and full semaphores count the number of empty and full buffers.
3. The semaphore empty is initialized to the value n; the semaphore full is initialized to
the value 0.
4. The code for the producer process is shown: do{
// produce an item in nextp
...
wait(empty); wait
(mutex);
...
// add nextp to buffer
...
signal(mutex); signal
(full);
}while (TRUE);

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 2


Operating Systems (BCS303) III-ISE, MODULE-III

5. The code for the consumer process is shown: do {


wait (full);
wait(mutex);
...
//remove an item from buffer to nextc
...
signal(mutex); signal(empty);
//consume the item in nextc
...
}while (TRUE);

6. We can interpret this code as the producer producing full buffers for the consumer or
as the consumer producing empty buffers for the producer.

3. The Readers-Writers Problem


• A database is to be shared among several concurrent processes. Some of these processes may
want only to read the database, whereas others may want to update (that is, to read and write)
the database.
• Readers - processes that only read the database.
• Writers - processes performing both read and write (update).
• Problem: If two readers access the shared data simultaneously, no problem will result. But if
a writer and some other thread (either a reader or a writer) access the database simultaneously,
problem arises.
• This synchronization problem is referred to as the readers-writers problem.
• The readers-writers problem has several variations.
• The first readers-writers problem, which requires that no reader must be kept waiting unless
a writer has already obtained permission to use the shared object.
• The second readers-writers problem requires that, once a writer is ready, that writer performs
its write as soon as possible.
• A solution to either problem may result in starvation. In the first case, writers may starve; in
the second case, readers may starve.
• In the solution to the first readers-writers problem, the reader processes share the following
data structures:
semaphore mutex, wrt; int
readcount;
• The semaphores mutex and wrt are initialized to 1and readcount is initialized to 0.
• The semaphore wrt is common to both reader and writer processes.
• The mutex semaphore is used to ensure mutual exclusion when the variable readcount is
updated.
• The readcount variable keeps track of how many processes are currently reading the object.
• The semaphore wrt functions as a mutual-exclusion semaphore for the writers.
• The code for a writer process is,

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 3


Operating Systems (BCS303) III-ISE, MODULE-III

do { wait
(wrt);
…..
// writing is performed
signal (wrt);
} while (TRUE);

• The code for a reader process is shown,

do { wait
(mutex);
readcount++;
if (readcount = = 1) wait
(wrt);
signal(mutex);
……..
// reading is performed
……..
wait(mutex);
readcount--; if
(readcount= = 0)
signal(wrt);
signal(mutex);
} while (TRUE);

• If a writer is in the critical section and n readers are waiting, then one reader is queued on wrt,
and n-1 readers are queued on mutex.
• Also, when a writer executes signal(wrt), we may resume the execution of either the waiting
readers or a single waiting writer. The selection is made by the scheduler.

4. The Dining-Philosophers Problem

• Consider five philosophers who spend their lives thinking and eating.
• The philosophers share a circular table surrounded by five chairs, each belonging to on*
philosopher.
• In the centre of the table is a bowl of rice, and the table is laid with five single chopsticks
(below figure).
• When a philosopher is thinking, she does not interact with her colleagues.
• When a philosopher gets hungry, she tries to pick up the two chopsticks that are closest to her
(the chopsticks that are between her and her left and right neighbors).
• When a hungry philosopher has both her chopsticks at the same time, she eats without
releasing her chopsticks. When she is finished eating, she puts down both of her chopsticks
and starts thinking again.

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 4


Operating Systems (BCS303) III-ISE, MODULE-III

figure: The situation of the dining philosophers

• One simple solution is to represent each chopstick with a semaphore.


• A philosopher tries to grab a chopstick by executing a wait() operation on that semaphore;
she releases her chopsticks by executing the signal() operation on the appropriate
semaphores.
• Thus, the shared data are
semaphore chopstick[5];
where all the elements of chopstick are initialized to 1.
• The structure of philosopher i is shown below,

do {
wait(chopstick[i]);
k[(i+l) % 5]);
……
// eat
........

signal(chopstick[i]);
signal(chopstick[(i+l) % 5]);
………
// think
..........
} while (TRUE);

• The disadvantage is it could create a deadlock. Suppose that all five philosophers become
hungry simultaneously and each grabs her left chopstick. All the elements of chopstick will
now be equal to 0. When each philosopher tries to grab her right chopstick, she will be delayed
forever.
• Several possible remedies to the deadlock problem are available.
o Allow at most four philosophers to be sitting simultaneously at the table.
o Allow a philosopher to pick up her chopsticks only if both chopsticks are
available (to do this, she must pick them up in a critical section).

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 5


Operating Systems (BCS303) III-ISE, MODULE-III
o Use an asymmetric solution; that is, an odd philosopher picks up first her left
chopstick and then her right chopstick, whereas an even philosopher picks up
her right chopstick and then her left chopstick.

5. Banker’s Algorithm

• This algorithm is applicable to the system with multiple instances of each resource types, but
this is less efficient than the resource allocation graph algorithm.
• When a new process enters the system it must declare the maximum number of resources that
it may need. This number may not exceed the total number of resources in the system. The
system must determine that whether the allocation of the resources will leave the system in a
safe state or not. If it is so resources are allocated else it should wait until the process release
enough resources.
• Several data structures are used to implement the banker’s algorithm. Let ‘n’ be the number
of processes in the system and ‘m’ be the number of resources types. The following data
structures are needed. o Available: A vector of length m indicates the number of available
resources. If Available[j]=k, then k instances of resource type Rj is available.
1. Max: An n*m matrix defines the maximum demand of each process. If
Max[i][j]=k, then Pi may request at most k instances of resource type Rj.
2. Allocation: An n*m matrix defines the number of resources of each type currently
allocated to each process. If Allocation[i][j]=k, then Pi is currently allocated k
instances of resource type Rj.
3. Need: An n*m matrix indicates the remaining resources need of each process. If
Need[i][j]=k, then Pi may need k more instances of resource type Rj to complete
its task. So Need[i][j]=Max[i][j]-Allocation[i][j].

• Safety Algorithm

• This algorithm is used to find out whether a system is in safe state or not. The algorithm can
be described as follows,
Step 1. Let Work and Finish be two vectors of length m and n
respectively. Initialize work available and Finish[i]=false for
i=1,2,3,…….n
Step 2. Find i such that both
Finish[i]==false
Needi ≤ Work
If no such i exists, then go to step 4
Step 3. Work = Work +Allocation
Finish[i]=true
Go to step 2
Step 4. If Finish[i]==true for all i, then the system is in safe state.

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 6


Operating Systems (BCS303) III-ISE, MODULE-III

• This algorithm may require an order of m*n2 operation to decide whether a state is safe.

• Resource Request Algorithm

• Let Requesti be the request vector of process Pi. If Requesti[j]==k, then process Pi wants k
instances of the resource type Rj. When a request for resources is made by process Pi the
following actions are taken.

Step 1: If Requesti ≤ Needi , go to step 2. Otherwise raise an error


condition, since the process has exceeded its maximum claim.
Step 2: If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since
the resources are not available.
Step 3: The system pretend to have allocated the requested resources to
process Pi, then modify the state as follows.

Available = Available – Requesti


Allocationi = Allocationi + Requesti
Needi = Needi – Requesti
• If the resulting resource allocation state is safe, the transaction is complete and Pi is allocated
its resources. If the new state is unsafe, then Pi must wait for Requesti and old resource
allocation state is restored.

• An Illustrative Example

• To illustrate the use of the banker's algorithm, consider a system with five Processes P o
through P4 and three resource types A, B, and C. Resource type A has ten instances, resource
type B has five instances, and resource type C has seven instances. Suppose that, at time T0,
the following snapshot of the system has been taken.

a)
Allocation Max Available
ABC ABC ABC
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

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 7


Operating Systems (BCS303) III-ISE, MODULE-III

• The content of the matrix Need is defined to be Max - Allocation and is as follows:
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

Now, we have to apply safety algorithm to this snapshot as shown below,

P0 →7 4 3 ≤ 3 3 2 is false,
P1 →1 2 2 ≤ 3 3 2 is true, so work=work + allocation
work=3 3 2 + 2 0 0=5 3 2
P2 →6 0 0 ≤ 5 3 2 is false,
P3 → 0 1 1 ≤ 5 3 2 is true, so work=5 3 2 + 2 1 1=7 4 3
P4 → 4 3 1≤ 7 4 3 is true, so work=7 4 3 + 0 0 2=7 4 5
P2 → 6 0 0 ≤ 7 4 5 is true, so work=7 4 5 + 3 0 2=10 4 7
P0 →7 4 3 ≤ 10 4 7 is true, so work=10 4 7 + 0 1 0=10 5 7

• We claim that the system is currently in a safe state. The sequence < Pl, P3, P4, P2,
P0> or < Pl, P3, P4, P0, P2> satisfies the safety criteria.

b) Suppose now the process P1 requests one additional instance of resource type A
and two instances of resource type C, so Request1 = (1,0,2).
* To decide whether this request can be immediately granted, we first check that
from Resource request algorithm,

Request1 ≤ Need1, that is, (1,0,2) ≤ (1,2,2), which is true then,


Request1 ≤ Available, that is, (1,0,2) ≤ (3,3,2), which is true. Then we arrive at
the following new state:

Allocation Need Available


ABC ABC ABC
P0 0 1 0 7 4 3 2 3 0
P1 3 0 2 0 2 0
P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 8


Operating Systems (BCS303) III-ISE, MODULE-III

• Now we must determine whether this new system state is safe. We execute safety algorithm
as shown below and find that the sequence <P1, P3, P4, Po, P2> satisfies the safety
requirement. Hence the request can be immediately granted.

P0 → 7 4 3 ≤ 2 3 0 is false,
P1 →0 2 0 ≤ 2 3 0 is true, so work=work + allocation
work=2 3 0+ 3 0 2=5 3 2
P2 →6 0 0 ≤ 5 3 2 is false,
P3 → 0 1 1 ≤ 5 3 2 is true, so work=5 3 2 + 2 1 1=7 4 3
P4→ 4 3 1 ≤ 7 4 3 is true, so work=7 4 3 + 0 0 2=7 4 5
P0 →7 4 3 ≤ 7 4 5 is true, so work=7 4 5 + 0 1 0=7 5 5
P2 → 6 0 0 ≤ 7 5 5 is true, so work=7 5 5 + 3 0 2=10 5 7

c) Suppose P4 request for (3,3,0) can it be granted immediately? From Resource Request
Algorithm, we must see that when the system is in this state, a request for
(3,3,0) by P4 cannot be granted, since the resources are not available, that is,
Request4 ≤ Need4, (3,3,0) ≤ (4,3,1)……true
Request4 ≤ Available, (3,3,0) ≤ (2,3,0)……false

d) Similarly, a request for (0,2,0) by Po cannot be granted, even though the resources are
available, because the resulting state is unsafe.
that is, Request4 ≤ Need1, (0,2,0) ≤ (7,4,3)……true
Request1 ≤ Available1, (0,2,0) ≤ (2,3,0)…… true

Now the snapshot changes as follows,


Allocation Need Available
ABC ABC ABC
P0 0 3 0 7 2 3 2 1 0
P1 3 0 2 0 2 0
P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1

Then we are supposed to apply safety algorithm to this snapshot as shown below, but no safe
sequence is generated, hence the request for (0,2,0) by Po cannot be granted.

That is, Needi ≤ Available P0


→7 2 3 ≤ 2 1 0 is false
P1 →0 2 0 ≤ 2 1 0 is false
P2 → 6 0 0 ≤ 2 1 0 is false
P3→ 0 1 1 ≤ 2 1 0 is false
P4 → 4 3 1≤ 2 1 0 is false

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 9


Operating Systems (BCS303) III-ISE, MODULE-III

6. Deadlock Detection

• If a system does not employ either deadlock prevention or a deadlock avoidance algorithm
then a deadlock situation may occur. In this environment the system must provide, o An
algorithm that examines the state of the system to determine whether a deadlock has occurred.
o An algorithm to recover from the deadlock.
• Single Instances of each Resource Type

• If all the resources have only a single instance then we can define deadlock detection
algorithm that uses a variant of resource allocation graph as shown in below figure (a)
called a wait-for graph as shown in below figure (b). This graph is obtained by removing
the resource nodes and collapsing appropriate edges.

• An edge from Pi to Pj in wait for graph implies that Pi is waiting for Pj to release a resource
that Pi needs.
• An edge from Pi to Pj exists in wait for graph if and only if the corresponding resource
allocation graph contains the edges Pi Rq and Rq Pj.
• Deadlock exists within the system if and only if there is a cycle. To detect deadlock the system
needs an algorithm that searches for cycle in a graph.

• Several Instances of Resource Type

• The wait-for graph scheme is not applicable to a resource-allocation system with multiple
instances of each resource type.

• The deadlock detection algorithm includes following time-varying data structures.


o Available. A vector of length m indicates the number of available resources of
each type.
o Allocation. An n*m matrix defines the number of resources of each type currently
allocated to each process.
o Request. An n*m matrix indicates the current request of each process. If
Request[i][j]=k then Pi is requesting k more instances of resources type Rj.
[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 10
Operating Systems (BCS303) III-ISE, MODULE-III

• The deadlock detection algorithm can be defined as follows,

Step 1. Let Work and Finish be vectors of length m and n respectively. Initialize
Work= Available. For i=0,1,2……….n-1, if allocation ≠ 0 then Finish[i]=false, else
Finish[i]=true.
Step 2. Find an index i such that both Finish[i]=
false
Requesti≤Work
If no such i exists, go to step 4.
Step 3. Work = Work + Allocationi
Finish[i] = true Go to step
2.
Step 4. If Finish[i] == false, for some i where 0≤i<n, then a system is in a deadlock
state.
• To illustrate this algorithm, we consider a system with five processes P0 through P4 and three
resource types A, B, and C. Resource type A has seven instances, resource type B has two
instances, and resource type C has six instances. Suppose that, at time T0, we have the
following resource-allocation state:

Allocation Request Available


ABC ABC ABC
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2

• From the above deadlock detection algorithm, the sequence <P0, P2, P3, P1, P4> or
<P0, P2, P3, P4, P1>will result in Finish[i]== true for all i. The steps includes,

Requesti ≤ work, that is,

P0 →0 0 0 ≤ 0 0 0 is true, so work=work + allocation


work=0 0 0+ 0 1 0=0 1 0
P1 → 2 0 2 ≤ 0 1 0 is false,
P2 → 0 0 0 ≤ 0 1 0 is true, so work=0 1 0 + 3 0 3=3 1 3
P3 → 1 0 0 ≤ 3 1 3 is true, so work=3 1 3 + 2 1 1=5 2 4
P4 → 0 0 2 ≤ 5 2 4 is true, so work=5 2 4 + 0 0 2=5 2 6
P1 → 2 0 2 ≤ 5 2 6 is true, so work=5 2 6+ 2 0 0= 7 2 6
If P2 requests an additional instance of type C ie., (0, 0, 1), the Request matrix is modified as
follows,
Request

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 11


Operating Systems (BCS303) III-ISE, MODULE-III

ABC
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2

From the deadlock detection algorithm, That is, Requesti ≤ work, P0


→0 0 0 ≤ 0 0 0 is true, so work=work + allocation
work=0 0 0+ 0 1 0=0 1 0
P1 → 2 0 2 ≤ 0 1 0 is false,
P2 → 0 0 1 ≤ 0 1 0 is false,
P3 → 1 0 0 ≤ 0 1 0 is false,
P4 → 0 0 2 ≤ 0 1 0 is false,
• The system is now deadlocked. Even though we can reclaim resources held by process P0, but
number of available resources is not sufficient to fulfill the requests of other processes. Thus,
deadlock exists, consisting of processes P1, P2, P3, and P4.

Solved Exercises (VTU QP problems)


1. Consider given chart where maximum resource available of type A, B, C and D are 3, 14, 12 and
12 respectively, and answer i) what is content of matrix need? ii) Is system safe? If yes give safe
sequence. iii) If request comes from P1 as (0, 4, 2, 0), can it be granted?

Allocation Max Available


ABCD ABCD ABCD
P0 0 0 1 2 00 1 2 1 5 2 0
P1 1 0 0 0 17 5 0
P2 1 3 5 4 23 5 6
P3 0 6 3 2 06 5 2
P4 0 0 1 4 065 6

The content of the matrix Need is defined to be Max - Allocation and is as follows:
Need
ABCD
P0 0 0 0 0
P1 0 7 5 0
P2 1 0 0 2
P3 0 0 2 0

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 12


Operating Systems (BCS303) III-ISE, MODULE-III

P4 0 6 4 2

Now, we have to apply safety algorithm to this snapshot as shown below,

P0 → 0 0 0 0 ≤ 1 5 2 0 is true, so work=work + allocation


work= 1 5 2 0 + 0 0 1 2=1 5 3 2
P1→ 0 7 5 0 ≤ 1 5 3 2 is false,
P2→1 0 0 2 ≤ 1 5 3 2 is true, so work=1 5 3 2 + 1 3 5 4=2 8 8 6
P3→0 0 2 0 ≤ 2 8 8 6 is true, so work=2 8 8 6 + 0 6 3 2=2 14 11 8
P4→ 0 6 4 2 ≤ 2 14 11 8 is true, so work=2 14 11 8+ 0 0 1 4=2 14 12 12
P1 → 0 7 5 0 ≤ 2 14 12 12 is true, so work=2 14 12 12+ 1 0 0 0=3 14 12 12
We claim that the system is currently in a safe state. The sequence < Po, P2, P3, P4, Pl > satisfies
the safety criteria.

a) Suppose now the process P1 requests for (0,4,2,0). To decide whether this request can be
immediately granted, we first check that,

Request1 ≤ Need1, that is, (0,4,2,0) ≤ (0,7,5,0) which is true then,


Request1 ≤ Available, that is, (0,4,2,0) ≤ (1,5,2,0) which is true. Then
we arrive at the following new state:

Allocation Available
ABCD ABCD
P0 0 0 1 2 1 1 0 0
P1 1 4 2 0
P2 1 3 5 4
P3 0 6 3 2
P4 0 0 1 4

Need
ABCD
P0 0 0 0 0
P1 0 3 3 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2

Now we must determine whether this new system state is safe. We execute safety algorithm as
shown below and find that the sequence <P0, P2, P3, P4, P1> satisfies the safety requirement.
Hence the request can be immediately granted.
[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 13
Operating Systems (BCS303) III-ISE, MODULE-III

P0 →0 0 0 0 ≤ 1 1 0 0 is true, so work=work + allocation


work= 1 1 0 0 + 0 0 1 2=1 1 1 2 P1
→0 3 3 0 ≤ 1 1 1 2 is false,

P2 →1 0 0 2 ≤ 1 1 1 2 is true, so work=1 1 1 2 + 1 3 5 4=2 4 6 6

P3 →0 0 2 0 ≤ 2 4 6 6 is true, so work=2 4 6 6 + 0 6 3 2=2 10 9 8

P4 → 0 6 4 2 ≤ 2 10 9 8 is true, so work=2 10 9 8 + 0 0 1 4=2 10 10 12

P1→ 0 3 3 0 ≤ 2 10 10 12 is true, so work=2 10 10 12 + 1 4 2 0=3 14 12 12

2. Using Banker’s algorithm determine whether the following system is in a safe state.

Process Allocation Max Available


ABC ABC ABC
P0 0 0 2 0 0 4 1 0 2
P1 1 0 0 2 0 1
P2 1 3 5 1 3 7
P3 6 3 2 8 4 2
P4 1 4 3 1 5 7
If a request from process P2 arrives for (0 0 2), can the request be granted immediately?

a) The content of the matrix Need is defined to be Max - Allocation and is as follows:

Need
ABC
P0 0 0 2
P1 1 0 1
P2 0 0 2
P3 2 1 0
P4 0 1 4
Now, we have to apply safety algorithm to this snapshot as shown below,
P0 →0 0 2 ≤ 1 0 2 is true, so work=work + allocation
work =1 0 2 + 0 0 2=1 0 4,

P1 →1 0 1 ≤ 1 0 4 is true, so work=1 0 4 + 1 0 0=2 0 4,

P2 → 0 0 2 ≤ 2 0 4 is true, so work= 2 0 4 + 1 3 5=3 3 9

P3 → 2 1 0 ≤ 3 3 9 is true, so work=3 3 9 + 6 3 2=9 6 11

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 14


Operating Systems (BCS303) III-ISE, MODULE-III

P4 → 0 1 4 ≤ 9 6 11 is true, so work=9 6 11 + 1 4 3=10 10 14

We claim that the system is currently in a safe state. The sequence


< P0, P1, P2, P3, P4> satisfies the safety criteria.

b) Suppose now the process P2 requests for the resources (0,0,2), so Request2 = (0,0,2).

To decide whether this request can be immediately granted or not, we have to first
check from Resource request algorithm that,

Request2 ≤ Need2, that is, (0,0,2) ≤ (0,0,2), which is true then,


Request2 ≤ Available, that is, (0,0,2) ≤ (1,0,2), which is true.

Hence, modify the snapshot for a request (0,0,2) by P2 and find the safe sequence.

[Link], [Link], Mrs. AMR Dept of ISE, RNSIT Page 15

You might also like