0% found this document useful (0 votes)
198 views10 pages

Unit 3 Os QB With Answer

Uploaded by

Santhiya S cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views10 pages

Unit 3 Os QB With Answer

Uploaded by

Santhiya S cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT 3 – CONCURRENCY CONTROL

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

● Mutual exclusion: When one process is executing in its critical section, no


other process is allowed to execute in its critical section.
4
● Progress: When no process is executing in its critical section, and there exists
a process that wishes to enter its critical section, it should not have to wait
indefinitely to enter it.

Give the condition necessary for a deadlock situation to arise? CO3 R


The four necessary conditions for a deadlock situation to occur are
● Mutual exclusion.
5
● Hold and Wait.
● No preemption.
● Circular set.
Define Label busy waiting and spinlock. CO3 R
Spin lock:
Spin locks are a low-level synchronization mechanism suitable primarily for use on
shared memory multiprocessors. When the calling thread requests a spin lock that is
already held by another thread, the second thread spins in a loop to test if the lock has
6 become available.
Label busy waiting:
Busy waiting, also known as spinning, or busy looping is a process synchronization
technique in which a process/task waits and constantly checks for a condition to be
satisfied before proceeding with its execution.

Define semaphores. CO3 R


● Semaphores can be implemented inside the operating system by interfacing
7 with the process state and scheduling queues: a thread that is blocked on a
semaphore is moved from running to waiting (a semaphore-specific waiting
queue).
How the deadlock can be recovered?
● The OS doesn't apply any mechanism to avoid or prevent the deadlocks.
Therefore the system considers that the deadlock will definitely occur. In
8 order to get rid of deadlocks, The OS periodically checks the system for any CO3 AP
deadlock.
● In case, it finds any of the deadlock then the OS will recover the system using
some recovery techniques. The main task of the OS is detecting the deadlocks
Process A is waiting for the result produced by process B. Also, process A has higher
priority than B. So the OS prefers process A and refuses to give CPU time to B.
Therefore, both A and B are stuck. What is this scenario called and justify?

 Starvation is a condition where a process does


not get the resources it needs for a long time
9 because the resources are being allocated to CO3 AZ
other processes. It generally occurs in a Priority
based scheduling System. Where High Priority
requests get processed first. Thus a request with
least priority may never be processed.
A total of 9 units of a resource type available, and given the safe state shown below,
Find the safe sequence.
Process Used Max
10 P1 2 7 CO3 U
P2 1 6
P3 2 5
P4 1 4
Define race condition. CO3 R
● A race problem occurs when the output of a software application is
determined by the timing or sequencing of other uncontrollable events.
11
● Race situations can also happen in multithreaded software, runs in a
distributed environment, or is interdependent on shared resources.

Define ‘Safe State”? CO3 R


12 ● A state of the system is called safe if the system can allocate all the resources
requested by all the processes without entering into deadlock.
What is mutual exclusion? CO3 R
● The mutual exclusion (mutex) is a program object that prevents simultaneous
13 access to a shared resource.
● This concept is used in concurrent programming with a critical section, a
piece of code in which processes or threads access a shared resource.
14 What is a resource-allocation graph? CO3 R

● In single instanced resource types, if a cycle is being formed in the system


then there will definitely be a deadlock. On the other hand, in multiple
instanced resource type graph, detecting a cycle is not just enough.
● We have to apply the safety algorithm on the system by converting the
resource allocation graph into the allocation matrix and request matrix.

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

● A problem in concurrent computing is where a process is continuously denied


16
the resources it needs to complete its work.
● It could be caused by errors in scheduling or mutual exclusion algorithm, but
resource leaks may also cause it .
What is producer consumer problem? CO3 R
● The producer consumer problem is a synchronization problem.
17 ● There is a fixed size buffer and the producer produces items and enters them
into the buffer.
● The consumer removes the items from the buffer and consumes them.
How can avoid deadlock? CO3
● A deadlock mainly happens when we give locks to multiple threads. Avoid
18 giving a lock to multiple threads if we already have given to one. Avoid U
Unnecessary Locks: We can have a lock only those members which are
required.
What are pipes in synchronization? CO3
● A pipe is a data channel that is unidirectional. Two pipes can be used to create
19 a two-way data channel between two processes. This uses standard input and R
output methods. Pipes are used in all POSIX systems as well as Windows
operating systems.
Consider a system with 10 tape drives and three processes:p0,p1,p2. Process P0
requires 8 tape drives, Process p1 requires 4 tape drives and process p2 requires upto 7
20 tape drives. Suppose that, at time t0 process p0 is holding 4 tape drives process p1 CO3 AZ
holding 2 tape drives and process p2 is holding 2 tape drives. At time t0, find the safe
sequence?
PART- B (16 Marks)

Q.No Questions CO BL
1 Solve the solution to Dining philosopher’s problem using monitors. CO3 AP

● The dining philosophers problem is another classic synchronization problem


which is used to evaluate situations where there is a need of allocating
multiple resources to multiple processes.

Dining Philosophers Problem


● At any instant, a philosopher is either eating or thinking. When a philosopher
wants to eat, he uses two chopsticks - one from their left and one from their
right. When a philosopher wants to think, he keeps down both chopsticks at
their original place.
● Here's the Solution
● From the problem statement, it is clear that a philosopher can think for an
indefinite amount of time. But when a philosopher starts eating, he has to stop
at some point of time. The philosopher is in an endless cycle of thinking and
eating.
● An array of five semaphores, stick[5], for each of the five chopsticks.
The code for each philosopher looks like:
while(TRUE)
{
wait(stick[i]);
/*
mod is used because if i=5, next
chopstick is 1 (dining table is circular)
*/
wait(stick[(i+1) % 5]);

/* 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.

2 Evaluate the following snapshot of the system CO3 AP


Allocation Max Available
Process
ABCD ABCD ABCD
Po 0012 0013 1520
P1 1000 1750
P2 1354 2356
P3 0632 0652
P4 0014 0656
Answer the follow based on banker’s algorithm.
1. What is the content of need matrix? 2. Is the system in a safe state?
3 Explain Dead lock detection (Banker’s Algorithm) with Example? CO3 U

● Banker's algorithm is a deadlock avoidance algorithm. It is named so because


this algorithm is used in banking systems to determine whether a loan can be
granted or not.
● Consider there are n account holders in a bank and the sum of the money in all
of their accounts is S. Every time a loan has to be granted by the bank, it
subtracts the loan amount from the total money the bank has. Then it checks if
that difference is greater than S. It is done because, only then, the bank would
have enough money even if all the n account holders draw all their money at
once.

Characteristics of Banker's Algorithm

● If any process requests for a resource, then it has to wait.


● This algorithm consists of advanced features for maximum resource
allocation.
● There are limited resources in the system we have.
● In this algorithm, if any process gets all the needed resources, then it is that it
should return the resources in a restricted period.
● Various resources are maintained in this algorithm that can fulfill the needs of
at least one client.

Banker’s algorithm comprises of two algorithms:

1. Safety algorithm
2. Resource request algorithm

Safety Algorithm

● A safety algorithm is an algorithm used to find whether or not a system is in


its safe state. The algorithm is as follows:
● Let Work and Finish be vectors of length m and n, respectively. Initially,

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.

Find an index i such that both

3. Finish[i] ==false
4. Needi <= Work

● If there is no such i present, then proceed to step 4.


● It means, we need to find an unfinished process whose needs can be satisfied
by the available resources. If no such process exists, just go to step 4.
Perform the following:

5. Work = Work + Allocationi


6. Finish[i] = true

● 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.

Resource Request Algorithm

● Now the next algorithm is a resource-request algorithm and it is mainly used


to determine whether requests can be safely granted or not.
● Let Requesti be the request vector for the process Pi. If Requesti[j]==k, then
process Pi wants k instance of Resource type Rj.When a request for resources
is made by the process Pi, the following are the actions that will be taken:

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.

Sleeping Barber Problem


● This problem is based on a hypothetical barbershop with one barber.
● When there are no customers the barber sleeps in his chair. If any customer
enters he will wake up the barber and sit in the customer chair.
CO3 U
● If there are no chairs empty they wait in the waiting queue.
Dining Philosopher’s problem
● This problem states that there are K number of philosophers sitting around a
circular table with one chopstick placed between each pair of philosophers.
The philosopher will be able to eat if he can pick up two chopsticks that are
adjacent to the philosopher.
● This problem deals with the allocation of limited resources.

Readers and Writers Problem


This problem occurs when many threads of execution try to access the same shared
resources at a time. Some threads may read, and some may write. In this scenario, we
may get faulty outputs.
Principles of deadlock

● System Model, Deadlock Characterization:


● Deadlock Characterization
● Elimination of “No-preemption” Condition
● High Cost
● Elimination of “Circular Wait” Condition
● Detection and avoidance

5 Write about critical section problem and monitors. CO3 U

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.

You might also like