Professional Documents
Culture Documents
cst206-os-MODULE 3 QUESTION AND ANSWERS
cst206-os-MODULE 3 QUESTION AND ANSWERS
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.
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.
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.
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
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)
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.
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.
6
lOMoARcPSD|41719939
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:
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:
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.
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.