Concurrent Programming in Harmony: Signaling and Conditional Critical Sections
CS 4410 Operating Systems
[Robbert van Renesse]
Concurrent Programming in Harmony: Signaling and Conditional - - PowerPoint PPT Presentation
Concurrent Programming in Harmony: Signaling and Conditional Critical Sections CS 4410 Operating Systems [Robbert van Renesse] Remember the recruiter... Asked >100 candidates if they could implement two threads, where one thread had to
[Robbert van Renesse]
2
3
def def T0(): while not while not done: pass; pass; ; ; def def T1(): done = True True; ; done = False False; spawn spawn T0(); spawn spawn T1();
4
def def T0(): while not while not done: pass; pass; ; ; def def T1(): done = True True; ; done = False False; spawn spawn T0(); spawn spawn T1();
5
def def T0(): await await done; ; def def T1(): done = True True; ; done = False False; spawn spawn T0(); spawn spawn T1();
import import synch; def def T0(): lock(?condition); assert assert done; # make sure T1 sent signal # no unlock ; def def T1(): # no lock done = True True; unlock(?condition); ; done = False False; condition = Lock(); lock(?condition); # weird stuff during init… spawn spawn T0(); spawn spawn T1();
6
import import synch; def def T0(): lock(?condition); assert assert done; # make sure T1 sent signal # no unlock ; def def T1(): # no lock done = True True; unlock(?condition); ; done = False False; condition = Lock(); lock(?condition); # weird stuff during init… spawn spawn T0(); spawn spawn T1();
7
8
9
10
(although see R/W lock)
11
12
13
import import synch; def def T0(): P(?condition); # wait for signal assert assert done; ; def def T1(): done = True True; V(?condition); # send signal ; done = False False; condition = Semaphore(0); spawn spawn T0(); spawn spawn T1();
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Invariants:
39
40
41
42
43
44
45
46
47
48
49
50
51
“condition variable” wait method signal method
52
53
54
55
56
mon_enter: grab lock mon_exit: release lock Condition: consists of lock + list of processes waiting wait: unlock + add process context to list of waiters notify: move one waiter to the list of suspended processes associated with the lock notifyAll: move all waiters to the list of suspended processes associated with the lock
57
Invariants:
rwlock protects the nreaders/nwriters variables, not the critical section!
58
59
60
61
62