Lecture 05
Lecture 05
2
Objectives
To introduce the critical-section problem, whose solutions
can be used to ensure the consistency of shared data
To present solutions to the critical-section problem
3
Background
4
Producer
5
Consumer
while (true) {
while (count == 0) //no stock
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
6
Race Condition
7
Solution to Critical-Section Problem
8
Peterson’s Solution
9
Algorithm for Process Pi
do {
they don’t want to execute,
flag[i] = TRUE; // and pass the turn to j
turn = j;
while (flag[j] && turn == j){
; } // do nothing if j did not meet
critical section with condition, i
will take place
flag[i] = FALSE;
remainder section
} while (TRUE);
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
12
Semaphore
Synchronization tool that does not require busy
waiting
Semaphore, S – integer variable
Two standard operations modify S: wait() and
signal()
Less complicated
wait (s) {
Can only be accessed
while signal
s <= 0 via two (s) {
indivisible
(atomic) operations:
; // no- s++;
op }
s--;
}
Lock Unlock
14
Semaphore Implementation
15
Semaphore Implementation with no
Busy waiting
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:
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.
NB: no Busy waiting – make the process sleep while waiting in the
waiting queue [block]
16
Semaphore Implementation with no Busy
waiting (Cont.)
17
Classical Problems of Synchronization
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
18
Bounded-Buffer Problem
N buffers, each can hold one item
Normally:
Semaphore mutex initialized to the
value 1
Semaphore full initialized to the value
0
Semaphore empty initialized to the
value N (N number of vacancy)
19
Bounded Buffer Problem (Cont.)
do {
signal (mutex);
signal (full);
20 } while (TRUE);
Bounded Buffer Problem (Cont.)
do {
wait (full);
wait (mutex);
signal (mutex);
signal (empty);
} while (TRUE);
21
Readers-Writers Problem –
Share Memory
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 (who did not obtain the
lock) to read at the same time but ONLY one single writer
(those obtain the lock) can access the shared data at the
same time.
Shared Data
Data set
Semaphore mutex initialized to 1
Semaphore wrt initialized to 1
Integer readcount initialized to 0
22
Readers-Writers Problem (Cont.)
The structure of a writer process
do {
wait(wrt); //to lock Semaphore wrt
//writing is performed
23
Readers-Writers Problem (Cont.)
The structure of a reader process
Once reader is executing the job, no wrt is allow!
24
Dining-Philosophers Problem
25
Dining-Philosophers Problem (Cont.)
The structure of Philosopher i:
do {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
// think
} while (TRUE);
26
End of Lecture 5