Unit 3 Os QB With Answer
Unit 3 Os QB With Answer
PART A (2 Marks)
Q.No Questions CO BL
List out the classic problems of synchronization. CO3 R
Four types of the classical problem of synchronization
1. Bound buffer problem.
1
2. Sleeping barber problem.
3. Dining Philosopher problem.
4. Readers and Writers problem
What is critical section problem CO3 R
● The critical section problem is used to design a protocol followed by a group
2
of processes, so that when entered its critical section, no other process is
allowed to execute in its critical section.
Define monitor. CO3 R
● Monitors are defined as the construct of programming language, which helps in
controlling shared data access.
3
● The Monitor is a module or package which encapsulates shared data structure,
procedures, and the synchronization between the concurrent procedure
invocations.
What requirement is to be satisfied for a solution of a critical section problem? CO3 R
CO3 R
Define deadlock prevention
● Deadlock prevention algorithms are used in concurrent programming when
15 multiple processes must acquire more than one shared resource.
● If two or more concurrent processes obtain multiple resources
indiscriminately, a situation can occur where each process has a resource
needed by another process.
Define Starvation in deadlock? CO3 R
Q.No Questions CO BL
1 Solve the solution to Dining philosopher’s problem using monitors. CO3 AP
/* eat */
signal(stick[i]);
signal(stick[(i+1) % 5]);
/* think */
}
Copy
● When a philosopher wants to eat the rice, he will wait for the chopstick at his
left and picks up that chopstick.
● Then he waits for the right chopstick to be available, and then picks it too.
After eating, he puts both the chopsticks down.
● But if all five philosophers are hungry simultaneously, and each of them
pickup one chopstick, then a deadlock situation occurs because they will be
waiting for another chopstick forever.
The possible solutions for this are:
● A philosopher must be allowed to pick up the chopsticks only if both the left
and right chopsticks are available.
● Allow only four philosophers to sit at the table. That way, if all the four
philosophers pick up four chopsticks, there will be one chopstick left on the
table.
● So, one philosopher can start eating and eventually, two chopsticks will be
available. In this way, deadlocks can be avoided.
1. Safety algorithm
2. Resource request algorithm
Safety Algorithm
1. Work = Available
2. Finish[i] =false for i = 0, 1, ... , n - 1.
● This means, initially, no process has finished and the number of available
resources is represented by the Available array.
3. Finish[i] ==false
4. Needi <= Work
● Go to step 2.
● When an unfinished process is found, then the resources are allocated and the
process is marked finished. And then, the loop is repeated to check the same
for all other processes.
● If Finish[i] == true for all i, then the system is in a safe state.
● That means if all processes are finished, then the system is in safe state.
● This algorithm may require an order of mxn² operations in order to determine
whether a state is safe or not.
1. If Requesti <= Needi, then go to step 2;else raise an error condition, since the
process has exceeded its maximum claim.
2.If Requesti <= Availablei then go to step 3; else Pi must have to wait as resources
are not available.
3.Now we will assume that resources are assigned to process Pi and thus perform the
following steps:
● Available= Available-Requesti;
● Allocationi=Allocationi +Requesti;
● Needi =Needi - Requesti;
4.If the resulting resource allocation state comes out to be safe, then the transaction is
completed and, process Pi is allocated its resources. But in this case, if the new state is
unsafe, then Pi waits for Requesti, and the old resource-allocation state is restored.
4 Illustrate the classical problems of Synchronization.
The classical problems of synchronization are as follows:
1. Bound-Buffer problem
2. Sleeping barber problem
3. Dining Philosophers problem
4. Readers and writers problem
Bound-Buffer problem
● Also known as the Producer-Consumer problem. In this problem, there is a
buffer of n slots, and each buffer is capable of storing one unit of data.
● There are two processes that are operating on the buffer – Producer and
Consumer. The producer tries to insert data and the consumer tries to remove
data.
● If the processes are run simultaneously they will not yield the expected output.
● The solution to this problem is creating two semaphores, one full and the other
empty to keep a track of the concurrent processes.
Critical Section
● The critical section in a code segment where the shared variables can be
accessed.
● Atomic action is required in a critical section i.e. 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 is given as follows:
do{
Entry Section
Critical Section
Exit Section
Remainder Section
} while (TRUE);
● In the above diagram, the entry sections 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 critical
section is free.
The critical section problem needs a solution to synchronise the different processes.
The solution to the critical section problem must satisfy the following conditions −
● 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.
● Progresss
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 Waitings
Bounded waiting means that each process must have a limited waiting time. It
should not wait endlessly to access the critical section.
ii. Difference between Mutex and Semaphores.(8)
Mutex vs Semaphore
Mutex and Semaphore both provide synchronization services but they are not the
same.
Mutex
● Mutex is a mutual exclusion object that synchronizes access to a resource. It
is created with a unique name at the start of a program.
● The Mutex is a locking mechanism that makes sure only one thread can
acquire the Mutex at a time and enter the critical section.
● This thread only releases the Mutex when it exits the critical section.
wait (mutex);
…..
Critical Section
…..
signal (mutex);
● A Mutex is different than a semaphore as it is a locking mechanism while a
semaphore is a signaling mechanism.
● A binary semaphore can be used as a Mutex but a Mutex can never be used as
a semaphore.
● Semaphore
● A semaphore is a signaling mechanism and a thread that is waiting on a
semaphore can be signaled by another thread.
● This is different than a mutex as the mutex can be signaled only by the thread
that called the wait function.
● A semaphore uses two atomic operations, wait and signal for process
synchronization.
● 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.
wait(S)
{
while (S<=0);
S--;
}
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}
● There are mainly two types of semaphores i.e. counting semaphores and
binary semaphores.
● Counting Semaphores are integer value semaphores and have an unrestricted
value domain.
● These semaphores are used to coordinate the resource access, where the
semaphore count is the number of available resources.
● The binary semaphores are like counting semaphores but their value is
restricted to 0 and 1.
The wait operation only works when the semaphore is 1 and the signal operation
succeeds when semaphore is 0.
6 Consider the following snapshot of a system: CO3 AZ
P0 – P4 are 5 processes present and A, B, C, D are the resources. The maximum need
of a Process and the allocated resources details are given in the table.
Answer the following based on banker’s algorithm.
1.What is the content of NEED matrix?
2.Is the system in a safe state?
3.If a request from process P0 arrives for (0, 1, 1,1) can the request be granted
immediately.