Thread Synchronization and Deadlock Management
Thread Synchronization and Deadlock Management
OS treats different process differently All user level threads treated as single
task for OS
Different process have different copy of Threads shares same copy of data and
data, files, and code code
Blocking of one process will not block Blocking of one thread will block entire
another process thread
Independent Interdependent
User level threads are managed by user level Kernel level threads are managed by OS (Using
library ( Using Procedure call) System call)
User level threads are faster in executions Kernel level threads are slower due to system
configuration and its execution parameters
If one user level threads performs blocking, then If one system level threads performs blocking,
entire process gets blocked then process does not get blocked
Process Contention Scope (PCS):- User
Thread Pool
• Each transactions must execute in a separate threat
• Each transaction might be assigned with unique thread
• Synchronization between thread is important here
Thread Specific data
In the following figure, thread T2 has a thread-specific data value of 12 associated with the key K3.
Thread T4 has the value of 2 associated with the same key.
Thread specific data allows each thread to possess a separate copy of data for the purpose of ensuring
isolated data and independent thread.
One challenge with thread-specific data is that when using
dynamically allocated thread, the programmer must ensure the key
allocation at run time with different data and threads
Schedular Activation
• The final issue is considered with multithreaded concept with
concern of kernel thread and user thread
• Here number of kernel thread must adjust dynamically to ensure
the best performance of user thread
• Thread scheduler works with one-to-one, many-to-one and many-
to-many services
• This data structure typically known as light-weight process (LWP)
Thread (Completed)
Process
• Process
• A program in execution is known as process
• Types of Process
• Co-operative Process
• When two process shares same Process
resource for its execution
• Independent Process
Co-operative Independent
• When process does not shares any
Process Process
resource for its execution
Process Execution Challenges
(Co-operative Process)
int shared= 5
Process (p1) Process (p2)
Here Both Processes
int x=shared; int y=shared; are Running Simultaneously
x++; y--;
sleep(1); sleep(1);
shared=x; shared=y;
Terminated Terminated
write(a) Terminated
Terminated
Process Synchronization
PROCESS
When more than one process executes in our system
Then there two modes of operations are performed namely
Code
Resources
Process Synchronization (Needs)
• Critical Section Problem and Mutual Exclusion
int shared= 5 A Program consists of Private section and
Shared resources.
x++; y--;
Program
sleep(1); sleep(1); Private Section
Private Code (Independent
shared=x; shared=y; code Process)
Terminated Terminated
Critical Section
Shared
Code (Co-operative
Code
Process)
Critical Section Problem (Solution Approach)
• These are the three criteria must be satisfied for solving critical
section problem
• Mutual exclusion (Mandatory condition) (Process should access critical
section one after another)
• Progress (Mandatory condition) (only those process should be entered in
critical section who wants to use resources of critical section else not)
• Bounded wait (Optional condition) (There must be a maximum bound a
process can wait to get access of critical section)
P0 P0 P1
{ while(1) while(1)
entry section { {
CS while(turn!=0); while(turn!=1);
exit section Critical Section Critical Section
Remainder Section turn=1 turn=0
} Remainder Section Remainder Section
} }
Flag Boolean variable
P0 P1 0 1
F F
while(1) while(1)
Flag
{ {
flag[0]=T flag[1]=T
while(flag[1]); while(flag[0]);
Critical Section Critical Section
flag[0]=F flag[1]=F
Remainder Section Remainder Section
} }
Peterson’s Solution
P0 P1
while(1) while(1)
{ {
flag[0]=T flag[1]=T
turn=1 turn=0
while( turn==1 && flag[1]==T); while(turn==0 && flag[0]==T);
Critical Section Critical Section Turn= 0/1
flag[0]=F flag[1]=F
Remainder Section Remainder Section
} }
Dekker’s Solution
Semaphores
A semaphores is an integer variable that apart from initialization, is accessed only through two
standard atomic operation i.e.; wait and signal.
Here S is semaphore variable
wait(s) signal(s) int s=1 Semaphore value must start with 1
{ { Use of Semaphores
• Critical Section Problem
while(s<=0); s=s+1; • Order of execution of Process
• Resource management
s=s-1; }
}
signal(s)
Semaphores (Critical Section Solution) {
s=s+1;
}
do do
{ {
entry section wait(s);
CS CS wait(s)
exit section signal(s); {
Remainder Section Remainder Section while(s<=0);
}while(T); }while(T); s=s-1;
}
P1 P2 P3 No. of Process
Semaphore
(Order of Process execution Solution)
P1 P2 P3
signal(s)
Order of execution {
s=s+1;
s1=0 s2=0 P2 P1 P3
}
while(T) 1 1 1 1 1
{
chopstick
wait( chopstick [i]);
wait( chopstick [ (i+1) %5 ] );
eat();
signal( chopstick [i]);
signal( chopstick [ (i+1) %5 ] );
think();
}
Dining Philosopher Problem
Indian Chopstick
Dining Problem Solution
• Allow four philosopher at a time on dining table
• Allow six chopstick on the table
• Allow philosopher to pick the chopstick when both the chopsticks
are available
• All philosopher pick the right chopstick except last one ( last one
has to pick the left chopstick, if available then he has to pick left
right chopstick). – standard solution
Dining Philosopher problem (second solution)
The sleeping barber problem
Home Work
Problem statement
How to synchronize such that
one customer can come for
cutting
Working of Shared memory and Message
Passing
Give Service
Necessary condition of deadlock
• A deadlock can occur if all these four conditions occur in the
system simultaneously
• Mutual exclusion
• Hold and wait
• No pre-emption
• Circular wait
Mutual exclusion
• At least one resources must be held in a
non-sharable mode that is only one
process at a time can use the resource
• If another process request that
resource, the requesting process must
be delayed until the resource has been
released. And resource must be desired
by more than one process.
Hold and wait Hold
processes.
P1
Hold R2
No Pre-emption
• Resources can not be pre-empted
that is a resource can be released
only voluntarily by the process
holding it, after that process has
completed its task.
P1 P2
R1 R2
• We check whether they are allocated to some other process that is waiting
for some additional resources. If so, we pre-empt the desired resources from
the waiting process and allocate them to the requesting process
(Considering priority)
• If the resources are neither available nor held by a waiting process, the
requesting process must wait, or may allow to pre-empt resource of a
running process considering priority
Prevention (Circular wait)
• We can eliminate circular wait problem by giving a natural number
mapping to every resource and then any process can request only
in the increasing order and if a process wants a lower number,
then process must first release all the resources larger than that
number and give a fresh request.
Problem with prevention
• Different deadlock prevention
approach put different type of
restrictions or conditions on the
processes and resources
because of which system
becomes slow, does not make
proper resource utilization, and
reduces system throughput.
Deadlock Avoidance
Banker Algorithm
Banker Algorithm
• In order to, avoid deadlock in run
time, system try to maintain some
books like a banker, whenever
someone ask for loan (resource), it
is granted only when the books
allow.
Deadlock avoidance (Banker Algorithm)
• To avoiding deadlock, we require additional information about
how resources are to be requested, which resources a process
will request during its lifetime
• With this additional knowledge, the operating system can decide
for each request whether process should wait or not.
Banker Algorithm
Safe Sequence
• Some sequence in which we can
satisfies demand of every process
without going into deadlock, if yes,
this sequence is known as safe
sequence.
• Safe sate : If there exist at least one
safe sequence
• Unsafe state: If there exist no
possible safe sequence
Banker Algorithm (Find the safe sequence)
There are 3 units of type X, 2 units of type Y and 2 units of type Z still available. The system is
currently in a safe state. Consider the following independent requests for additional resources in the
current state:
REQ1: P0 requests 0 units of X, 0 units of Y and 2 units of Z
REQ2: P1 requests 2 units of X, 0 units of Y and 0 units of Z
Safety Algorithm
Safety Algorithm
P1 0 1 1 0
P2 1 0 0 0
P3 1 0 0 1
P4 0 1 0 0
Current Available Resource ( R1, R2) = (0, 0) Max Resource ( R1, R2) = (2, 2)
Question
Find whether system is in deadlock state Find whether system is in deadlock state
Find the safe sequence Find the safe sequence
Question
{ { Use of Semaphores
• Critical Section Problem
while(s<=0); s=s+1; • Order of execution of Process
• Resource management
s=s-1; }
}
Semaphore
(Violating rules of Mutual Exclusion)
signal(s)
{
s=s+1;
s=1 P2 P1 P3
}
wait(s) P2 P3 wait(s)
P1 signal(s) signal(s) {
signal(s) while(s<=0);
s=s-1;
}
Difference between Mutex and Semaphore
Mutex Semaphore
A mutex is an object A semaphore is an integer
Mutex works upon the locking mechanism Semaphore uses signaling mechanism
Operation Operation
Lock Wait
Unlock Signal
Mutex does not have any subtypes Semaphore is of two types
Counting Semaphore
Binary Semaphore
A mutex can only be modified by the process that is Semaphore works with two atomic operations
requesting or releasing a resource (wait and signal) which can modify it
If the mutex is locked then the process needs to wait in the If the process needs a resource, and no resource
process queue, and mutex can only be accessed once the is free. So, the process needs to perform a wait
lock is released operation until the semaphore value is greater
then zero.
Monitor
• Monitors are a higher-level synchronization
construct that simplifies process
synchronization by providing a high-level
abstraction for data access and
synchronization.
• Monitors are implemented as programming
language constructs, typically in object-
oriented languages, and provide mutual
exclusion, condition variables, and data
encapsulation in a single construct.
• The key advantage of using monitors for
process synchronization is that they provide a
simple, high-level abstraction that can be
used to implement complex concurrent
systems.
Monitor