Lecture 5: Shared-memory Computing with Open MP. Shared Memory Computing.
Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread...
Transcript of Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread...
![Page 1: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/1.jpg)
Operating SystemsThread Synchronization Primitives
Thomas Ropars
2019
1
![Page 2: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/2.jpg)
Agenda
• Week 42: Synchronization primitives
• Week 43: Synchronization implementation
• Week 44: Vacation
• Week 45: No lecture
• Week 46: Second Midterm Exam + AdvancedSynchronization Techniques
• Week 47: CPU Scheduling + I/O and Disks
• Week 48: File Systems (+ RAID)
2
![Page 3: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/3.jpg)
References
The content of these lectures is inspired by:
• The lecture notes of Prof. Andre Schiper.
• The lecture notes of Prof. David Mazieres.
• The lectures notes of Arnaud Legrand.
• Operating Systems: Three Easy Pieces by R. Arpaci-Dusseauand A. Arpaci-Dusseau
Other references:
• Modern Operating Systems by A. Tanenbaum
• Operating System Concepts by A. Silberschatz et al.
3
![Page 4: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/4.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems4
![Page 5: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/5.jpg)
Seen previously
Threads• Schedulable execution context
• Multi-threaded program = multiple threads in the sameprocess address space
• Allow a process to use several CPUs
• Allow a program to overlap I/O and computation
Implementation
• Kernel-level threads
• User-level threads
• Preemptive vs non-preemptive
5
![Page 6: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/6.jpg)
Seen previously
POSIX threads API (pthreads) – pseudo API:
• tid thread create(void (*fn)(void *), void *arg);
• void thread exit();
• void thread join(tid thread);
Data sharing
• Threads share the data of the enclosing process
6
![Page 7: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/7.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems7
![Page 8: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/8.jpg)
Motivation
Observations• Multi-thread programming is used in many contexts.
I It is also called concurrent programming.
• Shared memory is the inter-thread communication medium.
Is it easy to use shared memory to cooperate?
NO
The problem:
A set of threads executing on a shared-memory (multi-)processor isan asynchronous system.
• A thread can be preempted at any time.
• Reading/writing a data in memory incurs unpredictable delays(data in L1 cache vs page fault).
8
![Page 9: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/9.jpg)
Motivation
Observations• Multi-thread programming is used in many contexts.
I It is also called concurrent programming.
• Shared memory is the inter-thread communication medium.
Is it easy to use shared memory to cooperate?NO
The problem:
A set of threads executing on a shared-memory (multi-)processor isan asynchronous system.
• A thread can be preempted at any time.
• Reading/writing a data in memory incurs unpredictable delays(data in L1 cache vs page fault).
8
![Page 10: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/10.jpg)
High-level goals of the lecture
• Start thinking like a concurrent programmer
• Learn to identify concurrency problems
• Learn to cooperate through shared memoryI SynchronizationI Communication
• Think about the correctness of an algorithm
9
![Page 11: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/11.jpg)
Content of this lecture
Classical concurrent programming problems
• Mutual exclusion
• Producer-consumer
Concepts related to concurrent programming
• Critical section
• Busy waiting
• Deadlock
Synchronization primitives
• Locks
• Semaphores
• Condition variables
10
![Page 12: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/12.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems11
![Page 13: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/13.jpg)
Example: A chat serverSingle-threaded version
users/channels
stat. counters
T1
update
read/write
12
![Page 14: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/14.jpg)
Example: A chat serverSingle-threaded version
users/channels
stat. counters
T1
update
read/write
12
![Page 15: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/15.jpg)
Example: A chat serverSingle-threaded version
users/channels
stat. counters
T1
update
read/write
12
![Page 16: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/16.jpg)
Example: A chat serverFirst multi-threaded version
users/channels
stat. counters
T1
T2
T3
Tn
update
upda
te
upda
te
stat. counters
read/write
read/writeread/write
users/channels
13
![Page 17: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/17.jpg)
Example: A chat serverFirst multi-threaded version
users/channels
stat. counters
T1
T2
T3
Tn
update
upda
te
upda
te
stat. counters
read/write
read/writeread/write
users/channels
13
![Page 18: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/18.jpg)
Example: A chat serverFirst multi-threaded version
users/channels
stat. counters
T1
T2
T3
Tn
update
upda
te
upda
te
stat. counters
read/write
read/writeread/write
users/channels
13
![Page 19: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/19.jpg)
Example: A chat serverFirst multi-threaded version
users/channels
stat. counters
T1
T2
T3
Tn
update
upda
te
upda
te
stat. counters
read/write
read/writeread/write
users/channels
13
![Page 20: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/20.jpg)
Example: A chat serverFirst multi-threaded version
users/channels
stat. counters
T1
T2
T3
Tn
update
upda
te
upda
te
stat. counters
read/write
read/writeread/write
users/channels
13
![Page 21: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/21.jpg)
Example: A chat serverFirst multi-threaded version
users/channels
stat. counters
T1
T2
T3
Tn
update
upda
te
upda
te
stat. counters
read/write
read/writeread/write
users/channels
13
![Page 22: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/22.jpg)
Example: A chat serverSecond multi-threaded version
users/channels
stat. counters
T1
Ta
Tb
thread pool
buffer
putget
14
![Page 23: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/23.jpg)
Example: A chat serverSecond multi-threaded version
users/channels
stat. counters
T1
Ta
Tb
thread pool
buffer
putget
14
![Page 24: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/24.jpg)
Example: A chat serverSecond multi-threaded version
users/channels
stat. counters
T1
Ta
Tb
thread pool
buffer
putget
14
![Page 25: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/25.jpg)
Example: A chat serverSecond multi-threaded version
users/channels
stat. counters
T1
Ta
Tb
thread pool
buffer
putget
14
![Page 26: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/26.jpg)
Classical problemsSynchronization
Mutual exclusion• Avoid that multiple threads execute operations on the same
data concurrently (critical sections)
• Example: Update data used for statistics
Reader-Writer• Allow multiple readers or a single writer to access a data
• Example: Access to list of users and channels
15
![Page 27: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/27.jpg)
Classical problemsSynchronization
Mutual exclusion• Avoid that multiple threads execute operations on the same
data concurrently (critical sections)
• Example: Update data used for statistics
Reader-Writer• Allow multiple readers or a single writer to access a data
• Example: Access to list of users and channels
15
![Page 28: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/28.jpg)
Classical problemsCooperation
Producer-Consumer• Some threads produce some data that are consumed by other
threads
• Example: A buffer of requests to be processed
16
![Page 29: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/29.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems17
![Page 30: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/30.jpg)
A shared counter
int count = 0;
Thread 1:
for(i=0; i<10; i++){
count++;
}
Thread 2:
for(i=0; i<10; i++){
count++;
}
What is the final value of count?
• A value between 2 and 20
18
![Page 31: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/31.jpg)
A shared counter
int count = 0;
Thread 1:
for(i=0; i<10; i++){
count++;
}
Thread 2:
for(i=0; i<10; i++){
count++;
}
What is the final value of count?
• A value between 2 and 20
18
![Page 32: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/32.jpg)
A shared counter: Explanation
Let’s have a look at the (pseudo) assembly code for count++:
mov count, register
add $0x1, register
mov register, count
A possible interleave (for one iteration on each thread)mov count, registeradd $0x1, register
mov count, registeradd $0x1, register
mov register, countmov register, count
At the end, count=1 :-(
19
![Page 33: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/33.jpg)
A shared counter: Explanation
Let’s have a look at the (pseudo) assembly code for count++:
mov count, register
add $0x1, register
mov register, count
A possible interleave (for one iteration on each thread)mov count, registeradd $0x1, register
mov count, registeradd $0x1, register
mov register, countmov register, count
At the end, count=1 :-(
19
![Page 34: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/34.jpg)
A shared counter: Explanation
Let’s have a look at the (pseudo) assembly code for count++:
mov count, register
add $0x1, register
mov register, count
A possible interleave (for one iteration on each thread)mov count, registeradd $0x1, register
mov count, registeradd $0x1, register
mov register, countmov register, count
At the end, count=1 :-(
19
![Page 35: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/35.jpg)
A shared counter
This may happen:
• When threads execute on different processor cores• When preemptive threads execute on the same core
I A thread can be preempted at any time in this case
20
![Page 36: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/36.jpg)
Critical section
Critical resourceA critical resource should not be accessed by multiple threads atthe same time. It should be accessed in mutual exclusion.
Critical section (CS)
A critical section is a part of a program code that accesses acritical resource.
21
![Page 37: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/37.jpg)
Critical section
Critical resourceA critical resource should not be accessed by multiple threads atthe same time. It should be accessed in mutual exclusion.
Critical section (CS)
A critical section is a part of a program code that accesses acritical resource.
21
![Page 38: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/38.jpg)
Critical section: Definition of the problem
Safety
• Mutual exclusion: At most one thread can be in CS at a time
Liveness• Progress: If no thread is currently in CS and threads are trying
to access, one should eventually be able to enter the CS.
• Bounded waiting: Once a thread T starts trying to enter theCS, there is a bound on the number of times other threadsget in.
22
![Page 39: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/39.jpg)
Critical section: About liveness requirements
Liveness requirements are mandatory for a solution to be useful
Progress vs. Bounded waiting
• Progress: If no thread can enter CS, we don’t have progress.
• Bounded waiting: If thread A is waiting to enter CS while Brepeatedly leaves and re-enters C.S. ad infinitum, we don’thave bounded waiting
23
![Page 40: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/40.jpg)
Critical section: About liveness requirements
Liveness requirements are mandatory for a solution to be useful
Progress vs. Bounded waiting
• Progress: If no thread can enter CS, we don’t have progress.
• Bounded waiting: If thread A is waiting to enter CS while Brepeatedly leaves and re-enters C.S. ad infinitum, we don’thave bounded waiting
23
![Page 41: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/41.jpg)
Shared counter: New version
Thread 1:
Enter CS;
count++;
Leave CS;
Thread 2:
Enter CS;
count++;
Leave CS;
How to implement Enter CS and Leave CS?
24
![Page 42: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/42.jpg)
Shared counter: New version
Thread 1:
Enter CS;
count++;
Leave CS;
Thread 2:
Enter CS;
count++;
Leave CS;
How to implement Enter CS and Leave CS?
24
![Page 43: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/43.jpg)
Implementation: First try using busy waiting
Shared variables:
int count=0;
int busy=0;
Thread 1:
while(busy){;}
busy=1;
count++;
busy=0;
Thread 2:
while(busy){;}
busy=1;
count++;
busy=0;
25
![Page 44: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/44.jpg)
Exercise
Show through an example that the solution violates safety.
while(busy){;}while(busy){;}busy = 1
busy = 1count++
count++
• The 2 threads access count at the same time.
26
![Page 45: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/45.jpg)
Exercise
Show through an example that the solution violates safety.
while(busy){;}while(busy){;}busy = 1
busy = 1count++
count++
• The 2 threads access count at the same time.
26
![Page 46: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/46.jpg)
Exercise
Show through an example that the solution violates liveness.
while(busy){;}busy = 1count++
while(busy){;}busy = 0while(busy){;}busy = 1
while(busy){;}count++. . .
• With a bad interleaving of threads, Thread 2 never getsaccess to count.
27
![Page 47: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/47.jpg)
Exercise
Show through an example that the solution violates liveness.
while(busy){;}busy = 1count++
while(busy){;}busy = 0while(busy){;}busy = 1
while(busy){;}count++. . .
• With a bad interleaving of threads, Thread 2 never getsaccess to count.
27
![Page 48: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/48.jpg)
Synchronization primitives
To implement mutual exclusion, we need help from the hardware(and the operating system).
• Implementing mutual exclusion is the topic of next course.
Threading libraries provide synchronization primitives:• A set of functions that allow synchronizing threads
I LocksI SemaphoresI Condition variables
28
![Page 49: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/49.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems29
![Page 50: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/50.jpg)
Locks
A lock provides a means to achieve mutual exclusion.
Specification
A lock is defined by a lock variable and two methods: lock() andunlock().
• A lock can be free or held
• lock(): If the lock is free, the calling thread acquires the lockand enters the CS. Otherwise the thread is blocked.
• unlock(): Releases the lock. It has to be called by the threadcurrently holding the lock.
30
![Page 51: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/51.jpg)
Locks
A lock provides a means to achieve mutual exclusion.
Specification
A lock is defined by a lock variable and two methods: lock() andunlock().
• A lock can be free or held
• lock(): If the lock is free, the calling thread acquires the lockand enters the CS. Otherwise the thread is blocked.
• unlock(): Releases the lock. It has to be called by the threadcurrently holding the lock.
30
![Page 52: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/52.jpg)
Locks
A lock provides a means to achieve mutual exclusion.
Specification
A lock is defined by a lock variable and two methods: lock() andunlock().
• A lock can be free or held
• lock(): If the lock is free, the calling thread acquires the lockand enters the CS. Otherwise the thread is blocked.
• unlock(): Releases the lock. It has to be called by the threadcurrently holding the lock.
30
![Page 53: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/53.jpg)
Locks
A lock provides a means to achieve mutual exclusion.
Specification
A lock is defined by a lock variable and two methods: lock() andunlock().
• A lock can be free or held
• lock(): If the lock is free, the calling thread acquires the lockand enters the CS. Otherwise the thread is blocked.
• unlock(): Releases the lock. It has to be called by the threadcurrently holding the lock.
30
![Page 54: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/54.jpg)
Locks: Analogy
lock
unlock
shared data
synchronized
doors
• Calling lock, a thread entersa waiting room
• A single thread can be inthe CS room (hosting theshared data)• When the thread in the CS
room calls unlock, it leavesthe CS room, and lets onethread from the waitingroom enter (opens the doorsof the CS room)
I The doors of the CS roomare initially opened.
31
![Page 55: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/55.jpg)
Locks: Analogy
lock
unlock
shared data
synchronized
doors
• Calling lock, a thread entersa waiting room
• A single thread can be inthe CS room (hosting theshared data)
• When the thread in the CSroom calls unlock, it leavesthe CS room, and lets onethread from the waitingroom enter (opens the doorsof the CS room)
I The doors of the CS roomare initially opened.
31
![Page 56: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/56.jpg)
Locks: Analogy
lock
unlock
shared data
synchronized
doors
• Calling lock, a thread entersa waiting room
• A single thread can be inthe CS room (hosting theshared data)
• When the thread in the CSroom calls unlock, it leavesthe CS room, and lets onethread from the waitingroom enter (opens the doorsof the CS room)
I The doors of the CS roomare initially opened.
31
![Page 57: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/57.jpg)
Locks: Analogy
lock
unlock
shared data
synchronized
doors
• Calling lock, a thread entersa waiting room
• A single thread can be inthe CS room (hosting theshared data)• When the thread in the CS
room calls unlock, it leavesthe CS room, and lets onethread from the waitingroom enter (opens the doorsof the CS room)
I The doors of the CS roomare initially opened.
31
![Page 58: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/58.jpg)
Locks: Analogy
lock
unlock
shared data
synchronized
doors
• Calling lock, a thread entersa waiting room
• A single thread can be inthe CS room (hosting theshared data)• When the thread in the CS
room calls unlock, it leavesthe CS room, and lets onethread from the waitingroom enter (opens the doorsof the CS room)I The doors of the CS room
are initially opened.
31
![Page 59: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/59.jpg)
Programming with locks
All critical data should be protected by a lock!
• Critical = accessed by more than one thread, at least onewrite
• Exception is initialization, before data is exposed to otherthreads
• This is the responsibility of the application writer
32
![Page 60: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/60.jpg)
Pthread locks: Mutexes
• mutex: variable of type pthread mutex t
• pthread mutex init(&mutex, ...): initialize the mutexI The macro PTHREAD MUTEX INITIALIZER can be used to
initialize a mutex allocated statically with the default options
• pthread mutex destroy(&mutex): destroy the mutex
• pthread mutex lock(&mutex)
• pthread mutex unlock(&mutex)
• pthread mutex trylock(&mutex): is equivalent to lock(),except that if the mutex is held, it returns immediately withan error code
33
![Page 61: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/61.jpg)
Pthread locks: Example
#include <pthread.h>
int count=0;
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
void* thread_routine(void *arg){
/* ... */
pthread_mutex_lock(&count_mutex);
count++;
pthread_mutex_unlock(&count_mutex);
/* ... */
}
34
![Page 62: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/62.jpg)
Pthread locks attributesman pthread mutex lock
Several attributes of a lock can be configured at initializationamong which:• type
I NORMAL: Deadlock on relock1
I RECURSIVE: Allows relocking. A lock count is implemented (asmany lock() as unlock() calls required).
I ERRORCHECK: Error returned on relock.I DEFAULT: Usually maps to NORMAL.
• robust: Defines what happens if a thread terminates withoutreleasing a lock, and if a non-owner thread calls unlock().
• Other attributes are related to priority management andvisibility of the lock.
1A thread calls lock() on a lock it already locked.35
![Page 63: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/63.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems36
![Page 64: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/64.jpg)
Semaphores
• Locks ensure mutual exclusion.
• A semaphore is another mechanism that allows controllingaccess to shared variables but is more powerful than a lock.
• Semaphores were proposed by Dijkstra in 1968
37
![Page 65: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/65.jpg)
Semaphores
A semaphore is initialized with an integer value N and can bemanipulated with two operations P and V.
About the interface• P stands for Proberen (Dutch) – try
• V stands for Verhogen (Dutch) – increment
POSIX interface• P → int sem wait(sem t *s)
• V → int sem post(sem t *s)I Other interfaces call it sem signal()
38
![Page 66: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/66.jpg)
Semaphores
When a tread calls sem wait():
N = N - 1;
if( N < 0 )
Calling thread is blocked
When a tread calls sem post():
N = N +1;
if( N <= 0 )
One blocked thread is unblocked
About the value of N:
• If N > 0, N is the capacity of the semaphore
• if N < 0, N is the number of blocked threads
39
![Page 67: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/67.jpg)
Semaphores
When a tread calls sem wait():
N = N - 1;
if( N < 0 )
Calling thread is blocked
When a tread calls sem post():
N = N +1;
if( N <= 0 )
One blocked thread is unblocked
About the value of N:
• If N > 0, N is the capacity of the semaphore
• if N < 0, N is the number of blocked threads
39
![Page 68: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/68.jpg)
Mutual exclusion with semaphores
• Initializing a semaphore with value N can be seen as providingit with N tokens• To implement critical sections, a semaphore should be
initialized with N = 1I Warning: A semaphore with N = 1 and a lock are not
equivalent
Example
#include <semaphore.h>
int count=0;
sem_t count_mutex;
sem_init(&count_mutex, 0, 1);
/* ... */
sem_wait(&count_mutex);
count++;
sem_post(&count_mutex);
40
![Page 69: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/69.jpg)
Mutual exclusion with semaphores• Initializing a semaphore with value N can be seen as providing
it with N tokens• To implement critical sections, a semaphore should be
initialized with N = 1I Warning: A semaphore with N = 1 and a lock are not
equivalent
Example
#include <semaphore.h>
int count=0;
sem_t count_mutex;
sem_init(&count_mutex, 0, 1);
/* ... */
sem_wait(&count_mutex);
count++;
sem_post(&count_mutex);
40
![Page 70: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/70.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems41
![Page 71: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/71.jpg)
Specification of the problem
Recall
T1
Ta
Tb
buffer
putget
Specification
• A buffer of fixed size
• Producer threads put items into the buffer. The put operationblocks if the buffer is full
• Consumer threads get items from the buffer. The getoperation blocks if the buffer is empty
42
![Page 72: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/72.jpg)
Producer-Consumer
void producer (void *ignored) {
for (;;) {
/* produce an item and put in
nextProduced */
while (count == BUFFER SIZE) {
/* Do nothing */
}
buffer [in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
count++;
}
}
void consumer (void *ignored) {
for (;;) {
while (count == 0) {
/* Do nothing */
}
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
count--;
/* consume the item in
nextConsumed */
}
}
43
![Page 73: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/73.jpg)
Producer-Consumer
void producer (void *ignored) {
for (;;) {
/* produce an item and put in
nextProduced */
while (count == BUFFER SIZE) {
/* Do nothing */
}
buffer [in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
count++;
}
}
void consumer (void *ignored) {
for (;;) {
while (count == 0) {
/* Do nothing */
}
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
count--;
/* consume the item in
nextConsumed */
}
}
Not correct: shared data are not protected
• count can be accessed by the prod. and the cons.
• With multiple prod./cons., concurrent accesses to in, out, buffer
43
![Page 74: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/74.jpg)
Producer-Consumer with Locks
mutex t mutex = MUTEX INITIALIZER;
void producer (void *ignored) {
for (;;) {
/* produce an item and put in
nextProduced */
mutex lock (&mutex);
while (count == BUFFER SIZE) {
thread yield ();
}
buffer [in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
count++;
mutex unlock (&mutex);
}
}
void consumer (void *ignored) {
for (;;) {
mutex lock (&mutex);
while (count == 0) {
thread yield ();
}
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
count--;
mutex unlock (&mutex);
/* consume the item in
nextConsumed */
}
}
43
![Page 75: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/75.jpg)
Producer-Consumer with Locksmutex t mutex = MUTEX INITIALIZER;
void producer (void *ignored) {
for (;;) {
/* produce an item and put in
nextProduced */
mutex lock (&mutex);
while (count == BUFFER SIZE) {
thread yield ();
}
buffer [in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
count++;
mutex unlock (&mutex);
}
}
void consumer (void *ignored) {
for (;;) {
mutex lock (&mutex);
while (count == 0) {
thread yield ();
}
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
count--;
mutex unlock (&mutex);
/* consume the item in
nextConsumed */
}
}
Not correct: If a thread enters a while loop, all threads are blockedforever (deadlock)
• yield() does not release the lock
43
![Page 76: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/76.jpg)
Producer-Consumer with Locks
mutex t mutex = MUTEX INITIALIZER;
void producer (void *ignored) {
for (;;) {
/* produce an item and put in
nextProduced */
mutex lock (&mutex);
while (count == BUFFER SIZE) {
mutex unlock (&mutex);
thread yield ();
mutex lock (&mutex);
}
buffer [in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
count++;
mutex unlock (&mutex);
}
}
void consumer (void *ignored) {
for (;;) {
mutex lock (&mutex);
while (count == 0) {
mutex unlock (&mutex);
thread yield ();
mutex lock (&mutex);
}
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
count--;
mutex unlock (&mutex);
/* consume the item in
nextConsumed */
}
}
43
![Page 77: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/77.jpg)
Producer-Consumer with Locksmutex t mutex = MUTEX INITIALIZER;
void producer (void *ignored) {
for (;;) {
/* produce an item and put in
nextProduced */
mutex lock (&mutex);
while (count == BUFFER SIZE) {
mutex unlock (&mutex);
thread yield ();
mutex lock (&mutex);
}
buffer [in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
count++;
mutex unlock (&mutex);
}
}
void consumer (void *ignored) {
for (;;) {
mutex lock (&mutex);
while (count == 0) {
mutex unlock (&mutex);
thread yield ();
mutex lock (&mutex);
}
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
count--;
mutex unlock (&mutex);
/* consume the item in
nextConsumed */
}
}
Correct ... but busy waiting
• We don’t want busy waiting
43
![Page 78: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/78.jpg)
About Busy WaitingBusy waiting
Waiting for some condition to become true by repeatedly checking(spinning) the value of some variable.
Why is it bad?
• Waste of CPU cyclesI Use CPU cycles to check the value of a variable while there is
no evidence that this value has changed.I Follows from previous comment: Using sleep is still busy
waiting.
• On a single processor: Wasted cycles could have been used byother threads.• On a multi-processor: Repeatedly reading a variable that is
used by other threads can slow down these threads.I In specific cases, with a careful design, busy waiting can be
efficient.
44
![Page 79: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/79.jpg)
Cooperation
Cooperation = Synchronization + Communication
• Synchronization: Imposing an order on the execution ofinstructions
• Communication: Exchanging information between threads
Semaphores allow cooperation between threads
45
![Page 80: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/80.jpg)
Producer-Consumer with semaphores
• Initialize fullCount to 0 (block consumer on empty buffer)
• Initialize emptyCount to N (block producer when buffer full)
• An additional semaphore (initialized to 1) should be used formutual exclusion (a lock could be used instead)
void producer (void *ignored) {
for (;;) {
/* produce an item and put in
nextProduced */
sem_wait(&emptyCount);
buffer [in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
/*count++;*/
sem_post(&fullCount)
}
}
void consumer (void *ignored) {
for (;;) {
sem_wait(&fullCount);
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
/*count--;*/
sem_post(&emptyCount);
/* consume the item in
nextConsumed */
}
}
46
![Page 81: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/81.jpg)
Producer-Consumer with semaphores
• Initialize fullCount to 0 (block consumer on empty buffer)
• Initialize emptyCount to N (block producer when buffer full)
• An additional semaphore (initialized to 1) should be used formutual exclusion (a lock could be used instead)
void producer (void *ignored) {
for (;;) {
/* produce an item and put in
nextProduced */
sem_wait(&emptyCount);
sem_wait(&mutex)
buffer [in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
/*count++;*/
sem_post(&mutex)
sem_post(&fullCount)
}
}
void consumer (void *ignored) {
for (;;) {
sem_wait(&fullCount);
sem_wait(&mutex)
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
/*count--;*/
sem_post(&mutex)
sem_post(&emptyCount);
/* consume the item in
nextConsumed */
}
}
46
![Page 82: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/82.jpg)
Comments on semaphores
• Semaphores allow elegant solutions to some problems(producer-consumer, reader-writer)
• However they are quite error prone:I If you call wait instead of post, you’ll have a deadlockI If you forget to protect parts of your code, you might violate
mutual exclusionI You have “tokens” of different types, which may be hard to
reason about
This is why other constructs have been proposed
47
![Page 83: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/83.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems48
![Page 84: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/84.jpg)
Condition variables (pthreads)
A condition variable is a special shared variable.• It allows a thread to explicitly put itself to wait.
I The condition variable can be seen as a container of waitingthreads.
I As such, this variable does not have a value.
• It is used together with a mutex:I When a thread puts itself to wait, the corresponding mutex is
released.
• It is often associated to a logical condition (reason for thisname)
49
![Page 85: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/85.jpg)
Condition variables (pthreads)Interface
• cond: variable of type pthread cond t
• pthread cond init(&cond, ...): initialize the conditionI The macro PTHREAD COND INITIALIZER can be used to
initialize a condition variable allocated statically with thedefault options
• void pthread cond wait(&cond, &mutex): atomicallyunlock mutex and put the thread to wait on cond.
• void pthread cond signal(&cond) andpthread cond broadcast(&cond): Wake one/all thethreads waiting on cond.
50
![Page 86: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/86.jpg)
Condition variable: Analogy
lock
unlock
shared data
synchronized
doors
51
![Page 87: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/87.jpg)
Condition variable: Analogy
wait
signal
lock
unlock
synchronized
doors
shared data
51
![Page 88: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/88.jpg)
On the semantic of the operations
• Calling wait() releases the lock similarly to unlock().
• When a thread is woken up by a call to signal() (orbroadcast()), it is guaranteed that at the time it returnsfrom wait(), it owns the corresponding lock again.I However, it has to compete with other threads to acquire that
lock before returning from wait().
• On a call to signal(), any of the waiting threads might bethe one that is woken up.
• Calling functions signal() and broadcast() does notrequire owning the lock.I However in most cases the lock should be held for the
application logic to be correct.
52
![Page 89: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/89.jpg)
Producer-Consumer with condition variablesmutex t mutex = MUTEX INITIALIZER;
cond t nonempty = COND INITIALIZER;
cond t nonfull = COND INITIALIZER;
void producer (void *ignored) {
for (;;) {
/* produce an item and
put in nextProduced */
mutex lock (&mutex);
while (count == BUFFER SIZE)
cond wait (&nonfull, &mutex);
buffer [in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
count++;
cond signal (&nonempty);
mutex unlock (&mutex);
}
}
void consumer (void *ignored) {
for (;;) {
mutex lock (&mutex);
while (count == 0)
cond wait (&nonempty, &mutex);
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
count--;
cond signal (&nonfull);
mutex unlock (&mutex);
/* consume the item
in nextConsumed */
}
}
Beware: this solution does not warrant First Come First Served!
53
![Page 90: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/90.jpg)
More on condition variablesWhy must cond wait both release mutex and sleep? Why notseparate mutexes and condition variables?
while (count == BUFFER SIZE) {
mutex unlock (&mutex);
cond wait(&nonfull);
mutex lock (&mutex);
}
A thread could end up stuck waiting because of a bad interleavingI A condition variable has no associated state
PRODUCER
while (count == BUFFER SIZE){
mutex unlock (&mutex);
cond wait (&nonfull);
}
CONSUMER
mutex lock (&mutex);
...
count--;
cond signal (&nonfull);
54
![Page 91: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/91.jpg)
More on condition variablesWhy must cond wait both release mutex and sleep? Why notseparate mutexes and condition variables?
while (count == BUFFER SIZE) {
mutex unlock (&mutex);
cond wait(&nonfull);
mutex lock (&mutex);
}
A thread could end up stuck waiting because of a bad interleavingI A condition variable has no associated state
PRODUCER
while (count == BUFFER SIZE){
mutex unlock (&mutex);
cond wait (&nonfull);
}
CONSUMER
mutex lock (&mutex);
...
count--;
cond signal (&nonfull);
54
![Page 92: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/92.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems55
![Page 93: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/93.jpg)
Monitors
• A monitor is a synchronization construct
• It provides synchronization mechanisms similar to mutex +condition variables. (Some people call both “monitors”)
Definition• A monitor is an object/module with a set of methods.
• Each method is executed in mutual exclusion
• Condition variables (or simply “conditions”) are defined withthe same semantic as defined previously
56
![Page 94: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/94.jpg)
Comments on monitors
• Proposed by Brinch Hansen (1973) and Hoare (1974)
• Possibly less error prone than raw mutexes
• Basic synchronization mechanism in Java
• Different flavors depending on the semantic of signal:I Hoare-style: The signaled thread get immediately access to the
monitor. The signaling thread waits until the signaled threadsleaves the monitor.
I Mesa-style (java): The signaling thread stays in the monitor.
• Semaphores can be implemented using monitors and monitorscan be implemented using semaphores
57
![Page 95: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/95.jpg)
Agenda
Goals of the lecture
A Multi-Threaded Application
Mutual Exclusion
Locks
Semaphores
The Producer-Consumer Problem
Condition Variables
Monitors
Other synchronization problems58
![Page 96: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/96.jpg)
The Reader-Writer problem
Problem statement• Several threads try to access the same shared data, some
reading, other writing.
• Either a single writer or multiple readers can access the shareddata at any time
Different flavors• Priority to readers
• Priority to writers
59
![Page 97: Operating Systems - Thread Synchronization Primitives · Shared memory is the inter-thread communication medium. Is it easy to use shared memory to cooperate? NO The problem: A set](https://reader030.fdocuments.in/reader030/viewer/2022040112/5eb5ac3896844760447294d3/html5/thumbnails/97.jpg)
The Dining Philosophers problemProposed by Dijkstra
Problem statement5 philosophers spend their live alternatively thinking and eating.They sit around a circular table. The table has a big plate of ricebut only 5 chopsticks, placed between each pair of philosophers.When a philosopher wants to eat, he has to peak the chopsticks onhis left and on his right. Two philosophers can’t use a chopstick atthe same time. How to ensure that no philosopher will starve?
Goals• Avoid deadlocks: Each philosopher holds one chopstick
• Avoid starvation: Some philosophers never eat
60