BCA Kiruthiga OS Process-Sync
BCA Kiruthiga OS Process-Sync
Process Synchronization
UNIT II: Process Synchronization: Critical-Section problem - Synchronization
Hardware – Semaphores – Classic Problems of Synchronization – Critical Region –
Monitors.
Contents
Producer Consumer
(process (process B)
A)
Buffer
Race Condition
ENTRY SECTION
CRITICAL SECTION
EXIT SECTION
REMAINDER SECTION
PETERSON’S SOLUTION
FLAG[i] =
false
Synchronization Hardware
• Problems of Critical Section are also solvable by hardware.
MUTEX LOCKS
• As the synchronization hardware solution is not easy to
implement from everyone, a strict software approach
called Mutex Locks was introduced. In this approach, in
the entry section of code, a LOCK is acquired over the
critical resources modified and used inside critical
section, and in the exit section that LOCK is released.
Semaphores as general
Synchronization Tool
• TYPES
Semaphores are mainly of two types:
1. Binary Semaphore
It is a special form of semaphore used for implementing mutual
exclusion, hence it is often called Mutex. A binary semaphore is
initialized to 1 and only takes the value 0 and 1 during execution of
a program.
2. Counting Semaphores
These are used to implement bounded concurrency.
Semaphores
• FORMAT:
wait ( mutex ); // Mutual exclusion: mutex init to 1.
CRITICAL SECTION
signal( mutex );
REMAINDER
• Note that not only must the variable-changing steps ( S-- and
S++ ) be indivisible, it is also necessary that for the wait
operation when the test proves false that there be no
interruptions before S gets decremented. It S okay, however,
for the busy loop to be interrupted when the test is true, which
prevents the system from hanging forever
Semaphores
• Properties (Characteristics)
1. Simple
2. Works with many processes
3. Can have many different critical sections
with different semaphores
4. Each critical section has unique access semaphores
5. Can permit multiple processes into the critical section at
once, if desirable.
Semaphores
• Semaphores can be used to force synchronization (precedence )
if the preceding process does a signal at the end, and the
follower does wait at beginning. For example, here we want P1
to execute before P2.
P1: P2:
statement 1; wait (synch );
signal ( synch ); statement 2;
typedef struct {
int value;
struct process *list; //linked list of PTBL waiting on
} SEMAPHORE; S
Semaphores
SEMAPHORE s; SEMAPHORE s;
wait(s) { signal(s) {
s.value = s.value - 1; s.value = s.value + 1;
if ( s.value < 0 ) { if ( s.value <= 0 ) {
add this process to s.L; remove a process P from s.L;
block; wakeup(P);
} }
} }
Semaphores
• DEADLOCK
• One important problem that can arise when using semaphores to block
processes waiting for a limited resource is the problem of deadlocks, which
occur when multiple processes are blocked, each waiting for a resource that
can only be freed by one of the other ( blocked ) processes, as illustrated in
the following example.
Semaphores
STARVATION
CONSUMER
PRODUCER
• Locks:are shared (for the readers) and exclusive (for the writer).
1. No reader is kept waiting unless a writer holds the lock (the readers
have precedence).
• ( NOTE: starvation can occur on either of these rules if they are followed
rigorously.)
G.Kiruthiga, Associate Professor, Dept of BCA, GNC
25
Writer:
do {
wait( wrt ); THE READERS/WRITERS PROBLEM:
/* writing is performed BINARY_SEMAPHOR wrt = 1;
*/ signal( wrt ); E mutex = 1;
BINARY_SEMAPHOR readcount = 0;
} while(TRUE); E
G.Kiruthiga, Associate Professor, Dept of BCA, GNC
int
26
Synchronization Examples
• SYNCHRONIZATION IN
WINDOWS
• SYNCHRONIZATION IN
LINUX
Monitors
• High-level synchronization construct that allows the safe
sharing of an abstract data type among concurrent
processes.
Monitors
• A monitor is essentially a
class, in which all data is
private, and with the special
restriction that only one
method within any given
monitor object may be
active at the same time.
• An additional restriction is
that monitor methods may
only access the shared data
within the monitor and any
data passed to them as
parameters, i.e. they cannot
access any data external to
the monitor.
Monitors
• In order to fully realize the potential of monitors, we
need to introduce one additional new data type,
known as a condition.
Atomic Transactions
• Database operations frequently need to carry out atomic
transactions, in which the entire transaction must either
complete or not occur at all.
Atomic Transactions
• FUNDAMENTAL PRINCIPLES – A C I D
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Resource-Allocation Graph
• set
A V ofis vertices V and a set
partitioned intoof edges E.
two types:
– P = {P1, P2, …, Pn}, the set consisting of all the processes in
the system.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Resource-Allocation Graph (Cont.)
• Process
• Pi requests instance of Rj
Pi
Rj
• Pi is holding an instance of Rj
Pi
Rj
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Example of a Resource Allocation Graph
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Resource Allocation Graph With A Deadlock
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Methods for Handling Deadlocks
• Ensure that the system will never enter a
deadlock state.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Deadlock Prevention
Restrain the ways request can be made.
• Mutual Exclusion – not required for sharable
resources; must hold for non sharable resources.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Deadlock Prevention (Cont.)
• No Preemption –
– If a process that is holding some resources requests
another resource that cannot be immediately allocated to
it, then all resources currently being held are released.
– Preempted resources are added to the list of resources for
which the process is waiting.
– Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Safe State
• When a process requests an available resource, system
must decide if immediate allocation leaves the system in a
safe state.
• System is in safe state if there exists a safe sequence of all
processes.
• Sequence <P1, P2, …, Pn> is safe if for each Pi, the
resources that Pi can still request can be satisfied by
currently available resources + resources held by all the Pj,
with j<I.
– If Pi resource needs are not immediately available, then
Pi can wait until all Pj have finished.
– When Pj is finished, Pi can obtain needed resources,
execute, return allocated resources, and terminate.
G.Kiruthiga, Associate Professor, Dept of BCA, GNC
– When Pi terminates, Pi+1
Operating System
can
Concepts
obtain its needed
Basic Facts
• If a system is in safe state ⇒ no deadlocks.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Safe, Unsafe , Deadlock State
• Detection algorithm
• Recovery scheme
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Single Instance of Each Resource Type
• Maintain wait-for graph
– Nodes are processes.
– Pi → Pj if Pi is waiting for Pj.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Resource-Allocation Graph and Wait-for Graph
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi ≠ 0, then
Finish[i] = false;otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti ≤ Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Detection Algorithm (Cont.)
Algorithm requires an order of O(m x n2) operations to detect whether
the system is in deadlocked state.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Example of Detection Algorithm
• Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances).
• Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for
all i.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Example (Cont.)
• P2 requests an additional instance of type C.
Request
ABC
P0 000
P1 201
P2 001
P3 100
P4 002
• State of system?
– Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests.
– Deadlock exists, consisting of processes P1, P2, P3, and P4.
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Detection-Algorithm Usage
• When, and how often, to invoke depends on:
– How often a deadlock is likely to occur?
– How many processes will need to be rolled back?
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Recovery from Deadlock: Process Termination
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Recovery from Deadlock: Resource Preemption
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Combined Approach to Deadlock Handling
G.Kiruthiga, Associate
Operating System Professor, Dept of BCA, GNC
Concepts
Traffic Deadlock for Exercise 8.4