Ch3-Process Synchronization 2021 NHV - Key
Ch3-Process Synchronization 2021 NHV - Key
Synchronization
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 2 Silberschatz, Galvin and Gagne ©2009
s
Objectives
Operating System Concepts – 8th Edition 3 Silberschatz, Galvin and Gagne ©2009
Background
Operating System Concepts – 8th Edition 4 Silberschatz, Galvin and Gagne ©2009
a
Shared Data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
Operating System Concepts – 8th Edition 5 Silberschatz, Galvin and Gagne ©2009
Producer
while (true)
/* produce an item and put in nextProduced *
while (counter == BUFFER_SIZE
; // do nothin
buffer [in] = nextProduced
in = (in + 1) % BUFFER_SIZE
counter++
}
Operating System Concepts – 8th Edition 6 Silberschatz, Galvin and Gagne ©2009
{
Consumer
while (true)
while (counter == 0
; // do nothin
nextConsumed = buffer[out]
out = (out + 1) % BUFFER_SIZE
counter--
/* consume the item in nextConsumed *
}
Operating System Concepts – 8th Edition 7 Silberschatz, Galvin and Gagne ©2009
Race Condition
‣ counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register
‣ counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register
‣ Consider this execution interleaving with “counter = 5” initially
S0: producer execute register1 = counter {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = counter {register2 = 5}
S3: consumer execute register2 = register2 - 1 {register2 = 4}
S4: producer execute counter = register1 {counter = 6 }
S5: consumer execute counter = register2 {counter = 4
Race condition: situation where several processes access and manipulate the same
data concurrently and the outcome of the execution depends on the particular order in
which the access takes place.
Operating System Concepts – 8th Edition 8 Silberschatz, Galvin and Gagne ©2009
2
Operating System Concepts – 8th Edition 9 Silberschatz, Galvin and Gagne ©2009
Critical Section
Operating System Concepts – 8th Edition 10 Silberschatz, Galvin and Gagne ©2009
n
Operating System Concepts – 8th Edition 11 Silberschatz, Galvin and Gagne ©2009
Peterson’s Solution
Operating System Concepts – 8th Edition 12 Silberschatz, Galvin and Gagne ©2009
n
do {
flag[i] = TRUE;
‣ Provable that
turn = j;
1. Mutual exclusion is preserve
while (flag[j] && turn == j);
2. Progress requirement is
critical section satisfie
flag[i] = FALSE; 3. Bounded-waiting requirement
remainder section is met
} while (TRUE);
Operating System Concepts – 8th Edition 13 Silberschatz, Galvin and Gagne ©2009
P0 P1
do { do {
flag[0] = TRUE; flag[1] = TRUE;
turn = 1; turn = 0;
while (flag[1] && turn == 1); while (flag[0] && turn == 0);
critical section critical section
flag[0] = FALSE; flag[1] = FALSE;
remainder section remainder section
} while (TRUE); } while (TRUE);
Operating System Concepts – 8th Edition 14 Silberschatz, Galvin and Gagne ©2009
Synchronization Hardware
Operating System Concepts – 8th Edition 15 Silberschatz, Galvin and Gagne ©2009
Solution to Critical-section
Problem Using Locks
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
Operating System Concepts – 8th Edition 16 Silberschatz, Galvin and Gagne ©2009
TestAndSet Instruction
‣ Definition
boolean rv = *target
*target = TRUE
return rv
}
Operating System Concepts – 8th Edition 17 Silberschatz, Galvin and Gagne ©2009
do
while ( TestAndSet(&lock )
; // do nothin
// critical sectio
lock = FALSE
// remainder section
} while (TRUE);
Operating System Concepts – 8th Edition 18 Silberschatz, Galvin and Gagne ©2009
{
Swap Instruction
‣ Definition
boolean temp = *a
*a = *b
*b = temp
}
Operating System Concepts – 8th Edition 19 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 20 Silberschatz, Galvin and Gagne ©2009
{
Operating System Concepts – 8th Edition 21 Silberschatz, Galvin and Gagne ©2009
Mutex Locks
‣ Previous solutions are complicated and generally inaccessible
to application programmers
Operating System Concepts – 8th Edition 22 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 23 Silberschatz, Galvin and Gagne ©2009
Semaphore
‣ Synchronization tool that provides more sophisticated ways (than
Mutex locks) for process to synchronize their activities
‣ Semaphore S – integer variabl
‣ Can only be accessed via two indivisible (atomic) operations : wait()
and signal(
✦ Originally called P() and V()
Operating System Concepts – 8th Edition 24 Silberschatz, Galvin and Gagne ©2009
Semaphore Usage
‣ Counting semaphore – integer value can range over an unrestricted domai
‣ Binary semaphore – integer value can range only between 0 and
✦ Same as a mutex locks
‣ Can implement a counting semaphore S as a binary semaphor
‣ Can solve various synchronization problems (general synchronization tool)
‣ Provides mutual exclusio
Semaphore mutex; // initialized to
do ✦ wait (S) {
while S <=
wait (mutex) ; // no-o
// Critical Sectio S--
signal (mutex)
✦ signal (S) {
// remainder sectio S++
} while (TRUE); }
Operating System Concepts – 8th Edition 25 Silberschatz, Galvin and Gagne ©2009
Semaphore Implementation
‣ Must guarantee that no two processes can execute wait ()
and signal () on the same semaphore at the same tim
‣ Thus, implementation becomes the critical section problem
where the wait and signal code are placed in the critical
sectio
✦ Could now have busy waiting in critical section
implementatio
! But implementation code is shor
! Little busy waiting if critical section rarely occupie
Operating System Concepts – 8th Edition 26 Silberschatz, Galvin and Gagne ©2009
}
Semaphore Implementation
with no Busy waiting
‣ Two operations
✦ block – place the process invoking the operation on the
appropriate waiting queu
✦ wakeup – remove one of processes in the waiting
queue and place it in the ready queu
Operating System Concepts – 8th Edition 27 Silberschatz, Galvin and Gagne ©2009
‣ Implementation of signal
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
Operating System Concepts – 8th Edition 28 Silberschatz, Galvin and Gagne ©2009
}
Operating System Concepts – 8th Edition 29 Silberschatz, Galvin and Gagne ©2009
✦ Bounded-Buffer Proble
✦ Dining-Philosophers Problem
Operating System Concepts – 8th Edition 30 Silberschatz, Galvin and Gagne ©2009
d
Bounded-Buffer Problem
Operating System Concepts – 8th Edition 31 Silberschatz, Galvin and Gagne ©2009
signal (mutex)
signal (full)
} while (TRUE);
Operating System Concepts – 8th Edition 32 Silberschatz, Galvin and Gagne ©2009
signal (mutex)
signal (empty)
…
// consume the item in next consumed
…
} while (TRUE);
Operating System Concepts – 8th Edition 33 Silberschatz, Galvin and Gagne ©2009
Readers-Writers Problem
‣ A data set is shared among a number of concurrent processe
✦ Readers – only read the data set; they do not perform any update
✦ Writers – can both read and write
‣ Shared Dat
✦ Data se
✦ Semaphore mutex initialized to
✦ Semaphore wrt initialized to
✦ Integer readcount initialized to 0
Operating System Concepts – 8th Edition 34 Silberschatz, Galvin and Gagne ©2009
{
do
wait (wrt)
…
// writing is performe
…
signal (wrt)
} while (TRUE);
Operating System Concepts – 8th Edition 35 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 36 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 37 Silberschatz, Galvin and Gagne ©2009
Dining-Philosophers Problem
‣ Philosophers spend their lives thinking
and eatin
‣ Don’t interact with their neighbors,
occasionally try to pick up 2 chopsticks
(one at a time) to eat from bow
✦ Need both to eat, then release both
when don
‣ In the case of 5 philosopher
✦ Shared data
! Bowl of rice (data set)
! Semaphore chopstick [5] initialized to 1
Operating System Concepts – 8th Edition 38 Silberschatz, Galvin and Gagne ©2009
g
Dining-Philosophers Algorithm
‣ The structure of Philosopher i
do {
wait ( chopstick[i] )
wait ( chopstick[ (i + 1) % 5] )
// ea
signal ( chopstick[i] )
signal (chopstick[ (i + 1) % 5] )
// thin
} while (TRUE)
‣ What is the problem with this algorithm?
Operating System Concepts – 8th Edition 39 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 40 Silberschatz, Galvin and Gagne ©2009
.
Operating System Concepts – 8th Edition 41 Silberschatz, Galvin and Gagne ©2009
Monitors
‣ A high-level abstraction that provides a convenient and
effective mechanism for process synchronizatio
‣ Abstract data type, internal variables only accessible by code
within the procedur
‣ Only one process may be active within the monitor at a tim
monitor monitor-nam
}
Operating System Concepts – 8th Edition 42 Silberschatz, Galvin and Gagne ©2009
{
Operating System Concepts – 8th Edition 43 Silberschatz, Galvin and Gagne ©2009
Condition Variables
Operating System Concepts – 8th Edition 44 Silberschatz, Galvin and Gagne ©2009
;
Operating System Concepts – 8th Edition 45 Silberschatz, Galvin and Gagne ©2009
‣ Options includ
✦ Signal and wait – P waits until Q either leaves the monitor or it waits
for another condition
✦ Signal and continue – Q waits until P either leaves the monitor or it
waits for another condition
✦ Both have pros and cons – language implementer can decid
✦ Monitors implemented in Concurrent Pascal compromis
! P executing signal immediately leaves the monitor, Q is resume
✦ Implemented in other languages including Mesa, C#, Java
Operating System Concepts – 8th Edition 46 Silberschatz, Galvin and Gagne ©2009
t
initialization_code() {
for (int i = 0; i < 5; i++
state[i] = THINKING
Operating System Concepts – 8th Edition 48 Silberschatz, Galvin and Gagne ©2009
&
&
DiningPhilosophers.pickup (i)
EA
DiningPhilosophers.putdown (i)
Operating System Concepts – 8th Edition 49 Silberschatz, Galvin and Gagne ©2009
body of F
if (next_count > 0
signal(next
else
signal(mutex);
Operating System Concepts – 8th Edition 50 Silberschatz, Galvin and Gagne ©2009
…
…
Operating System Concepts – 8th Edition 51 Silberschatz, Galvin and Gagne ©2009
if (x_count > 0)
next_count++
signal(x_sem)
wait(next)
next_count--
Operating System Concepts – 8th Edition 52 Silberschatz, Galvin and Gagne ©2009
}
Operating System Concepts – 8th Edition 53 Silberschatz, Galvin and Gagne ©2009
Synchronization Examples
‣ Solari
‣ Windows X
‣ Linu
‣ Pthreads
Operating System Concepts – 8th Edition 55 Silberschatz, Galvin and Gagne ©2009
Solaris Synchronization
‣ Implements a variety of locks to support multitasking, multithreading (including
real-time threads), and multiprocessin
‣ Uses adaptive mutexes for efficiency when protecting data from short code
segment
✦ Starts as a standard semaphore spin-loc
✦ If lock held, and by a thread running on another CPU, spin
✦ If lock held by non-run-state thread, block and sleep waiting for signal of lock
being release
‣ Uses readers-writers locks to protect data that are accessed frequently but are
usually accessed in a read-only manner.
‣ Uses turnstiles to order the list of threads waiting to acquire either an adaptive
mutex or reader-writer loc
✦ Turnstiles are per-lock-holding-thread, not per-objec
Operating System Concepts – 8th Edition 56 Silberschatz, Galvin and Gagne ©2009
x
Windows XP Synchronization
‣ Uses interrupt masks to protect access to global resources on
uniprocessor system
Operating System Concepts – 8th Edition 57 Silberschatz, Galvin and Gagne ©2009
Linux Synchronization
‣ Linux
✦ Prior to kernel Version 2.6, disables interrupts to
implement short critical section
✦ Version 2.6 and later, fully preemptiv
‣ Linux provides
✦ semaphore
✦ spinlock
✦ reader-writer versions of bot
Operating System Concepts – 8th Edition 58 Silberschatz, Galvin and Gagne ©2009
:
Pthreads Synchronization
‣ Pthreads API is OS-independen
‣ It provides
✦ mutex lock
✦ condition variables
Operating System Concepts – 8th Edition 59 Silberschatz, Galvin and Gagne ©2009
End of Chapter 3
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
: