Synchronization
Synchronization
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 6.2 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 6.3 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 6.4 Silberschatz, Galvin and Gagne ©2018
Critical Section Critical-Section Problem (Cont.)
Operating System Concepts – 10th Edition 6.5 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 6.6 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 6.7 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 6.8 Silberschatz, Galvin and Gagne ©2018
Semaphore Semaphore (Cont.)
Synchronization tool that provides more sophisticated ways Counting semaphore – integer value can range over
(than Mutex locks) for processes to synchronize their activities. an unrestricted domain
Semaphore S – integer variable Binary semaphore – integer value can range only
Can only be accessed via two indivisible (atomic) operations between 0 and 1
• wait() and signal() • Same as a mutex lock
Originally called P() and V()
Can implement a counting semaphore S as a binary
Definition of the wait() operation semaphore
wait(S) { With semaphores we can solve various synchronization
while (S <= 0) problems
; // busy wait
S--;
}
Definition of the signal() operation
signal(S) {
S++;
}
Operating System Concepts – 10th Edition 6.9 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 6.10 Silberschatz, Galvin and Gagne ©2018
signal(synch);
P2:
wait(synch);
S 2;
Operating System Concepts – 10th Edition 6.11 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 6.12 Silberschatz, Galvin and Gagne ©2018
Semaphore Implementation with no Busy waiting Classical Problems of Synchronization
With each semaphore there is an associated waiting queue Classical problems used to test newly-proposed
Each entry in a waiting queue has two data items: synchronization schemes
• Value (of type integer) • Bounded-Buffer Problem (Not discussed)
• Pointer to next record in the list • Readers and Writers Problem
Two operations: • Dining-Philosophers Problem (Not discussed)
• block – place the process invoking the operation on the
appropriate waiting queue
• wakeup – remove one of processes in the waiting queue
and place it in the ready queue
Operating System Concepts – 10th Edition 6.13 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 7.14 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 7.15 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 7.16 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.) Readers-Writers Problem (Cont.)
Shared Data
The structure of a writer process
• Data set
• Semaphore rw_mutex initialized to 1
while (true) {
• Semaphore mutex initialized to 1 wait(rw_mutex);
• Integer read_count initialized to 0 ...
/* writing is performed */
...
signal(rw_mutex);
}
Operating System Concepts – 10th Edition 7.17 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 7.18 Silberschatz, Galvin and Gagne ©2018
The structure of a reader process The solution in previous slide can result in a situation where
while (true){ a writer process never writes. It is referred to as the “First
wait(mutex); reader-writer” problem.
read_count++;
if (read_count == 1) /* first reader */ The “Second reader-writer” problem is a variation the first
reader-writer problem that state:
wait(rw_mutex);
signal(mutex); • Once a writer is ready to write, no “newly arrived
reader” is allowed to read.
...
/* reading is performed */ Both the first and second may result in starvation. leading to
... even more variations
wait(mutex); Problem is solved on some systems by kernel providing
read_count--; reader-writer locks
if (read_count == 0) /* last reader */
signal(rw_mutex);
signal(mutex);
}
Operating System Concepts – 10th Edition 7.19 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 7.20 Silberschatz, Galvin and Gagne ©2018
System Model
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.22 Silberschatz, Galvin and Gagne ©2018
• A semaphore S1 initialized to 1 Mutual exclusion: only one thread at a time can use a
• A semaphore S2 initialized to 1 resource
Two threads T1 and T2 Hold and wait: a thread holding at least one resource is
waiting to acquire additional resources held by other threads
T1:
No preemption: a resource can be released only voluntarily
wait(s1) by the thread holding it, after that thread has completed its
wait(s2) task
T2: Circular wait: there exists a set {T0, T1, …, Tn} of waiting
wait(s2) threads such that T0 is waiting for a resource that is held by
T1, T1 is waiting for a resource that is held by T2, …, Tn–1 is
wait(s1) waiting for a resource that is held by Tn, and Tn is waiting for a
resource that is held by T0.
Operating System Concepts – 10th Edition 8.23 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.24 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph Resource Allocation Graph Example
Operating System Concepts – 10th Edition 8.25 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.26 Silberschatz, Galvin and Gagne ©2018
Resource Allocation Graph with a Deadlock Graph with a Cycle But no Deadlock
Operating System Concepts – 10th Edition 8.27 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.28 Silberschatz, Galvin and Gagne ©2018
Basic Facts Methods for Handling Deadlocks
If graph contains no cycles no deadlock Ensure that the system will never enter a deadlock state:
If graph contains a cycle • Deadlock prevention
• if only one instance per resource type, then deadlock • Deadlock avoidance
• if several instances per resource type, possibility of deadlock Allow the system to enter a deadlock state and then recover
Ignore the problem and pretend that deadlocks never occur in the
system.
Operating System Concepts – 10th Edition 8.29 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.30 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.31 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.32 Silberschatz, Galvin and Gagne ©2018
Deadlock Avoidance Safe State
Requires that the system has some additional a priori information When a thread requests an available resource, system must decide
available if immediate allocation leaves the system in a safe state
Simplest and most useful model requires that each thread declare System is in safe state if there exists a sequence <T1, T2, …, Tn>
the maximum number of resources of each type that it may need of ALL the threads in the systems such that for each Ti, the
The deadlock-avoidance algorithm dynamically examines the resources that Ti can still request can be satisfied by currently
resource-allocation state to ensure that there can never be a available resources + resources held by all the Tj, with j < I
circular-wait condition That is:
Resource-allocation state is defined by the number of available and • If Ti resource needs are not immediately available, then Ti can
allocated resources, and the maximum demands of the processes wait until all Tj have finished
• When Tj is finished, Ti can obtain needed resources, execute,
return allocated resources, and terminate
• When Ti terminates, Ti +1 can obtain its needed resources, and
so on
Operating System Concepts – 10th Edition 8.33 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.34 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.35 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.36 Silberschatz, Galvin and Gagne ©2018
Avoidance Algorithms Resource-Allocation Graph Scheme
Single instance of a resource type Claim edge Ti Rj indicated that process Tj may request resource
• Use a resource-allocation graph Rj; represented by a dashed line
Claim edge converts to request edge when a thread requests a
resource
Multiple instances of a resource type
Request edge converted to an assignment edge when the resource
• Use the Banker’s Algorithm is allocated to the thread
When a resource is released by a thread, assignment edge
reconverts to a claim edge
Resources must be claimed a priori in the system
Operating System Concepts – 10th Edition 8.37 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.38 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.39 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.40 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph Algorithm Banker’s Algorithm
Suppose that thread Ti requests a resource Rj Multiple instances of resources
The request can be granted only if converting the request edge to an Each thread must a priori claim maximum use
assignment edge does not result in the formation of a cycle in the When a thread requests a resource, it may have to wait
resource allocation graph
When a thread gets all its resources it must return them in a finite
amount of time
Operating System Concepts – 10th Edition 8.41 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.42 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.43 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.44 Silberschatz, Galvin and Gagne ©2018
Resource-Request Algorithm for Process Pi Example of Banker’s Algorithm
Requesti = request vector for process Ti. If Requesti [j] = k then
process Ti wants k instances of resource type Rj 5 threads T0 through T4;
1. If Requesti Needi go to step 2. Otherwise, raise error 3 resource types:
condition, since process has exceeded its maximum claim A (10 instances), B (5instances), and C (7 instances)
2. If Requesti Available, go to step 3. Otherwise Ti must wait, Snapshot at time T0:
since resources are not available
Allocation Max Available
3. Pretend to allocate requested resources to Ti by modifying the
state as follows: ABC ABC ABC
• If unsafe Ti must wait, and the old resource-allocation state T4 002 433
is restored
Operating System Concepts – 10th Edition 8.45 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.46 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.47 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 8.48 Silberschatz, Galvin and Gagne ©2018