6 Process Synchronization
6 Process Synchronization
Synchronization
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
To present the concept of
process synchronization.
To introduce the critical-section
problem, whose solutions can
be used to ensure the
consistency of shared data
To present both software and
Objectives hardware solutions of the
critical-section problem
To examine several classical
process-synchronization
problems
To explore several tools that are
used to solve process
synchronization problems
Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne ©2013
Background
Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne ©2013
Producer
while (true) {
/* produce an item in next produced */
Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne ©2013
Consumer
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next
consumed */
}
Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne ©2013
Let's Go Behind the Curtain
Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne ©2013
counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
What Can Consider this execution interleaving with “counter = 5” initially:
Possibly Go 1. S0: producer executes register1 = counter
{register1 = 5}
Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne ©2013
What Can Possibly Go Wrong?
Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne ©2013
A situation where several
processes access and
manipulate the same data
What’s
concurrently and the
outcome of the execution
depends on the order in
which the access takes
place.
the
We need a way to
synchronize the
processes so that only
one process at a time can
be manipulating a critical
problem?
data item.
Race Condition
Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne ©2013
Critical Section Problem
Operating System Concepts – 9th Edition 5.11 Silberschatz, Galvin and Gagne ©2013
General structure of process Pi
Critical
Section
Process
Structure
Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne ©2013
1. Mutual Exclusion - If process Pi is
executing in its critical section, then no other
processes can be executing in their critical
sections
2. Progress - If no process is executing in its
critical section and some processes wish to
enter their critical sections,
only those processes that are not executing
in their remainder sections can participate in
Solution to deciding which will enter its critical section
next, and
Critical- this selection cannot be postponed
indefinitely
Section 3. Bounded Waiting - A bound must exist on
the number of times that other processes are
Problem allowed to enter their critical sections after a
process has made a request to enter its
critical section and before that request is
granted
Assume that each process executes at a
nonzero speed
No assumption concerning relative speed of
the
5.13
n processes Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Kernel Open Files data structure
List must be modified when a new file is
opened or closed
If two processes were to open files
Areas simultaneously, the separate updates to
this file could cause a race condition
Prone to
Memory Allocation Structures
Race Process List Structures
Conditions Interrupt Handling
Operating System Concepts – 9th Edition 5.14 Silberschatz, Galvin and Gagne ©2013
Non-preemptive – runs until exits kernel
mode, blocks, or voluntarily yields CPU
Essentially free of race conditions in kernel
mode
Preemptive – allows preemption of process
when running in kernel mode
Critical- Are hard to design especially in SMP
(Symmetric Multiprocessors) architectures
Section where two kernel-mode processes can be
running simultaneously on different
Handling in processors.
Why choose preemptive?
OSs More responsive since risk of long-running
processes holding up the works is eliminated
More suitable for real-time programming as
real-time processes can preempt as needed.
Operating System Concepts – 9th Edition 5.15 Silberschatz, Galvin and Gagne ©2013
Synchronization Hardware
Many systems provide hardware support for
implementing the critical section code.
All solutions below based on idea of locking
Protecting critical regions via locks
Uniprocessors – could disable interrupts
Currently running code would execute without
preemption
This is the approach generally taken for non-
preemptive kernels
Generally too inefficient on multiprocessor
systems.
Why?
Message is passed to all processors
This delays entry into each critical section
decreasing system efficiency
May impact clock performance if clock is
updated using interrupts
Operating System Concepts – 9th Edition 5.16 Silberschatz, Galvin and Gagne ©2013
Synchronization
Hardware
Operating System Concepts – 9th Edition 5.17 Silberschatz, Galvin and Gagne ©2013
Mutex Locks
Operating System Concepts – 9th Edition 5.19 Silberschatz, Galvin and Gagne ©2013
But this solution requires busy waiting
When a process is in its critical section, any
other process that tries to enter its critical
section must loop continuously in the calls to
acquire()
and is therefore called a spinlock because the
process spins while waiting for the lock to
become available.
In a single CPU multiprogramming environment
Mutex busy waiting wastes CPU cycles that could be
used by other processes.
Locks But…
Spinlocks do not require context switching and
are efficient when locks are expected to be held
for short times.
Multiprocessor systems are good candidates for
spinlocks as another thread can enter its critical
section on another processor.
Operating System Concepts – 9th Edition 5.20 Silberschatz, Galvin and Gagne ©2013
Synchronization tool that provides more
sophisticated ways (than Mutex locks) for
processes to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible (atomic)
operations
wait() and signal()
Definition of the wait() operation
Semaphore wait(S) {
while (S <= 0)
; // busy wait
S--;
}
Definition of the signal() operation
signal(S) {
S++;
}
Operating System Concepts – 9th Edition 5.21 Silberschatz, Galvin and Gagne ©2013
Counting Semaphore – integer value can range over an
unrestricted domain
Used to provide access to a resource consisting of a finite # of
resources
The semaphore is initialized to the number of available
resources
Each process that wishes to use a resource performs a wait()
operation the decrements the semaphore
When a process releases a resource, it performs a signal()
operations that increments the semaphore.
When the semaphore goes to 0, all resources are being used.
After that, all processes that wish to use a resource will block
until the count becomes >0.
Semaphore Usage
Operating System Concepts – 9th Edition 5.22 Silberschatz, Galvin and Gagne ©2013
Semaphore Usage
Operating System Concepts – 9th Edition 5.24 Silberschatz, Galvin and Gagne ©2013
With each semaphore there is an
associated waiting queue
Each entry in a waiting queue has two
data items:
value (of type integer)
pointer to next record in the list
Two operations:
Semaphore block – place the process invoking the
operation on the appropriate waiting
Implementation queue
with no Busy wakeup – remove one of processes in
waiting the waiting queue and place it in the
ready queue
typedef struct{
int value;
struct process *list;
} semaphore;
Operating System Concepts – 9th Edition 5.25 Silberschatz, Galvin and Gagne ©2013
Implementation with no Busy
Waiting (Cont.)
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
} Semaphore values can be negative.
}
Magnitude = # of processes waiting
5.26
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Semaphore Implementation
Operating System Concepts – 9th Edition 5.27 Silberschatz, Galvin and Gagne ©2013
Thus, the implementation
becomes the critical section
problem where the wait and
signal code are placed in the
critical section
Could now have busy waiting in
critical section implementation
Implementation But implementation of wait and
with no Busy signal code is short
waiting (Cont.) Little busy waiting if critical
section rarely occupied
Busy waiting is more of a
concern in applications that may
spend a long time in their critical
section and therefore this is not
a good solution
Operating System Concepts – 9th Edition 5.28 Silberschatz, Galvin and Gagne ©2013
2
9
Operating System Concepts – 9th Edition 5.29 Silberschatz, Galvin and Gagne ©2013
Deadlock
Operating System Concepts – 9th Edition 5.30 Silberschatz, Galvin and Gagne ©2013
Deadlock and Starvation
Deadlock – two or more processes are waiting indefinitely for an event that can be
caused by only one of the waiting processes
Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
BLOCKED ... BLOCKED ...
signal(S); signal(Q);
signal(Q); signal(S);
Operating System Concepts – 9th Edition 5.32 Silberschatz, Galvin and Gagne ©2013
Priority Inversion – Scheduling problem
when lower-priority process holds a lock
needed by higher-priority process
Consider processes L, M, and H whose
priorities are represented by:
L < M < H.
Assume process H requires resource R
Priority that is currently being accessed by
process L.
Inversion H would have to wait for L to complete to
access resource R.
BUT…
M becomes runnable and preempts
process L.
Now, M, a process with a lower priority
than H, has affected how long H must
wait for L to relinquish resource R.
Operating System Concepts – 9th Edition 5.33 Silberschatz, Galvin and Gagne ©2013
Priority Inversion - Solved
Operating System Concepts – 9th Edition 5.34 Silberschatz, Galvin and Gagne ©2013
Classical Problems of Synchronization
Operating System Concepts – 9th Edition 5.35 Silberschatz, Galvin and Gagne ©2013
Bounded-Buffer Problem
Operating System Concepts – 9th Edition 5.36 Silberschatz, Galvin and Gagne ©2013
Bounded Buffer Problem (Cont.)
The structure of the producer process
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Operating System Concepts – 9th Edition 5.37 Silberschatz, Galvin and Gagne ©2013
Bounded Buffer Problem (Cont.)
The structure of the consumer process
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
Operating System Concepts – 9th Edition 5.38 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem
A data set is
shared among Problem – Shared Data
several concurrent 1.Data set
processes Allow multiple
readers to read at 2. Semaphore rw_mutex
Readers – only read initialized to 1
the same time
the data set; they do
not perform any When a writer 3. Semaphore mutex initialized
updates accesses the shared to 1 (protects read_count
data, no reader (or actions)
Writers – can both
writer) may access 4. Integer read_count
read and write
the shared data at initialized to 0 (how many
the same time readers)
Operating System Concepts – 9th Edition 5.39 Silberschatz, Galvin and Gagne ©2013
The structure of a writer process
do {
wait(rw_mutex);
Readers- ...
/* writing is
Writers performed */
Problem ...
signal(rw_mutex);
(Cont.) } while (true);
Operating System Concepts – 9th Edition 5.40 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem (Cont.)
The structure of a reader process
do {
wait(mutex); lock the read_count variable
read_count++;
if (read_count == 1) for first or last reader
wait(rw_mutex);
signal(mutex); unlock the read_count variable
...
/* reading is performed */
...
wait(mutex); lock the read_count variable
read_count--;
if (read_count == 0)
signal(rw_mutex); for the last reader
signal(mutex); unlock the read_count variable
} while (true);
Operating System Concepts – 9 Edition
th 5.41 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers
Problem
Operating System Concepts – 9th Edition 5.42 Silberschatz, Galvin and Gagne ©2013
The structure of Philosopher i:
do {
wait (chopstick[i] );
wait (chopStick[ (i + 1) %
5] );
// eat
signal (chopstick[i] );
Dining- signal (chopstick[ (i + 1) % 5]
Philosophers );
Problem // think
Algorithm
} while (TRUE);
If they all get hungry at the same time and grab their left chopstick,
when they try to grab their right chopstick they will be delayed forever
= DEADLOCK
Operating System Concepts – 9th Edition 5.43 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers Problem Algorithm
Deadlock Handling
Operating System Concepts – 9th Edition 5.44 Silberschatz, Galvin and Gagne ©2013
Transactional Memory
A memory transaction is a sequence of read-write operations to
memory that are performed atomically.
void update()
{
atomic {
/* modify shared data */
}
}
Advantages:
System provides atomicity, not developer
No locks are involved … so no deadlock.
Transactional memory system can detect opportunities for
concurrency within an atomic block
Implementation
Software Transactional Memory (STM)– code inserted by
compiler to guarantee atomicity and detect concurrency options
Hardware Transactional Memory (HTM)– Uses cache
hierarchies and cache coherency protocols to manage shared
data conflicts. Less overhead than STM
Operating System Concepts – 9th Edition 5.45 Silberschatz, Galvin and Gagne ©2013
End
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013