Module 3
Module 3
Process Synchronization
2.1 Synchronization
2.2 The Critical-Section Problem
2.3 Peterson‘s Solution
2.4 Synchronization Hardware
2.4.1 Hardware based Solution for Critical-section Problem
2.4.2 Hardware instructions for solving critical-section problem
2.4.2.1 TestAndSet()
2.4.2.2 TestAndSet with Mutual Exclusion
2.4.2.3 Swap()
2.4.2.4 Bounded waiting Mutual Exclusion with TestAndSet()
2.5 Semaphores
2.5.1 Semaphore Usage
2.5.2 Semaphore Implementation
2.5.3 Deadlocks & Starvation
2.6 Classic Problems of Synchronization
2.6.1 Bounded-Buffer Problem
2.6.2 Readers-Writers Problem
2.6.3 Dining-Philosophers Problem
3.1 Deadlocks
3.2 System Model
3.3 Deadlock Characterization
3.3.1 Necessary Conditions
3.3.2 Resource Allocation Graph
3.4 Methods for Handling Deadlocks
3.5 Deadlock Prevention
3.5.1 Mutual Exclusion
3.5.2 Hold and Wait
3.5.3 No Preemption
3.5.4 Circular Wait
3.6 Deadlock Avoidance
3.6.1 Safe State
3.6.2 Resource Allocation Graph Algorithm
3.6.3 Banker's Algorithm
3.6.3.1 Safety Algorithm
3.6.3.2 Resource Request Algorithm
3.6 3.3 An Illustrative Example
3.7 Deadlock Detection
3.7.1 Single Instance of Each Resource Type
3.7.2 Several Instances of a Resource Type
3.7.3 Detection Algorithm Usage
3.8 Recovery from Deadlock
3.8.1 Process Termination
3.8.2 Resource Preemption
2.1 Synchronization
• Co-operating process is one that can affect or be affected by other processes.
• Co-operating processes may either
→ share a logical address-space (i.e. code & data) or
→ share data through files or
→ messages through threads.
• Concurrent-access to shared-data may result in data-inconsistency.
• To maintain data-consistency:
The orderly execution of co-operating processes is necessary.
• Suppose that we wanted to provide a solution to producer-consumer problem that fills all buffers.
We can do so by having an variable counter that keeps track of the no. of full buffers.
Initially, counter=0.
counter is incremented by the producer after it produces a new buffer.
counter is decremented by the consumer after it consumes a buffer.
• Shared-data:
• A situation where several processes access & manipulate same data concurrently and the outcome of
the execution depends on particular order in which the access takes place, is called a race condition.
• Example:
counter++ could be implemented as: counter— may be implemented as:
• The value of counter may be either 4 or 6, where the correct result should be 5. This is an example
for race condition.
• To prevent race conditions, concurrent-processes must be synchronized.
2.2 Critical-Section Problem
• Critical-section is a segment-of-code in which a process may be
→ changing common variables
→ updating a table or
→ writing a file.
• Each process has a critical-section in which the shared-data is accessed.
• General structure of a typical process has following (Figure 2.12):
1) Entry-section
Requests permission to enter the critical-section.
2) Critical-section
Mutually exclusive in time i.e. no other process can execute in its critical-section.
3) Exit-section
Follows the critical-section.
4) Remainder-section
• Problem statement:
―Ensure that when one process is executing in its critical-section, no other process is to be allowed
to execute in its critical-section‖.
• A solution to the problem must satisfy the following 3 requirements:
1) Mutual Exclusion
Only one process can be in its critical-section.
2) Progress
Only processes that are not in their remainder-section can enter their critical-section, and the
selection of a process cannot be postponed indefinitely.
3) Bounded Waiting
There must be a bound on the number of times that other processes are allowed to enter
their critical-sections after a process has made a request to enter its critical-section and before
the request is granted.
• Two approaches used to handle critical-sections:
1) Preemptive Kernels
Allows a process to be preempted while it is running in kernel-mode.
2) Non-preemptive Kernels
Does not allow a process running in kernel-mode to be preempted.
2.3 Peterson’s Solution
• This is a classic software-based solution to the critical-section problem.
• This is limited to 2 processes.
• The 2 processes alternate execution between
→ critical-sections and
→ remainder-sections.
• The 2 processes share 2 variables (Figure 2.13):
section‖.
Figure 2.14: Solution to the critical-section problem using locks
2.13.2.1 TestAndSet()
• This instruction is executed atomically (Figure 2.15).
• If two TestAndSet() are executed simultaneously (on different CPU), they will be executed
sequentially in some arbitrary order.
• When one process modifies the semaphore-value, no other process can simultaneously modify that
same semaphore-value.
• Also, in the case of wait(S), following 2 operations must be executed without interruption:
1) Testing of S(S<=0) and
2) Modification of S (S--)
2.14.1 Semaphore
Usage Counting
Semaphore
• The value of a semaphore can range over an unrestricted domain
Binary Semaphore
• The value of a semaphore can range only between 0 and 1.
• On some systems, binary semaphores are known as mutex locks, as they are locks that provide
mutual-exclusion.
• Because synch is initialized to 0, P2 will execute S2 only after P1 has invoked signal (synch), which is
after statement S1 has been executed.
where,
¤ mutex provides mutual-exclusion for accesses to the buffer-pool.
¤ empty counts the number of empty buffers.
¤ full counts the number of full buffers.
• The symmetry between the producer and the consumer.
¤ The producer produces full buffers for the consumer.
¤ The consumer produces empty buffers for the producer.
• Producer Process: Consumer Process:
2.6.2 The Readers-Writers Problem
• A data set is shared among a number of concurrent processes.
• Readers are processes which want to only read the database (DB).
Writers are processes which want to update (i.e. to read & write) the DB.
• Problem:
Obviously, if 2 readers can access the shared-DB simultaneously without any problems.
However, if a writer & other process (either a reader or a writer) access the shared-DB
simultaneously, problems may arise.
Solution:
The writers must have exclusive access to the shared-DB while writing to the DB.
• Shared-data
where,
¤ mutex is used to ensure mutual-exclusion when the variable readcount is updated.
¤ wrt is common to both reader and writer processes.
wrt is used as a mutual-exclusion semaphore for the writers.
wrt is also used by the first/last reader that enters/exits the critical-section.
¤ readcount counts no. of processes currently reading the object.
Initialization
mutex = 1, wrt = 1, readcount = 0
Writer Process: Reader Process:
• The readers-writers problem and its solutions are used to provide reader-writer locks on some
systems.
• The mode of lock needs to be specified:
2.6.2.1 read mode
When a process wishes to read shared-data, it requests the lock in read mode.
2.6.2.2 write mode
When a process wishes to modify shared-data, it requests the lock in write mode.
• Multiple processes are permitted to concurrently acquire a lock in read mode,
but only one process may acquire the lock for writing.
• These locks are most useful in the following situations:
1) In applications where it is easy to identify
→ which processes only read shared-data and
→ which threads only write shared-data.
2) In applications that have more readers than writers.
2.6.3 The Dining-Philosophers Problem
• Problem statement:
There are 5 philosophers with 5 chopsticks (semaphores).
A philosopher is either eating (with two chopsticks) or thinking.
The philosophers share a circular table (Figure 2.21).
The table has
→ a bowl of rice in the center and
→ 5 single chopsticks.
From time to time, a philosopher gets hungry and tries to pick up the 2 chopsticks that are
closest to her.
A philosopher may pick up only one chopstick at a time.
Obviously, she cannot pick up a chopstick that is already in the hand of a neighbor.
When hungry philosopher has both her chopsticks at the same time, she eats without
releasing her chopsticks.
When she is finished eating, she puts down both of her chopsticks and starts thinking again.
• Problem objective:
To allocate several resources among several processes in a deadlock-free & starvation-free manner.
• Solution:
Represent each chopstick with a semaphore (Figure 2.22).
A philosopher tries to grab a chopstick by executing a wait() on the semaphore.
The philosopher releases her chopsticks by executing the signal() on the semaphores.
This solution guarantees that no two neighbors are eating simultaneously.
Shared-data
semaphore chopstick[5];
Initialization
chopstick[5]={1,1,1,1,1}.
Figure 2.21 Situation of dining philosophers Figure 2.22 The structure of philosopher
• Disadvantage:
2.6.3.1 Deadlock may occur if all 5 philosophers become hungry simultaneously and grab
their left chopstick. When each philosopher tries to grab her right chopstick, she will be
delayed forever.
• Three possible remedies to the deadlock problem:
1) Allow at most 4 philosophers to be sitting simultaneously at the table.
2) Allow a philosopher to pick up her chopsticks only if both chopsticks are available.
3) Use an asymmetric solution; i.e. an odd philosopher picks up first her left chopstick and then
her right chopstick, whereas an even philosopher picks up her right chopstick and then her left
chopstick.
MODULE 3 conti…: DEADLOCKS
3.1Deadlocks
• Deadlock is a situation where a set of processes are blocked because each process is
→ holding a resource and
→ waiting for another resource held by some other process.
• Real life example:
When 2 trains are coming toward each other on same track and there is only one track, none of
the trains can move once they are in front of each other.
• Similar situation occurs in operating systems when there are two or more processes hold some
resources and wait for resources held by other(s).
• Here is an example of a situation where deadlock can occur (Figure 3.1).
3.3.2 Resource-Allocation-Graph
• The resource-allocation-graph (RAG) is a directed graph that can be used to describe the deadlock
situation.
• RAG consists of a
→ set of vertices (V) and
→ set of edges (E).
• V is divided into two types of nodes
1) P={P1,P2…....Pn} i.e., set consisting of all active processes in the system.
2) R={R1,R2….....Rn} i.e., set consisting of all resource types in the system.
• E is divided into two types of edges:
1) Request Edge
A directed-edge Pi → Rj is called a request edge.
Pi → Rj indicates that process Pi has requested a resource Rj.
2) Assignment Edge
A directed-edge Rj → Pi is called an assignment edge.
Rj → Pi indicates that a resource Rj has been allocated to process Pi.
• Suppose that process Pi requests resource Rj.
Here, the request for Rj from Pi can be granted only if the converting request-edge to
assignment-edge do not form a cycle in the resource-allocation graph.
• Pictorially,
→ We represent each process Pi as a circle.
→ We represent each resource-type Rj as a rectangle.
• As shown in below figures, the RAG illustrates the following 3 situation (Figure 3.3):
1) RAG with a deadlock
2) RAG with a cycle and deadlock
3) RAG with a cycle but no deadlock
(a) Resource allocation Graph (b) With a deadlock (c) with cycle but no deadlock
Figure 3.3 Resource allocation graphs
Conclusion:
1) If a graph contains no cycles, then the system is not deadlocked.
2) If the graph contains a cycle then a deadlock may exist.
Therefore, a cycle means deadlock is possible, but not necessarily present.
3.5Deadlock-Prevention
• Deadlocks can be eliminated by preventing at least one of the four required conditions:
1) Mutual exclusion
2) Hold-and-wait
3) No preemption
4) Circular-wait.
}
• These 2 protocols may be applicable for resources whose states are easily saved and restored, such
as registers and memory.
• But, these 2 protocols are generally not applicable to other devices such as printers and tape drives.
3.5.4 Circular-Wait
• Deadlock can be prevented by using the following 2 protocol:
Protocol-1
Assign numbers all resources.
Require the processes to request resources only in increasing/decreasing order.
Protocol-2
Require that whenever a process requests a resource, it has released resources with a lower
number.
• One big challenge in this scheme is determining the relative ordering of the different resources.
3.6Deadlock Avoidance
• The general idea behind deadlock avoidance is to prevent deadlocks from ever happening.
• Deadlock-avoidance algorithm
→ requires more information about each process, and
→ tends to lead to low device utilization.
• For example:
1) In simple algorithms, the scheduler only needs to know the maximum number of each
resource that a process might potentially use.
2) In complex algorithms, the scheduler can also take advantage of the schedule of exactly
what resources may be needed in what order.
• A deadlock-avoidance algorithm dynamically examines the resources allocation state to ensure that
a circular-wait condition never exists.
• The resource-allocation state is defined by
→ the number of available and allocated resources and
→ the maximum demand of each process.
• Problem:
The resource-allocation graph algorithm is not applicable when there are multiple instances
for each resource.
• Solution:
Use banker's algorithm.
3.6.3 Banker's Algorithm
• This algorithm is applicable to the system with multiple instances of each resource types.
• However, this algorithm is less efficient then the resource-allocation-graph algorithm.
• When a process starts up, it must declare the maximum number of resources that it may need.
• This number may not exceed the total number of resources in the system.
• When a request is made, the system determines whether granting the request would leave the
system in a safe state.
• If the system in a safe state,
the resources are allocated;
else
the process must wait until some other process releases enough resources.
• Assumptions:
Let n = number of processes in the system
Let m = number of resources types.
• Following data structures are used to implement the banker’s algorithm.
1) Available [m]
This vector indicates the no. of available resources of each type.
If Available[j]=k, then k instances of resource type Rj is available.
2) Max [n][m]
This matrix indicates the maximum demand of each process of each resource.
If Max[i,j]=k, then process Pi may request at most k instances of resource type Rj.
3) Allocation [n][m]
This matrix indicates no. of resources currently allocated to each process.
If Allocation[i,j]=k, then Pi is currently allocated k instances of Rj.
4) Need [n][m]
This matrix indicates the remaining resources need of each process.
If Need[i,j]=k, then Pi may need k more instances of resource Rj to complete its task.
So, Need[i,j] = Max[i,j] - Allocation[i]
• The Banker’s algorithm has two parts: 1) Safety Algorithm
2) Resource – Request Algorithm
Step 1:
Let Work and Finish be two vectors of length m and n respectively.
Initialize:
Work = Available
Finish[i] = false for i=1,2,3,…….n
Step 2:
Find an index(i) such that both
a) Finish[i] = false
b) Need i <= Work.
If no such i exist, then go to step 4
Step 3:
Set:
Work = Work + Allocation(i)
Finish[i] = true
Go to step 2
Step 4:
If Finish[i] = true for all i, then the system is in safe state.
3.6.3.2 Resource-Request Algorithm
• This algorithm determines if a new request is safe, and grants it only if it is safe to do so.
• When a request is made ( that does not exceed currently available resources ), pretend it has been
granted, and then see if the resulting state is a safe one. If so, grant the request, and if not, deny the
request.
• Let Request(i) be the request vector of process Pi.
• If Request(i)[j]=k, then process Pi wants K instances of the resource type Rj.
Step 1:
If Request(i) <= Need(i)
then
go to step 2
else
raise an error condition, since the process has exceeded its maximum claim.
Step 2:
If Request(i) <= Available
then
go to step 3
else
Pi must wait, since the resources are not available.
Step 3:
If the system want to allocate the requested resources to process Pi then modify the
state as follows:
Available = Available – Request(i)
Allocation(i) = Allocation(i) + Request(i)
Need(i) = Need(i) – Request(i)
Step 4:
If the resulting resource-allocation state is safe,
then i) transaction is complete and
ii) Pi is allocated its resources.
Step 5:
If the new state is unsafe,
then i) Pi must wait for Request(i) and
ii) old resource-allocation state is restored.
3.6.3.3 An Illustrative Example
Question: Consider the following snapshot of a system:
Allocation Max Available
A B C A B C A B C
P 0 1 0 7 5 3 3 3 2
0
P 2 0 0 3 2 2
1
P 3 0 3 9 0 2
2
P 2 1 1 2 2 2
3
P 0 0 2 4 3 3
4
Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
Solution (ii):
• Applying the Safety algorithm on the given system,
Step 1: Initialization
Work = Available i.e. Work =3 3 2
……P0………P1……..P2……..P3……P4…..
Finish = | false | false | false | false | false |
• 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 execute an algorithm that searches for a cycle in the graph.
3.7.2 Several Instances of a Resource Type
• The wait-for-graph is applicable to only a single instance of a resource type.
• Problem: However, the wait-for-graph is not applicable to a multiple instance of a resource type.
• Solution: The following detection-algorithm can be used for a multiple instance of a resource type.
• Assumptions:
Let ‘n’ be the number of processes in the system
Let ‘m’ be the number of resources types.
• Following data structures are used to implement this algorithm.
1) Available [m]
This vector indicates the no. of available resources of each type.
If Available[j]=k, then k instances of resource type Rj is available.
2) Allocation [n][m]
This matrix indicates no. of resources currently allocated to each process.
If Allocation[i,j]=k, then Pi is currently allocated k instances of Rj.
3) Request [n][m]
This matrix indicates the current request of each process.
If Request [i, j] = k, then process Pi is requesting k more instances of resource type Rj.
Step 1:
Let Work and Finish be vectors of length m and n respectively.
a) Initialize Work = Available
b) For i=0,1,2…......n
if Allocation(i) != 0
then
Finish[i] = false;
else
Finish[i] = true;
Step 2:
Find an index(i) such that both
a) Finish[i] = false
b) Request(i) <= Work.
If no such i exist, goto step 4.
Step 3:
Set:
Work = Work + Allocation(i)
Finish[i] = true
Go to step 2.
Step 4:
If Finish[i] = false for some i where 0 < i < n, then the system is in a deadlock state.
Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C
P0 0 0 2
P1 1 0 1
P2 0 0 2
P3 2 1 0
P4 0 1 4
Solution (ii):
• Applying the Safety algorithm on the given system,
Step 1: Initialization
Work = Available i.e. Work =1 0 2
……P0………P1……..P2……..P3……P4…..
Finish = | false | false | false | false | false |
2) For the following snapshot, find the safe sequence using Banker's algorithm:
The number of resource units is (A, B, C) which are (7, 7, 10) respectively.
Allocation Max Available
A B C A B C A B C
P1 2 2 3 3 6 8 7 7 10
P2 2 0 3 4 3 3
P3 1 2 4 3 4 4
Solution:
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C
P1 1 4 5
P2 2 3 0
P3 2 2 0
• Applying the Safety algorithm on the given system,
Step 1: Initialization
Here, m=3, n=3
Work = Available i.e. Work =7 7 10
….P1………..P2…. .P3…
Finish = | false | false | false |
Step 2: For i=1
Finish[P1] = false and Need[P1]<=Work i.e. (1 4 5)<=(7 7 10) true So
P1 must be kept in safe sequence.
Step 3: Work = Work + Allocation[P1] =(7 7 10)+(2 2 3)=(9 9 13)
……P1……P2……….P3….
Finish = | true | false | false |
Page 20
[Type the document title]
Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C
P0 0 0 0
P1 0 0 2
P2 0 0 0
P3 0 0 0
P4 0 0 0
Solution (ii):
• Applying the Safety algorithm on the given system,
Step 1: Initialization
Work = Available i.e. Work =0 0 0
……P0………P1…….P2…. P3……P4…
Finish = | false | false | false | false | false |
Page 21
[Type the document title]
…P0……P1………P2…….P3…….P4…
Finish = | true | false | true | true | false |
A B C D A B C D A B C D
P1 0 0 1 2 0 0 1 2 1 5 2 0
P2 1 0 0 0 1 7 5 0
P3 1 3 5 4 2 3 5 6
P4 0 6 3 2 0 6 5 2
P5 0 0 1 4 0 6 5 6
Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C D
P 0 0 0 0
1
P 0 7 5 2
2
P 1 0 0 2
3
P 0 0 2 0
4
P 0 6 4 2
5
Solution (ii):
• Applying the Safety algorithm on the given system,
Step 1: Initialization
Work = Available i.e. Work =1 5 2 0
....P1………P2…….P3……….P4…..P5…..
Finish = | false | false | false | false | false |
Page 23
[Type the document title]
Page 24
[Type the document title]
5) Consider a system containing ‘m’ resources of the same type being shared by ‘n’ processes.
Resources can be requested and released by processes only one at a time. Show that the system is
deadlock free if the following two conditions hold:
i) The maximum need of each process is between 1 and m resources
ii) The sum of all maximum needs is less than m+n.
Page 25
[Type the document title]
Ans:
• Suppose N = Sum of all Needi
A = Sum of all Allocationi
M = Sum of all Maxi.
• Use contradiction to prove: Assume this system is not deadlock free.
• If there exists a deadlock state, then A=m because there's only one kind of resource and resources
can be requested and released only one at a time.
• From condition (ii), N+A = M<m+n
• So we get N+m <m +n.
• So we get N < n.
• It shows that at least one process i that Needi=0.
• From condition (i), Pi can release at least one resource.
• So, there are n-1 processes sharing ‘m’ resources now, condition (i) and (ii) still hold.
• Go on the argument, no process will wait permanently, so there's no deadlock.
6) Consider the traffic deadlock depicted in the figure given below, explain that the four necessary
conditions for dead lock indeed hold in this examples.
Ans:
• The four necessary conditions for a deadlock are:
1) Mutual exclusion
2) Hold-and-wait
3) No preemption and
4) Circular-wait.
• The mutual exclusion condition holds since only one car can occupy a space in the roadway.
• Hold-and-wait occurs where a car holds onto its place in the roadway while it waits to advance in the
roadway.
• A car cannot be removed (i.e. preempted) from its position in the roadway.
• Lastly, there is indeed a circular-wait as each car is waiting for a subsequent car to advance.
• The circular-wait condition is also easily observed from the graphic.
Page 26