1 Shared Memory
2 processes
3 Types of Shared Variables Read/Write Test & Set Read-Modify-Write
4 Read/Write Variables Read(v)Write(v,a) return(v); v = a; Or simpler: x = v; v = x; (read v) (write v)
5 write 10
6 10
7 write read
8 write read 10
9 write 10 write 20 Simultaneous writes
10 write 10 write 20 Slower process Possibility 1 20 Simultaneous writes
11 write 10 write 20 Slower process Possibility 2 10 Simultaneous writes
12 write In general: write x Simultaneous writes
13 write Slower process: write Simultaneous writes
14 read Simultaneous Reads a
15 read Simultaneous Reads aa a All read the same value
16 Test&Set Variables Test&Set(v) temp = v; v = 1; return (temp); Reset(v) v = 0; v is a binary variable
17 0
18 test&set 0
19 test&set 0 1
20 test&set 1
21 test&set 11
22 1
23 reset 0
24 test&set 0 simultaneous accesses test&set
25 test&set 1 simultaneous accesses 0 test&set 1 1 Faster process
26 Read-Modify-Write Variables RMW(v, f) temp = v; v = f(v); return (temp); function on v
27 0
28 RMW(+5) 0
RMW(+5)
30 RMW(+8) 5
RMW(+8)
32 0 simultaneous accesses RMW(+5) RMW(+8)
33 13 simultaneous accesses RMW(+5) RMW(+8) 8 0 Faster process
34 0 simultaneous accesses RMW(+5) RMW(+8) RMW(+10)
35 23 simultaneous accesses RMW(+5) RMW(+8) RMW(+10) fastest middle slower
36 RMW(v, f) temp = v; v = f(v); return (temp); RMW simulate Test&Set Test&Set(v) temp = v; v = 1; return (temp); Reset(v) v = 0; (f(v) = 1) RMW(v, f) temp = v; v = f(v); return (temp); (f(v) = 0)
37 Mutual Exclusion
38 Process 1 - program V = 1; For (x = 10; x< y; x++) { cout << “hello”; } If (t > 10) then while (x < 100) m = 20; ……
39 Process 1 - program Remainder code Critical Section Remainder Code Process 2 - program Remainder code Critical Section Remainder Code Only one process can enter the critical section Critical Section
40 Process 1 - program Remainder code Critical Section Remainder Code Process 2 - program Remainder code Critical Section Remainder Code Processes may update the same variables in the critical section Critical Section v = v+10;
41 Process 1 Remainder code Entry code Critical Section Exit code Remainder Code Process 2 Remainder code Entry Code Critical Section Exit Code Remainder Code Mutual Exclusion Entry/Exit code guaranty that only one process is in the critical section
42 Process 1 Remainder code Entry code Critical Section Exit code Remainder Code Process 2 Remainder code Entry Code Critical Section Exit Code Remainder Code Mutual Exclusion
43 Process 1 Remainder code Entry code Critical Section Exit code Remainder Code Process 2 Remainder code Entry Code Critical Section Exit Code Remainder Code Mutual Exclusion
44 Process 1 Remainder code Entry code Critical Section Exit code Remainder Code Process 2 Remainder code Entry Code Critical Section Exit Code Remainder Code Mutual Exclusion
45 Process 1 Remainder code Entry code Critical Section Exit code Remainder Code Process 2 Remainder code Entry Code Critical Section Exit Code Remainder Code Mutual Exclusion
46 Process 1 Remainder code Entry code Critical Section Exit code Remainder Code Process 2 Remainder code Entry Code Critical Section Exit Code Remainder Code Mutual Exclusion
47 Mutual Exclusion Critical Exit Remainder Entry
48 Attributes of mutual exclusion algorithms No Deadlock: if some process is in the entry section then some process will enter the critical region No Lockout: if some process is in the entry section then the same process will enter the critical region
49 Mutual Exclusion test&set variables
50 While (test&set(v) = 1) Critical section Reset (v) Entry: Exit:
51 0 test&set entry v
52 1 critical section v 0
53 0 exit v reset
54 0 test&set entry v Two processors entry test&set
55 1 entry v critical section 0 1
56 1 entry v critical section 0 test&set
57 1 entry v critical section 0 1
58 0 entry v exit reset test&set
59 1 critical section v 0
60 0 exit v reset
61 1 entry v critical section test&set Problem: the algorithm doesn’t guaranty no lockout, a process may starve Example: may never enter the critical section
62 1 entry v critical section test&set Example: may never enter the critical section
63 1 entry v critical section test&set Example: may never enter the critical section : faster than
64 Mutual exclusion read-modify-write variables
65 v.first v.last Ticket of current process in critical section Ticket of last process waiting in entry section Shared variable v
66 p = RMW(v, (v.first, v.last +1)) Repeat q = RMW(v,v) Until q.first = p.last Critical section RMV(v, (v.first+1, v.last)) Entry: Exit: (p and q are local variables)
67 v.first 1 v.last 1
68 v.first 1 v.last 1 p.last
69 v.first 1 v.last 2 entry p.last1
70 v.first 1 v.last 2 critical section p.last1
71 v.first 2 v.last 2 exit p.last1
72 v.first 1 v.last 1 Four processes p.last
73 v.first 1 v.last 2 p.last1 entry
74 v.first 1 v.last 2 p.last1 entry
75 v.first 1 v.last 3 p.last1200 entry
76 v.first 1 v.last 3 p.last1200 entry
77 v.first 1 v.last 4 p.last1230 entry
78 v.first 1 v.last 4 p.last1230 entry
79 v.first 1 v.last 5 p.last1234 entry
80 v.first 1 v.last 5 p.last1234 critical section entry
81 v.first 2 v.last 5 p.last1234 entry exit
82 v.first 2 v.last critical section entry
83 v.first 3 v.last entry exit
84 v.first 3 v.last 4 34 entry
85 v.first i v.last-1 i+k i critical section entry i+1 entry The behavior is similar with a queue …… (head) (tail)
86 Good features of algorithm: Guarantees no lockout (any process will eventually enter the critical section) Uses only one shared variable (v)
87 A problem: values can grow unbounded ii+1 … i+k …
88 Solution: a circular queue …… 1 2 n (for n processes) v.firstv.last-1 Only n different values are needed