COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores...
Transcript of COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores...
![Page 1: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/1.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
COMP3151/9151 Foundations ofConcurrency Lecture 6
Semaphores, Monitors, POSIX Threads, Java
Kai Engelhardt
CSE, UNSW (and data61)
Revision: 1.5 of Date: 2017/08/30 00:52:45 UTC
1
![Page 2: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/2.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Semaphores
are our first important programming abstraction for concurrencycontrol.
Ben-Ari suggests to think about a semaphore S as a pair (v , L) ofnon-negative integers and sets of process descriptor. S should beinitialised to some v > 0 and L = ∅.
There are 2 basic operations a process p could performs on S :
wait(S) decrements v if it’s positive; otherwise suspends p andadds it to L,
signal(S) unblock a member of L if there is one; otherwiseincrement v .
2
![Page 3: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/3.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Semaphores classes
Semaphores as defined above are known as weak semaphores in theliterature. If L is a FIFO queue instead of a set, we get a strongsemaphore.
We often treat sempahores as if they were just an integer with anaccess policy requiring us to use only the two operations:
1 inline wait(S) { d step { S > 0 ; S−− } }2 inline signal (S) { d step{ S++ }}
This is known as a busy-wait semaphore.
3
![Page 4: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/4.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Basic Uses of Semaphores (1): CSs
Algorithm 6.1: Critical section with semaphores (two processes)binary semaphore S ← (1, ∅)p q
loop forever loop foreverp1: non-critical section q1: non-critical sectionp2: wait(S) q2: wait(S)p3: critical section q3: critical sectionp4: signal(S) q4: signal(S)
4
![Page 5: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/5.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
State Diagram for the Semaphore Solution
p1: wait(S),
q1: wait(S),
(1, ∅)
'
&
$
%p2: signal(S),
q1: wait(S),
(0, ∅)
'
&
$
%p2: signal(S),q1: blocked,
(0, {q})
'
&
$
%p1: wait(S),
q2: signal(S),
(0, ∅)
'
&
$
%p1: blocked,
q2: signal(S),
(0, {p})
'
&
$
%
--
-
�
@@@@@R@
@@@@I
��
���
@@
@@@I p
q
5
![Page 6: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/6.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.2: Critical section with semaphores (N proc.)binary semaphore S ← (1, ∅)
loop foreverp1: non-critical sectionp2: wait(S)p3: critical sectionp4: signal(S)
6
![Page 7: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/7.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Reasoning about semaphores
Proofs of safety properties of programs with semaphores often hingeon the appropriate exploitation of semaphore invariants. To expresssemaphore invariants, we let #wait(S) and #signal(S) denote thenumber of wait(S), resp., signal(S) operations executed so far. If Swas initialised to (k , ∅) then
v ≥ 0 (1)
v = k + #signal(S)−#wait(S) (2)
7
![Page 8: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/8.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.3: Producer-consumer (infinite buffer)queue[T] buffer ← empty queuesemaphore full ← (0, ∅)
producer consumerT d T dloop forever loop forever
p1: d ← produce q1: wait(full)p2: append(d, buffer) q2: d ← take(buffer)p3: signal(full) q3: consume(d)
8
![Page 9: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/9.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Partial State Diagram for Producer-Consumer withInfinite Buffer
p1: append,q1: wait(S),
(0, ∅), [ ]
'
&
$
%p2: signal(S),
q1: wait(S),
(0, ∅), [x ]
'
&
$
%p1: append,q1: wait(S),
(1, ∅), [x ]
'
&
$
%p1: append,q1: blocked,(0, {con}), [ ]
'
&
$
%p2: signal(S),q1: blocked,
(0, {con}), [x ]
'
&
$
%p1: append,
q2: take,(0, ∅), [x ]
'
&
$
%
- - -
- - -
? ? ?
6
9
![Page 10: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/10.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.4: Producer-consumer (finite buffer, semaphores)bounded[N] queue[T] buffer ← empty queuesemaphore full ← (0, ∅)semaphore empty ← (N, ∅)
producer consumerT d T dloop forever loop forever
p1: d ← produce q1: wait(full)p2: wait(empty) q2: d ← take(buffer)p3: append(d, buffer) q3: signal(empty)p4: signal(full) q4: consume(d)
10
![Page 11: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/11.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
The Dining Philosophers Problem
Problem
Five philosophers sit around a diningtable with a huge bowl of spaghetti inthe centre, five plates, and five forks, alllaid out evenly. For whatever reason,philosophers can eat spaghetti only withtwo forks.a The philosophers would liketo alternate between eating and thinking.
aThis is obviously a poor adaptation of anold problem from the East where requiring twochopsticks is more convincing.
11
![Page 12: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/12.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.5: Dining philosophers (outline)loop forever
p1: thinkp2: preprotocolp3: eatp4: postprotocol
Same structure as the CS problem but with each fork being acritical section.
12
![Page 13: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/13.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.6: Dining philosophers (first attempt)semaphore array [0..4] fork ← [1,1,1,1,1]
loop foreverp1: thinkp2: wait(fork[i])p3: wait(fork[i+1])p4: eatp5: signal(fork[i])p6: signal(fork[i+1])
13
![Page 14: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/14.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.7: Dining philosophers (second attempt)semaphore array [0..4] fork ← [1,1,1,1,1]semaphore room ← 4
loop foreverp1: thinkp2: wait(room)p3: wait(fork[i])p4: wait(fork[i+1])p5: eatp6: signal(fork[i])p7: signal(fork[i+1])p8: signal(room)
14
![Page 15: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/15.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.8: Dining philosophers (third attempt)semaphore array [0..4] fork ← [1,1,1,1,1]
philosopher 4loop forever
p1: thinkp2: wait(fork[0])p3: wait(fork[4])p4: eatp5: signal(fork[0])p6: signal(fork[4])
15
![Page 16: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/16.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Main Disadvantages of Semaphores
1 Lack of structure: when building a large system, responsibilityis diffused among implementers.Someone forgets to call signal =⇒ possible deadlock.
2 Global visibility: when something goes wrong, the wholeprogram must be inspected =⇒ deadlocks are hard to isolate.
Solution: monitors concentrate one responsibility into a singlemodule and encapsulate critical resources.
16
![Page 17: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/17.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Monitors
more structure than semaphores; more control than await
History:
Hoare’s 1974 paper
languages — Concurrent Pascal (1975). . . Java, Pthreadslibrary
Generalise objects:
data encapsulation—all fields are private
implicit mutual exclusion—procedure invocations are implicitlyatomic
explicit signaling
17
![Page 18: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/18.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.9: Atomicity of monitor operationsmonitor CS
integer n ← 0
operation incrementinteger temptemp ← nn ← temp + 1
p qp1: loop ten times q1: loop ten timesp2: CS.increment q2: CS.increment
18
![Page 19: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/19.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Program structure
monitor1 . . . monitorM
process1 . . . processN
processes interact indirectly by using the same monitor
processes call monitor procedures
at most one call active in a monitor at a time — by definition
explicit signaling using condition variables
monitor invariant: predicate about local state that is true whenno call is active
19
![Page 20: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/20.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Condition variables
are named FIFO queues of blocked processes:
Ben-Ari writes: condition cv
Processes executing a procedure of that monitor can
voluntarily suspend themselves using waitC(cv),
unblock the first suspended process by calling signalC(cv), or
test for emptiness of the queue: empty(cv).
20
![Page 21: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/21.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.10: Semaphore simulated with a monitormonitor Sem
integer s ← kcondition notZerooperation wait
if s = 0waitC(notZero)
s ← s − 1operation signal
s ← s + 1signalC(notZero)
p qloop forever loop forever
non-critical section non-critical sectionp1: Sem.wait q1: Sem.wait
critical section critical sectionp2: Sem.signal q2: Sem.signal
21
![Page 22: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/22.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
State Diagram for the Semaphore Simulation
p1: Sem.wait,q1: Sem.wait,
1, 〈〉
#"
!
p1: Sem.wait,q2: Sem.signal,
0, 〈〉
#"
!
p2: Sem.signal,q1: Sem.wait,
0, 〈〉
#"
!
blocked,q2: Sem.signal
0, 〈p〉
#"
!
p2: Sem.signal,blocked,
0, 〈q〉
#"
!
-
�-
6
?
-
6
��������������������9
22
![Page 23: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/23.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.11: Producer-consumer (finite buffer, monitor)monitor PC
bufferType buffer ← emptycondition notEmptycondition notFulloperation append(datatype V)
if buffer is fullwaitC(notFull)
append(V, buffer)signalC(notEmpty)
operation take()datatype Wif buffer is empty
waitC(notEmpty)W ← head(buffer)signalC(notFull)return W
23
![Page 24: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/24.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.11: Producer-consumer . . . (continued)producer consumer
datatype D datatype Dloop forever loop forever
p1: D ← produce q1: D ← PC.takep2: PC.append(D) q2: consume(D)
24
![Page 25: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/25.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
The Immediate Resumption Requirement
AAA
AAA
���
���
HHH
monitor
condition 1
condition 2
waiting
signaling
fff
ffff
ff
ff
f
25
![Page 26: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/26.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Signaling disciplines
Precedences:
S the signaling process
W waiting on a condition variable
E waiting on entry
signal and urgent wait: E < S < W (Hoare’s paper)
signal and continue E = W < S (Java, pthreads)
26
![Page 27: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/27.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Diagram for monitors
entryqueue
executing in monitor
condition variable queue
monitor free
SW
wait
SW
SC
call return
27
![Page 28: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/28.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Simulating Monitors in Promela 1
1 bool lock = false;2
3 typedef Condition {4 bool gate;5 byte waiting ;6 }7 inline enterMon() {8 atomic {9 ! lock;
10 lock = true;11 }12 }13 inline leaveMon() {14 lock = false;15 }
28
![Page 29: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/29.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Simulating Monitors in Promela 2
1 inline waitC(C) {2 atomic {3 C.waiting ++;4 lock = false; /∗ Exit monitor ∗/5 C.gate; /∗ Wait for gate ∗/6 lock = true; /∗ IRR ∗/7 C.gate = false; /∗ Reset gate ∗/8 C.waiting−−;9 }
10 }
29
![Page 30: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/30.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Simulating Monitors in Promela 3
1 inline signalC (C) {2 atomic {3 if4 /∗ Signal only if waiting ∗/5 :: (C.waiting > 0) −>6 C.gate = true;7 ! lock; /∗ IRR − wait for released lock ∗/8 lock = true; /∗ Take lock again ∗/9 :: else
10 fi ;11 }12 }13
14 #define emptyC(C) (C.waiting == 0)
30
![Page 31: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/31.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.12: Readers and writers with a monitormonitor RW
integer readers ← 0integer writers ← 0condition OKtoRead, OKtoWriteoperation StartRead
if writers 6= 0 or not empty(OKtoWrite)waitC(OKtoRead)
readers ← readers + 1signalC(OKtoRead)
operation EndReadreaders ← readers − 1if readers = 0
signalC(OKtoWrite)
31
![Page 32: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/32.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Algorithm 6.12: Readers and writers with a monitor (continued)operation StartWrite
if writers 6= 0 or readers 6= 0waitC(OKtoWrite)
writers ← writers + 1
operation EndWritewriters ← writers − 1if empty(OKtoRead)
then signalC(OKtoWrite)else signalC(OKtoRead)
reader writerp1: RW.StartRead q1: RW.StartWritep2: read the database q2: write to the databasep3: RW.EndRead q3: RW.EndWrite
32
![Page 33: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/33.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
POSIX threads
What? a widely available, standardized C library for multi-threadedprogrammingHow?
1 include standard headers:
#include <pthread.h>
2 create some threads by (repeatedly) calling
int pthread create (pthread t ∗thread,pthread attr t ∗attr ,void ∗(∗start routine )(void ∗),void ∗arg);
If attr is NULL, default attributes are used, eg on linux, thethread is joinable and has non-real-time scheduling policy.
33
![Page 34: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/34.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Thread Attributes
Upon thread creation one needs to decide whether the creatingthread wants to be able to synchronize with the termination of thecreated thread (which is the default behaviour) using
int pthread join (pthread t th, void ∗∗t ret );
which “suspends the execution of the calling thread until the threadidentified by th terminates, either by calling pthread exit or by beingcancelled.”
Pitfall
When a joinable thread terminates, its memory resources (threaddescriptor and stack) are not deallocated until another threadperforms pthread join on it. Therefore, pthread join must be calledonce for each joinable thread created to avoid memory leaks.
34
![Page 35: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/35.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
If synchronization is not required, save a few resources and avoidthe pitfall:
pthread attr t attr ;pthread attr init (&attr);pthread attr setdetachstate(&attr, PTHREAD CREATE DETACHED);
35
![Page 36: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/36.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Examples
simple.c: shows the basic structure
pc.busy.c: producer/consumer using busy waiting and a singlebufferpcn.busy.c: producer/consumer using busy waiting and ann-place buffer
36
![Page 37: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/37.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Semaphores in POSIX threads
#include <semaphore.h>
int sem init (sem t ∗sem, int pshared,unsigned int value);
int sem wait(sem t ∗sem); /∗ P(sem) ∗/int sem trywait(sem t ∗sem);int sem post(sem t ∗sem); /∗ V(sem) ∗/int sem getvalue(sem t ∗sem, int ∗sval );int sem destroy(sem t ∗sem);
If pshared is zero then the sem is local to the current process,otherwise it is to be shared between several processes.
37
![Page 38: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/38.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Examples
pc.sems.c: producer/consumer using semaphores and a single buffer
pcn.sems.c: producer/consumer using semaphores and an n-placebuffer
38
![Page 39: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/39.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Locks & Monitors in POSIX Threads
In pthreads, a basic lock is called a mutex, for mutual exclusiondevice.
Such a device is in one of two states:
unlocked ' not owned by any thread
locked ' owned by one thread
A thread attempting to lock a mutex already owned by anotherthread is suspended.
A monitor procedure is simulated using pthreads by bracketing theprocedure code with mutex lock/unlock.
39
![Page 40: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/40.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Details: RTFM
Static initialization:
pthread mutex t fastmutex =PTHREAD MUTEX INITIALIZER;
pthread mutex t recmutex =PTHREAD RECURSIVE MUTEX INITIALIZER NP;
pthread mutex t errchkmutex =PTHREAD ERRORCHECK MUTEX INITIALIZER NP;
40
![Page 41: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/41.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
int pthread mutex init ( pthread mutex t ∗mutex,const pthread mutexattr t ∗mutexattr);
int pthread mutex lock( pthread mutex t ∗mutex);int pthread mutex trylock(pthread mutex t ∗mutex);int pthread mutex unlock( pthread mutex t ∗mutex);int pthread mutex destroy(pthread mutex t ∗mutex);
pthread mutex lock behaviour depends on mutex kind. RTFM.
41
![Page 42: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/42.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Condition variables
pthread cond t cond = PTHREAD COND INITIALIZER;int pthread cond init (pthread cond t ∗cond,
pthread condattr t ∗cond attr );int pthread cond signal (pthread cond t ∗cond);int pthread cond broadcast(pthread cond t ∗cond);int pthread cond wait(pthread cond t ∗cond,
pthread mutex t ∗mutex);int pthread cond timedwait(pthread cond t ∗cond,
pthread mutex t ∗mutex,const struct timespec ∗abstime);
int pthread cond destroy (pthread cond t ∗cond);
ZZZZZzzzzzzzzzzzzzzzzzzzzz
42
![Page 43: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/43.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Example
summing the elements of a matrix uses a thread for each strip
See matrix.sum.c
43
![Page 44: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/44.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Process states
For our purposes it suffices to consider a process as being in one offive states:
inactive ready running
blocked
completed- --�
?
HHHH
HHHY
while in some version of reality. . .
44
![Page 45: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/45.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Java process states
new
runnable running
suspended
blocked
suspended-blocked
dead
Thread t =new Thread(...)
t.start()
yield()
t.supend()
t.resume()
t.sleep()wait()
t2.join()
perf. blocking I/O
sleep() completes, t2 terminates,blocking I/O fulfilled, notify[All]()
t.supend()
run() completest.stop()
t.resume()
...
45
![Page 46: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/46.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Thread state
A Java thread object t always is in one of 7 possible states:
new – just created Thread t = new Thread(...);
runnable – entered when t. start () is called. All runnablethreads are maintained in the runnable set or readyqueue and take turns executing their run method.
running – actually executing; can get back to runnable bycalling yield
suspended – entered when t. suspend(); is called; can get back byanother thread calling t. resume()
But do read Oracle’s gospel on deprecated thread primitives.
46
![Page 47: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/47.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
blocked – entered when
t. sleep () is called,it calls wait() inside a synchronized method,it calls join () on some thread object that hasn’t,terminated yet, orit performs blocking I/O
and exited to enter the runnable state when
its sleep method call completes,it joins with a terminated thread,its blocking I/O request is fulfilled by the OS, orsome other thread calls notify or notifyAll in asynchronized method of the object t earlier calledwait.
suspended-blocked – suspended while blocked. . .
dead – entered when run completes or t. stop() is called.
47
![Page 48: COMP3151/9151 Foundations of Concurrency Lecture 6cs3151/17s2/lec/PDF/lecture06a.pdf · Semaphores Monitors C + POSIX Threads Java Threads Semaphores are our rst important programming](https://reader035.fdocuments.in/reader035/viewer/2022070719/5edf6ab6ad6a402d666ac3cd/html5/thumbnails/48.jpg)
Semaphores Monitors C + POSIX Threads Java Threads
Execution model
Each thread t has a priority, either inherited from its creator threador set explicitly using t. setPriority (..) . The Java thread schedulerensures that one of the highest priority threads executes. It evenpreempts a lower priority running thread when a higher prioritythread becomes runnable.
All highest priority runnable threads share the process’s CPU time ina round-robin fashion executing for time slices in the order of 100ms.
48