Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

lOMoARcPSD|41719939

MODULE 3
PYQ
lOMoARcPSD|41719939

1. What is meant by critical section? What are the conditions to be satisfied to ensure a
solution to the critical section problem?

 Critical Section is the part of a program which tries to access shared resources. That
resource may be any resource in a computer like a memory location, Data structure, CPU
or any IO device.
 Only one process can execute in its critical section at a time. All the other processes have
to wait to execute in their critical sections.
 The critical section problem is used to design a set of protocols which can ensure that the
Race condition among the processes will never arise.

A diagram that demonstrates the critical section:

The entry section handles the entry into the critical section. It acquires the resources needed for
execution by the process.
The exit section handles the exit from the critical section. It releases the resources and also
informs the other processes that the critical section is free.
Solution to the Critical Section Problem
Every solution of critical section problem must satisfy these three requirements.

 Mutual Exclusion
Mutual exclusion implies that only one process can be inside the critical section at any
time. If any other processes require the critical section, they must wait until it is free.
 Progress
Progress means that if a process is not using the critical section, then it should not stop any
other process from accessing it. In other words, any process can enter a critical section if it
is free.
 Bounded Waiting
Bounded waiting means that each process must have a limited waiting time. Itt should not
wait endlessly to access the critical section.

2. What is the dining philosopher’s problem?


 The dining philosophers problem states that there are 5 philosophers sharing a circular table
and they eat and think alternatively.
 There is a bowl of rice for each of the philosophers and 5 chopsticks. A philosopher needs
both their right and left chopstick to eat.

1
lOMoARcPSD|41719939

 A hungry philosopher may only eat if there are both chopsticks available. Otherwise
philosophers put down their chopstick and begin thinking again.
 The dining philosopher is a classic synchronization problem as it demonstrates a large class
of concurrency control problems.
 The dining-philosophers problem is a simple representation of the need to allocate several
resources among several processes in a deadlock-free and starvation-free manner.

Solution of Dining Philosophers Problem

A solution of the Dining Philosophers Problem is to use a semaphore to represent a chopstick.


A chopstick can be picked up by executing a wait operation on the semaphore and released by
executing a signal semaphore.
The structure of the chopstick: semaphore chopstick [5];
Initially the elements of the chopstick are initialized to 1 as the chopsticks are on the table and
not picked up by a philosopher.
The structure of a random philosopher i is given as follows −
do
{
wait( chopstick[i] );
wait( chopstick[ (i+1) % 5] );
..
. EATING THE RICE
.
signal( chopstick[i] );
signal( chopstick[ (i+1) % 5] );
.
. THINKING
.
} while(1);

First wait operation is performed on chopstick[i] and chopstick[ (i+1) % 5]. This means that the
philosopher i has picked up the chopsticks on his sides. Then the eating function is performed.

After that, signal operation is performed on chopstick[i] and chopstick[ (i+1) % 5]. This means
that the philosopher i has eaten and put down the chopsticks on his sides. Then the philosopher
goes back to thinking.

2
lOMoARcPSD|41719939

3. What do you mean by deadlock? What are the four necessary conditions for a deadlock
to occur?
A deadlock happens in operating system when two or more processes need some resource to
complete their execution that is held by the other process.

In the above diagram, the process 1 has resource 1 and needs to acquire resource 2. Similarly
process 2 has resource 2 and needs to acquire resource 1. Process 1 and process 2 are in deadlock
as each of them needs the other’s resource to complete their execution but neither of them is
willing to release their resources.

Necessary Conditions: A deadlock situation can arise if the following four conditions hold
simultaneously in a system:

1. Mutual exclusion: Only one process at a time can use the resource. If another process
requests that resource, the requesting process must be delayed until the resource has been
released.
2. Hold and wait: A process must be holding at least one resource and waiting to acquire
additional resources that are currently being held by other processes.
3. No preemption: A resource cannot be preempted from a process by force. A process can
only release a resource voluntarily by the process holding it, after that process has completed
its task.
4. Circular wait: A process is waiting for the resource held by the second process, which is
waiting for the resource held by the third process and so on, till the last process is waiting for
a resource held by the first process. This forms a circular chain.
4. Explain bankers algorithm with an example problem.
Banker’s algorithm is a deadlock avoidance algorithm. It is used in multiple instance resource
allocation. Whenever a new process is created, it must specify the maximum instances of each
resource type that it needs, exactly.
To implement the banker’s algorithm, following 4 data structures are used:
 Available: A vector of length m indicates the number of available resources of each type.
 Max: An n × m matrix defines the maximum demand of each process.
 Allocation: An n × m matrix defines the number of resources of each type currently
allocated to each process.
Work =Work + Allocation
 Need: An n × m matrix indicates the remaining resource need of each process.
Need = Max – Allocation

Eg: To illustrate the use of the banker’s algorithm, consider a system with five processes P0
through P4 and three resource types A, B, and C. Resource type A has ten instances,

3
lOMoARcPSD|41719939

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) Need matrix?
b) Illustrate that the system is in a safe state by demonstrating an order in which the
processes may complete.
c) If a request from process P1 arrives for (1,0,2), can the request be granted immediately?

Ans:
a) Need matrix:
743
122
600
011
431

b) If Need < = Work then work = work + allocation, Work is available number of resources in
the system

Allocation Max Available Need


A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 5 3 2 1 2 2
P2 3 0 2 9 0 2 7 4 3 6 0 0
P3 2 1 1 2 2 2 7 4 5 0 1 1
P4 0 0 2 4 3 3 7 5 5 4 3 1
10 5 7

P0: (7, 4, 3) < = (3, 3, 2) is false, so P0 must wait


P1: (1, 2, 2) < = (3, 3, 2) is true, new work value will be
work = work + allocation
(3, 3, 2) + (2, 0, 0)
work = (5, 3, 2)

P2: (6, 0, 0) < = (5, 3, 2) is false, so P2 must wait


P3: (0, 1, 1) < = (5, 3, 2) is true, new work value will be
work =work +allocation
(5, 3, 2) + (2, 1, 1)

4
lOMoARcPSD|41719939

work =(7,4,3)

P4: (4, 3 ,1) < = (7, 4 , 3) is true, new work value will
be work =work +allocation
(7, 4, 3) + (0, 0, 2)
work =(7,4,5)

P0: (7, 4, 3) < = (7, 4, 5) is true, new work value will be


work =work +allocation
(7, 4, 5)+ (0, 1, 0)
work= ( 7, 5, 5)
P2: (6, 0, 0) < = (7, 5, 5) is true, new work value will be
work =work +allocation
(7, 5, 5) + (3, 0, 2)
work = (10, 5 ,7)

System is in safe state. The safe sequence is < p1, p3, p4, p0, p2 >

If all the processes can be executed in some sequence with the available number of
resources, then the system is said to be in a safe state.
c) Yes this request can be granted immediately since the requested resources are available in the
system .i.e.
P1 request for (1, 0, 2)
1. Request ≤ Need is true
(1, 0, 2) < = (1, 2, 2)
2. Request ≤ Available is true
(1, 0, 2) < = (3, 3, 2)

5. Discuss how Resource Allocation Graph can be used for deadlock avoidance?

 The resource allocation graph is the pictorial representation of the state of a system. The
resource allocation graph is the complete information about all the processes which are
holding some resources or waiting for some resources.
 It also contains the information about all the instances of all the resources whether they are
available or being used by the processes.
 In Resource allocation graph, the process is represented by a Circle while the Resource is
represented by a rectangle.
 Ensure there is no cycle in the graph to avoid deadlock. Allocate a resource only if it can
do that without creating a cycle in the graph.
 In a Resource Allocation Graph where all the resources are single
instance, If a cycle is being formed, then system is in a deadlock
state.
If no cycle is being formed, then system is not in a deadlock state.

There are two major components of a Resource Allocation Graph-


1. Vertices
2. Edges

1. Vertices: There are two main types of resource allocation graph vertices.

5
lOMoARcPSD|41719939

Process Vertex:
 Process vertices represent the processes. They are drawn as a circle by mentioning
the name of process inside the circle.

Resource Vertex:
 Resource vertices represent the resources. They are drawn as a rectangle by
mentioning the dots inside the rectangle.
 The number of dots inside the rectangle indicates the number of instances of that
resource existing in the system.
 Depending on the number of instances that exists in the system, resource vertices
may be single instance or multiple instances.

 Single Instance resource: In a single instance resource type, there is a


single dot inside the box. The single dot mainly indicates that there is one
instance of the resource.

 Multiple instance resource: In multiple instance resource types, there are


multiple dots inside the box, and these multiple dots indicate that there are
multiple instances of the resources.

2. Edges: There are two types of edges in a Resource Allocation Graph


Assign Edges:
 Assign edges represent the assignment of resources to the processes. They are
drawn as an arrow where the head of the arrow points to the process and tail of the
process points to the instance of the resource.
Request Edges:
 Request edges represent the waiting state of processes for the resources. They are
drawn as an arrow where the head of the arrow points to the instance of the
resource and tail of the process points to the process.
 If a process requires ‘n’ instances of a resource type, then ‘n’ assign edges will be
drawn.

The following diagram represents a Resource Allocation Graph-

It gives the following information-


 There exist three processes in the system namely P1, P2 and P3.
 There exist two resources in the system namely R1 and R2.
 There exists a single instance of resource R1 and two instances of resource R2.
 Process P1 holds one instance of resource R1 and is waiting for an instance of resource R2.
 Process P2 holds one instance of resource R2 and is waiting for an instance of resource R1.
 Process P3 holds one instance of resource R2 and is not waiting for anything.

6
lOMoARcPSD|41719939

3. What are the measures to recover from deadlock?

One possibility is to inform the operator that a deadlock has occurred and to let the operator
deal with the deadlock manually.
Another possibility is to let the system recover from the deadlock automatically
There are two options for breaking a deadlock:

1. Terminate one or more processes involving deadlock


2. Preempt some resources from one or more of the deadlocked processes.
Process Termination
To eliminate deadlocks by aborting a process, we use one of two methods. In both methods,
the system reclaims all resources allocated to the terminated processes.
1. Abort all deadlocked processes: This method clearly will break the deadlock cycle. The
deadlocked processes may have computed for a long time and the result of those partial
computations must be discarded and there is a probability to recalculate them later.
2. Abort one process at a time until deadlock is eliminated: Due to method cause
considerable overhead, because after aborting each process, we have to run deadlock
detection algorithm to check whether any processes are still deadlocked.
Resource Preemption
To eliminate deadlocks using resource preemption, we successively preempt some resources
from processes and give these resources to other processes.

When preempting resources to relieve deadlock, there are three important issues to be
addressed:
(a). Selecting a victim: We must determine which resources and which processes are to be
preempted and also the order to minimize the cost.
(b). Rollback: We must determine what should be done with the process from which
resources are preempted. One simple idea is total rollback. That means abort the process and
restart it.
(c). Starvation: In a system, it may happen that same process is always picked as a victim. As
a result, that process will never complete its designated task.
4. Explain with an example how the wait for the graph is used to detect deadlocks?

A Deadlock is a situation where a set of processes are blocked as each process in a Distributed
system is holding some resources and that acquired resources are needed by some other
processes. The deadlock Detection Algorithm is of two types:
 Banker’s Algorithm (Multiple Instance)
 Wait-for-Graph Algorithm (Single Instance)

 If all resources have only a single instance, then we use a variant of the resource-allocation
graph, called the wait-for graph.
 Wait-for-Graph Algorithm: It is a variant of the Resource Allocation graph. In this
algorithm, we only have processes as vertices in the graph. If the Wait-for-Graph contains
a cycle then we can say the system is in a Deadlock state.

7
lOMoARcPSD|41719939

 Obtain the graph from the resource-allocation graph (a) by removing the resource nodes
and collapsing the appropriate edges (b).
 To detect deadlocks, the system needs to maintain the wait for graph and periodically
invoke an algorithm that searches for a cycle in the graph.
Eg:

5. Explain the wait and signal operations used in semaphores.


 Semaphores are integer variables that are used to solve the critical section problem by
using two atomic operations wait and signal that are used for process synchronization.
 All modifications to the integer value of the semaphore in the wait() and signal()
operations must be executed indivisibly.
 That is, when one process modifies the semaphore value, no other process can
simultaneously modify that same semaphore value.

Wait
 The wait operation decrements the value of its argument S, if it is positive.
 If S is negative or zero, then no operation is performed.
 This Operation mainly helps you to control the entry of a task into the critical section.
 wait() operation was originally termed as P; so it is also known as P(S) operation.

The definition of wait operation:

wait(S)
{
while (S<=0);
S--;
}

Signal
 The signal operation increments the value of its argument S.
 This Operation is mainly used to control the exit of a task from the critical section.
 signal() operation was originally termed as V; so it is also known as V(S) operation.

The definition of signal operation:


signal(S)
{
S++;
}
8

You might also like