Unit 3
Unit 3
When the count for the semaphore goes to 0, all resources are being used. After that, processes that
wish to use a resource will block until the count becomes greater than 0.
Semaphore Usage
• The value of a counting semaphore can range over an unrestricted
domain. The value of a binary semaphore can range only between 0 and 1.
• Binary semaphores can be used instead for providing mutual exclusion.
• Counting semaphores can be used to control access to a given resource
consisting of a finite number of instances.
• The semaphore is initialized to the number of resources available.
• Each process that wishes to use a resource performs a wait() operation on
the semaphore
• When a process releases a resource, it performs a signal() operation
To solve
For example, consider two concurrently running processes: P1 with a statement S1 and P2
with a statement S2. Suppose we require that S2 be executed only after S1 has completed.
Solution : We can implement this scheme readily by letting P1 and P2 share a common
semaphore synch, initialized to 0. In process P1, we insert the statements
Process P1
S1;
signal(synch);
Process P2
wait (synch);
S2;
Semaphore Implementation to prevent busy
waiting
• When a process executes the wait() operation and finds that the semaphore
value is not positive, it must wait.
• However, rather than engaging in busy waiting, the process can block itself.
• The block operation places a process into a waiting queue associated with the
semaphore, and the state of the process is switched to the waiting state;
• A process that is blocked, waiting on a semaphore S, should be restarted
when some other process executes a signal()operation.
• The process is restarted by a wakeup() operation, which changes the process
from the waiting state to the ready state.
Semaphore Definition of wait and signal
When P0
executes wait(Q),
P1 executes it must wait until
wait(S), it P1 executes
must wait signal(Q).
until P0
executes
signal(S).
• Solution:
All processes that are accessing resources needed by a higher-priority
process inherit the higher priority until they are finished with the resources.
When they are finished, their priorities revert to their original values.
Classic problems of synchronization
• Producer Consumer Problem
• The Readers–Writers Problem
• Dining Philosopher Problem
Producer Consumer Problem
We can interpret this code as the producer producing full buffers for the consumer or as the consumer
producing empty buffers for the producer.
The Readers–Writers Problem
• Suppose that 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.
• We distinguish between these two types of processes by referring
to the former as readers and to the latter as writers.
• The writers should have exclusive access to the shared database
while writing to the database.
Readers and Writers
• first readers–writers problem, requires that no reader be kept waiting
unless a writer has already obtained permission to use the shared object.
no reader should wait for other readers to finish simply because a writer is
waiting
• Second once a writer is ready, that writer perform its write as soon as
possible. In other words, if a writer is waiting to access the object, no new
readers may start reading.
Solution to Readers and Writers Problem
Condition to delay when philosopher is hungry but unable to get the chopsticks
Deadlock
• A process requests resources; if the resources are not available at that time,
the process enters a waiting state.
• Sometimes, a waiting process is never again able to change state, because
the resources it has requested are held by other waiting processes. This
situation is called a deadlock.
Deadlock – No progress
System Model
Under the normal mode of operation, a process may utilize a resource in
only the following sequence:
1. Request. The process requests the resource. If the request cannot be
granted immediately (for example, if the resource is being used by
another process), then the requesting process must wait until it can
acquire the resource.
2. Use. The process can operate on the resource (for example, if the resource
is a printer, the process can print on the printer).
3. Release. The process releases the resource.
Necessary Conditions for Deadlock
Rj Pi
Request Edge
Assignment Edge
Pi Rj
A simple RAG
The sets P, R, and E:
P = { P1, P2, P3 }
R = {R1, R2, R3, R4 }
E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3 }
• Resource instances:
✓ One instance of resource type R1
✓ Two instances of resource type R2
✓ One instance of resource type R3
✓ Three instances of resource type R4
• Process states:
✓ Process P1 is holding an instance of resource type R2 and is
waiting for an instance of resource type R1.
✓ Process P2 is holding an instance of R1 and an instance of R2
and is waiting for an instance of R3.
✓ Process P3 is holding an instance of R3.
Conditions for Deadlock
• Given the definition of a resource-allocation graph, it can be shown that, if the
graph contains no cycles, then no process in the system is deadlocked.
• If the graph does contain a cycle, then a deadlock may exist.
• If each resource type has exactly one instance, then a cycle implies that a
deadlock has occurred.
• If the cycle involves only a set of resource types, each of which has only a single
instance, then a deadlock has occurred. Each process involved in the cycle is
deadlocked. In this case, a cycle in the graph is both a necessary and a sufficient
condition for the existence of deadlock.
• If each resource type has several instances, then a cycle does not necessarily
imply that a deadlock has occurred. In this case, a cycle in the graph is a
necessary but not a sufficient condition for the existence of deadlock.
RAG cyclic with deadlock
Two minimal cycles exist in this RAG
II. A process may request some resources and use them. Before it
can request any additional resources, it must release all the
resources that it is currently allocated.
Problem with first protocol
We consider a process that copies data from pen drive to a file on disk, sorts
the file, and then prints the results to a printer
• If all resources must be requested at the beginning of the process, then the
process must initially request the pen drive, disk file, and printer. It will
hold the printer for its entire execution, even though it needs the printer
only at the end.
• Problem is resource utilization may be low, since resources may be
allocated but unused for a long period.
Problem with second protocol
The second protocol allows the process to request initially only the pen
drive and disk file.
• Process copies from the pen drive to the disk and then releases both the
pen drive and the disk file. The process must then request the disk file and
the printer.
• After copying the disk file to the printer, it releases these two resources
and terminates.
• Problem is starvation is possible. A process that needs several popular
resources may have to wait indefinitely, because at least one of the
resources that it needs will be always allocated to some other process.
4. Circular Wait
• One way to ensure that this condition never holds is to impose a total
ordering of all resource types and to require that each process requests
resources in an increasing order of enumeration.
• Process can initially request any number of instances of a resource type
say, Ri. After that, the process can request instances of resource type Rj if
and only if F(Rj ) > F(Ri ).
• Then process requesting an instance of resource type Rj must have
released any resources Ri such that F(Ri ) ≥ F(Rj ).
• If several instances of the same resource type are needed, a single request
for all of them must be issued.
• If these two protocols are used, then the circular-wait condition cannot
hold.
Deadlock Avoidance
• Possible side effects of preventing deadlocks by this method, however, are
low device utilization and reduced system throughput.
• An alternative method for avoiding deadlocks is to require additional
information about how resources are to be requested.
• A deadlock-avoidance algorithm dynamically examines the resource-
allocation state to ensure that a circular-wait condition can never exist.
• The resource allocation state is defined by the number of available and
allocated resources and the maximum demands of the processes.
Safe , Unsafe states
• A state is safe if the system can allocate resources to each process (up to its
maximum) in some order and still avoid a deadlock. More formally, a
system is in a safe state only if there exists a safe sequence.
• When Pi terminates, Pi+1 can obtain its needed resources, and so on. If no
such sequence exists, then the system state is said to be unsafe.
Banker’s Algorithm
• Deadlock avoidance Algorithm
• The name was chosen because the algorithm could be used in a
banking system to ensure that the bank never allocated its available
cash in such a way that it could no longer satisfy the needs of all its
customers.
• When a new process enters the system, it must declare the maximum
number of instances of each resource type that it may need.
• This number may not exceed the total number of resources in the
system.
• When a user requests a set of resources, the system must determine
whether the allocation of these resources will leave the system in a
safe state.
Data Structures in Banker’s Algorithm
Deadlock Detection
• If a system does not employ either a deadlock-prevention or a deadlock
avoidance algorithm, then a deadlock situation may occur.
In this environment, the system may provide:
1. An algorithm that examines the state of the system to determine whether a deadlock
has occurred
2. An algorithm to recover from the deadlock
Deadlock Detection – Wait for Graph
• We obtain this graph from the resource-allocation graph by removing the
resource nodes and collapsing the appropriate edges.
• An edge Pi → Pj exists in a wait-for graph if and only if the RAG contains
two edges Pi → Rq and Rq → Pj for some resource Rq
Wait for Graph
• A deadlock exists in the system if and only if the wait-for graph
contains a cycle.
• 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.
• Can be used for resources have only a single instance.
Recovery from Deadlock
Process Termination
Resource Preemption