ch5 Process Synchronization
ch5 Process Synchronization
Synchronization
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 5: Process Synchronization
Background
The Critical-Section Problem
Peterson’s Solution
Synchronization Hardware
Mutex Locks
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Alternative Approaches
Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne ©2013
Objectives
Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne ©2013
VXUIKYY3Y_TINXUTO`GZOUT 3XKUXJKXOTM3ZNK3VXUIKYYKY3YU3ZNGZ3UTR_3狣3[YK3ZNK3YNGXKJ3
Background
XKYU[XIK3
inconsistency
SGQK3JGZG3IUXXKIZ
Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne ©2013
Producer
while (true) {
/* produce an item in next produced */
/* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++; GJJY3IU[TZKX3OTIXKSKTZ3
IU[TZKX3OY3YNGXKJ3HKZ]KKT3VXUJ[IKX3GTJ3IUTY[SKX
Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne ©2013
IU[TZKX3OY3YNGXKJ3HKZ]KKT3VXUJ[IKX3GTJ3
IUTY[SKX
Consumer
while (true) {
while (counter == 0) KJOZOTM3ZNK3RUUV3IUTZOZOUT
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--; GJJOTM3IU[TZKX3JKIXKSKTZ
Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne ©2013
Race Condition SGQOTM393UL3XKYU[XIKY3#393UL3VXUIKYYKY
SG_3RKGJ3ZU3JGZG3OTIUTYOYZKTI_
XKY[RZ3OY3TUZ3IUXXKIZ3GTJ3
RUMOIGR
counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
VXKKSVZOUT 3IGT3I[Z3ZNK3ZGYQ3GTJ3MKZ3GTUZNKX3UTK3
Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne ©2013
Critical Section Problem
Consider system of n processes {p0, p1, … pn-1}
Each process has critical section segment of code
Process may be changing common variables, updating
table, writing file, etc
When one process in critical section, no other may be in its
critical section
Critical section problem is to design protocol to solve this
Each process must ask permission to enter critical section in
entry section, may follow critical section with exit section,
then remainder section
IG[YK3UL3VXUHRKS3
狣393UL3XKYU[XIKY3GXK3RKYY3ZNGT3T[SHKX3UL3VXUIKYYKY IUSVGXK3VXUIKYYKY3]OZN3IV[Y
狤3UY3SG_3]UXQ3OT3VXKKSVZOUT3SUJK
YUR[ZOUT3SGQK3UY3]UXQ3OT3TU3VXKKSVZOUT3SUJK NOMN3IUYZ
GTJ3SGQK393UL3XKYU[XIKY3IV[Y3#393UL3VXUIKYYKY
Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne ©2013
Critical Section
ROQK3G3JUUX3JKZKXSOTK3]NU3KTZKX3LOXYZ
S[YZ3K^KI[ZKJ3OT3YKXOGR
IGTZ3HK3K^KI[ZKJ3OT3VGXGRR GR]G_Y3VXUZKIZKJ VGXZ3UL3IUJK3TGSKJ3IXOZOIGR3
HKIG[YK3OZY3NGY3GT3KJOZ3UT3
YNGXKJ3XKYU[XIK
SGQOTM3Y[XK3]NU3]GOZY3U[ZYOJK3RKZY3
ZNKS3QTU]3TU]3OZY3ZNKOX3ZOSK
VGXZ3UL3IUJK3
JUKY3TUZ3NG\K3
YNGXKJ3XKYU[XIKY
Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne ©2013
Algorithm for Process Pi
do {
critical section
turn = j;
remainder section
} while (true);
Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne ©2013
Solution to Critical-Section Problem
UT3KTZX_3YKIZOUT
critical sections
OL3GT_3VXUIKYYKY3KTJY3LXUS3IXOZOIGR3YKIZOUT3TK^Z3VXUIKYY3]ORR3HK3YKRKIZKJ3ZU3KTZKX3H_3ZKGS3SKSHKXY3
Operating System Concepts – 9th Edition 5.11 Silberschatz, Galvin and Gagne ©2013
Critical-Section Handling in OS
Two approaches depending on if kernel is preemptive or non-
preemptive
Preemptive – allows preemption of process when running
in kernel mode
Non-preemptive – runs until exits kernel mode, blocks, or
voluntarily yields CPU
Essentially free of race conditions in kernel mode
Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne ©2013
Peterson’s Solution
Good algorithmic description of solving the problem
Two process solution
Assume that the load and store machine-language
instructions are atomic; that is, cannot be interrupted
The two processes share two variables:
int turn;
Boolean flag[2]
Operating System Concepts – 9th Edition 5.13 Silberschatz, Galvin and Gagne ©2013
]KGQ3狣3ZNOY3YUR[ZOUTY3UTR_3Y[VVUXZ3狤3VXUIKYYKY33]KGQ3狤3ZNKOX3OY3IUSVKZOZOUT3UT3ZKXS3
JU3a3
3333333LRGMA3P3C3#3ZX[K!
3333333Z[XT3#3O!
3333333]NORK3LRGMA3O3C33 3Z[XT3##O!
33333333333333333IXOZOIGR3YKIZOUT
3333333LRGMA3P3C3#3LGRYK!
33333333333333333XKSOTJKX3YKIZOUT
c3]NORK3ZX[K!
Operating System Concepts – 9th Edition 5.14 Silberschatz, Galvin and Gagne ©2013
Peterson’s Solution (Cont.)
Provable that the three CS requirement are met:
1. Mutual exclusion is preserved
Pi enters CS only if:
either flag[j] = false or turn = i
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met
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
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
Modern machines provide special atomic hardware instructions
Atomic = non-interruptible 3K^KI[ZKJ3YKXOGR3
UTR_3狣3ZOSK3K^KI[ZKJ3狣3IV[3GZ3ZOSK
Either test memory word and set value
Or swap contents of two memory words
Operating System Concepts – 9th Edition 5.16 Silberschatz, Galvin and Gagne ©2013
Solution to Critical-section Problem Using Locks
do {
acquire lock KTZX_
critical section
release lock K^OZ
remainder section
} while (TRUE);
Operating System Concepts – 9th Edition 5.17 Silberschatz, Galvin and Gagne ©2013
test_and_set Instruction
Definition: RUIQ3NGY3ZNOY3\GR[K
Operating System Concepts – 9th Edition 5.18 Silberschatz, Galvin and Gagne ©2013
Solution using test_and_set()
Operating System Concepts – 9th Edition 5.19 Silberschatz, Galvin and Gagne ©2013
compare_and_swap Instruction
Definition:
int compare _and_swap(int *value, int expected, int new_value) {
int temp = *value;
if (*value == expected)
*value = new_value;
return temp;
}
1. Executed atomically
2. Returns the original value of passed parameter “value”
3. Set the variable “value” the value of the passed parameter “new_value”
but only if “value” ==“expected”. That is, the swap takes place only under
this condition.
Operating System Concepts – 9th Edition 5.20 Silberschatz, Galvin and Gagne ©2013
Solution using compare_and_swap
Shared integer “lock” initialized to 0;
Solution:
do {
while (compare_and_swap(&lock, 0, 1) != 0)
; /* do nothing */
/* critical section */
lock = 0;
/* remainder section */
} while (true);
Operating System Concepts – 9th Edition 5.21 Silberschatz, Galvin and Gagne ©2013
Bounded-waiting Mutual Exclusion with test_and_set
YGZOYL_3ZNK3狥3IUTJOZOUTY 3狣S[ZGR3K^IR[YOUT33狤VXUMXGKYY3
do { 狥HUTJGJKJ3]GOZOTM
waiting[i] = true;
key = true; IUSVRK^33Y[VVUXZKJ3H_3NGXJ]GXK3SG_3
while (waiting[i] && key) TUZ3HK3K^KI[ZGHRK
key = test_and_set(&lock);
waiting[i] = false;
/* critical section */
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = false;
else
H[Y_3]GOZOTM3
waiting[j] = false;
/* remainder section */
} while (true);
TUZ3]OJKR_3[YKJ
Operating System Concepts – 9th Edition 5.22 Silberschatz, Galvin and Gagne ©2013
S[ZK^ 3IRGYY3]OZN3(UURKGT3\GXOGHRK33狤3SKSHKX3L[TIZOUTY3GIW[OXK33XKRKGYK
Mutex Locks
S[Z[GR3K^IR[YOUT3
YKXOGRY3OTYZX[IZOUTY
UTR_3狣3OTYZGTZ3
Operating System Concepts – 9th Edition 5.23 Silberschatz, Galvin and Gagne ©2013
acquire() and release() TU3IUTZK^Z3Y]OZIN
VXUZKIZOUT
acquire() {
while (!available) GZ3YZGXZ3OZ3OY3ZX[K
; /* busy wait */ VXUZKIZ3IXOZOIGR3YKIZOUT33IV[3ZNXU[MNV[Z3MUKY3JU]T3
available = false;
}
release() {
available = true;
}
do {
acquire lock
critical section
release lock
remainder section
} while (true);
Operating System Concepts – 9th Edition 5.24 Silberschatz, Galvin and Gagne ©2013
YKSGVNUXK 3YGSK3GY3S[ZK^3
Semaphore XKGR3VXUZKIZOUT
Synchronization tool that provides more sophisticated ways (than Mutex locks)
for process to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible (atomic) operations
wait() and signal() GZUSOI
Originally called P() and V()
; // busy wait
S--;
}
Definition of the signal() operation
signal(S) {
S++;
}
Operating System Concepts – 9th Edition 5.25 Silberschatz, Galvin and Gagne ©2013
IGT3HK3[YKJ3]OZNU[Z3ZNK3IXOZOIGR3YKIZOUT3ZU3XKUXJKX3ZNK3IUJK
IRGYY3H[Z3OTOZOGR3\GR[K3LUX3IU[TZOTM3YKSGVNUXK3#3T[SHKX3UL3IUVOKY
Operating System Concepts – 9th Edition 5.26 Silberschatz, Galvin and Gagne ©2013
Semaphore Implementation
Must guarantee that no two processes can execute the wait()
and signal() on the same semaphore at the same time
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
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections
and therefore this is not a good solution
Operating System Concepts – 9th Edition 5.27 Silberschatz, Galvin and Gagne ©2013
Semaphore Implementation with no Busy waiting
Operating System Concepts – 9th Edition 5.28 Silberschatz, Galvin and Gagne ©2013
Implementation with no Busy waiting (Cont.)
ZNKXK3GXK3IUTZK^Z3Y]OZIN3GTJ3
wait(semaphore *S) { W[K[K3K^ZXG3NGXJ]GXK
S->value--; JKIXKSKTZ3TU]3OY3GZ3ZNK3ZUV3UL3]GOZOTM3
if (S->value < 0) {
add this process to S->list;
block(); VXUIKYY3HRUIQ3OZYKRL
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P); V3NKXK3OTJOIGZKY3ZNK3LOXYZ3VXUIKYY3OY3]GOZOTM3GZ3ZNK3ROYZ
}
}
Operating System Concepts – 9th Edition 5.29 Silberschatz, Galvin and Gagne ©2013
VXUHRKSY3GVVKGX3ZU3VXUMXGSSKX3]OZN3TUZ3XOMNZ3[YK3YKSGVNUXK
Operating System Concepts – 9th Edition 5.30 Silberschatz, Galvin and Gagne ©2013
Classical Problems of Synchronization
GRR3UL3ZNKS3NGY3YNGXKJ3XKYU[XIKY3
Operating System Concepts – 9th Edition 5.31 Silberschatz, Galvin and Gagne ©2013
Bounded-Buffer Problem
HKIG[YK3ZNKXK3OY3TUT3L[RR
Operating System Concepts – 9th Edition 5.32 Silberschatz, Galvin and Gagne ©2013
ROSOZKJ3H[LLKX
do {
...
/* produce an item in next_produced */
...
wait(empty); OL3狢3ZNKT3YZUV3OL3TUZ3IUTZOT[K3
wait(mutex); HRUIQ3LUX3GT_3UZNKX3VXUIKYY3]GTZ3ZU3GIIKYY
...
/* add next produced to the buffer */
...
signal(mutex); RKZ3MU3UZNKX3VXUIKYY3ZU3RKZ3ZNKS3QTU]3ZNKXK3OY3G3VRGIK3LUX3ZNKS
signal(full); L[RR
} while (true);
Operating System Concepts – 9th Edition 5.33 Silberschatz, Galvin and Gagne ©2013
Bounded Buffer Problem (Cont.)
The structure of the consumer process
Do {
wait(full); OL3T3IUTZOT[K3OL3狢3YZUV
wait(mutex); HRUIQ3LUX3GT_3UZNKX3VXUIKYY3]GTZ3ZU3GIIKYY
...
/* remove an item from buffer to next_consumed */
...
signal(mutex); RKZ3MU3UZNKX3VXUIKYY3ZU3RKZ3ZNKS3QTU]3ZNKXK3OY3G3VRGIK3LUX3ZNKS
signal(empty); KSVZ_
...
/* consume the item in next consumed */
...
} while (true);
IGT Z3ZKRR3OL3ZNKXK3H[Y_3]GOZOTM33ZNKXK3OY3S[Z[GR3K^IR[YOUT33ZNKXK3OY3VXUMXKYY33ZNKXK3OY3HU[TJKJ3]GOZOTM
LUX3VXUJ[IKX3IUJK3GTJ3IUTY[SKX3IUJK
Operating System Concepts – 9th Edition 5.34 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem
A data set is shared among a number of concurrent processes
Readers – only read the data set; they do not perform any updates
Writers – can both read and write
Problem – allow multiple readers to read at the same time
Only one single writer can access the shared data at the same time
Several variations of how readers and writers are considered – all
involve some form of priorities
Shared Data
Data set
Semaphore rw_mutex initialized to 1
Semaphore mutex initialized to 1
Integer read_count initialized to 0
Operating System Concepts – 9th Edition 5.35 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem (Cont.)
do {
OL3狢3SKGTY3ZNKXK3 wait(rw_mutex); OTOZOGRO`KJ3ZU3狣
RUIQ3IXOZOIGR3YKIZOUT
OY3VXUIKYY3OT3
...
IXOZOIGR3YKIZOUT3
/* writing is performed */
...
signal(rw_mutex);
} while (true);
Operating System Concepts – 9th Edition 5.36 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem (Cont.)
The structure of a reader process
do {
wait(mutex);
read_count++;
if (read_count == 1)
wait(rw_mutex);
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read count--;
if (read_count == 0)
signal(rw_mutex);
signal(mutex);
} while (true);
Operating System Concepts – 9th Edition 5.37 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem Variations
Operating System Concepts – 9th Edition 5.38 Silberschatz, Galvin and Gagne ©2013
T[SHKX3UL3IUJKY3#3T[SHKX3UL3)USVKZOZUXY
Dining-Philosophers Problem
NKXK3]K3]GTZ3狧3IUJKY
Operating System Concepts – 9th Edition 5.39 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers Problem Algorithm
The structure of Philosopher i:
do {
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
// eat IXOZOIGR3YKIZOUT
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think ]K3TKKJ3狧3YKSGVNUXKY3
} while (TRUE);
What is the problem with this algorithm?
Operating System Concepts – 9th Edition 5.40 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers Problem Algorithm (Cont.)
Deadlock handling
Allow at most 4 philosophers to be sitting
simultaneously at the table.
Allow a philosopher to pick up the forks only if both
are available (picking must be done in a critical
section.
Use an asymmetric solution -- an odd-numbered
philosopher picks up first the left chopstick and then
the right chopstick. Even-numbered philosopher picks
up first the right chopstick and then the left chopstick.
Operating System Concepts – 9th Edition 5.41 Silberschatz, Galvin and Gagne ©2013
Problems with Semaphores
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
condition x, y;
Two operations are allowed on a condition variable:
x.wait() – a process that invokes the operation is
suspended until x.signal()
x.signal() – resumes one of processes (if any) that
invoked x.wait()
If no x.wait() on the variable, then it has no effect on
the variable
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);
Variables
wait(mutex);
…
body of F;
…
if (next_count > 0)
signal(next)
else
signal(mutex);
x_count++;
if (next_count > 0)
signal(next);
else
signal(mutex);
wait(x_sem);
x_count--;
if (x_count > 0) {
next_count++;
signal(x_sem);
wait(next);
next_count--;
}
R.acquire(t);
...
access the resurce;
...
R.release;
Solaris
Windows
Linux
Pthreads
^
Operating System Concepts – 9th Edition 5.57 Silberschatz, Galvin and Gagne ©2013
Solaris Synchronization
Implements a variety of locks to support multitasking, multithreading
(including real-time threads), and multiprocessing
Uses adaptive mutexes for efficiency when protecting data from short
code segments
Starts as a standard semaphore spin-lock
If lock held, and by a thread running on another CPU, spins
If lock held by non-run-state thread, block and sleep waiting for signal of
lock being released
Uses condition variables
Uses readers-writers locks when longer sections of code need
access to data
Uses turnstiles to order the list of threads waiting to acquire either an
adaptive mutex or reader-writer lock
Turnstiles are per-lock-holding-thread, not per-object
Priority-inheritance per-turnstile gives the running thread the highest of
the priorities of the threads in its turnstile
Transactional Memory
OpenMP
void update()
{
/* read/write memory */
}
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013