0% found this document useful (0 votes)
16 views

Lecture 05

Lecture 5 focuses on process synchronization, covering race conditions, the critical-section problem, and solutions such as locks and semaphores. It discusses classical synchronization problems including the bounded-buffer problem, readers-writers problem, and dining-philosophers problem. The lecture emphasizes the importance of ensuring consistency in shared data through effective synchronization mechanisms.

Uploaded by

Amanda James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture 05

Lecture 5 focuses on process synchronization, covering race conditions, the critical-section problem, and solutions such as locks and semaphores. It discusses classical synchronization problems including the bounded-buffer problem, readers-writers problem, and dining-philosophers problem. The lecture emphasizes the importance of ensuring consistency in shared data through effective synchronization mechanisms.

Uploaded by

Amanda James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Lecture 5: Process Synchronization

Lecture 5: Process Synchronization


Race conditions
The Critical-Section Problem and Mutual Exclusion
Locks and Semaphores
Classical Problems of Synchronization

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--;

/* consume the item in nextConsumed */


}

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);

10 critical section vs remainder section


Synchronization Hardware
Many systems provide hardware support
for critical section code
Uniprocessor (one processor) – could
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-interruptable
11
Solution to Critical-section Problem Using Locks

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

13 wait() is to lock and signal() is unlock


Semaphore as General
Synchronization Tool

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

Solved by Semaphore Implementation

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 {

// produce an item in nextp


wait (empty);
wait (mutex);

// add the item to the buffer

signal (mutex);
signal (full);

20 } while (TRUE);
Bounded Buffer Problem (Cont.)

do {
wait (full);
wait (mutex);

// remove an item from buffer to nextc

signal (mutex);
signal (empty);

// consume the item in nextc

} 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

signal(wrt);//to release Semaphore wrt


} while (TRUE);

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

signal ( chopstick[i] ); //release chopstick


signal ( chopstick[ (i + 1) % 5] );

// think

} while (TRUE);

26
End of Lecture 5

Slides adapted from the book:

Abraham Silberschatz, Peter Baer Galvin,


Greg Gagne, “Operating System
Concepts”, 9/E, John Wiley & Sons.

You might also like