1 shared memory. 2 processes 3 types of shared variables read/write test & set read-modify-write
TRANSCRIPT
1
Shared Memory
2
Shared Memory
1p
3p
2p
4p
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
1p write 10
6
1p write 1010
7
1p write 1010
2pread
8
1p write 1010
2pread
10
9
1p write 10 2pwrite 20
Simultaneous writes
10
1p write 10 2pwrite 20
Slower process
Possibility 1
20
Simultaneous writes
11
1p write 10 2pwrite 20Slower process
Possibility 2
10
Simultaneous writes
12
1p write 2pwrite
In general:
1a2a
3pwrite 3a
ipwrite ia
x
Simultaneous writes
13
1p write 2pwrite
Slower process:
1a2a
3pwrite 3a
ipwrite ia
ka
kpSimultaneous writes
14
1p read 2pread
Simultaneous Reads
a
15
1p read 2pread
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
1p test&set0
19
1p test&set0 1
20
2ptest&set
1
21
2ptest&set
1 1
22
1
23
3p reset0
24
1p test&set0
2ptest&set
simultaneous accesses
3ptest&set
25
1p test&set1
simultaneous accesses
0
2ptest&set
3ptest&set
11
Faster process
26
Read-Modify-Write Variables
RMW(v, f)temp = v;v = f(v);return (temp);
function on v
27
0
28
1p RMW(+5)0
29
1p0 5
RMW(+5)
30
2pRMW(+8)
5
31
2p
13 5
RMW(+8)
32
1p0
2p
simultaneous accesses
RMW(+5) RMW(+8)
33
1p13
2p
simultaneous accesses
RMW(+5) RMW(+8)
8 0
Faster process
34
1p0
2p
simultaneous accesses
RMW(+5) RMW(+8)
2pRMW(+10)
35
1p23
2p
simultaneous accesses
RMW(+5) RMW(+8)
2pRMW(+10)
08 13
fastestmiddle
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; v = v+10;
41
Process 1
Remainder codeEntry codeCritical SectionExit codeRemainder Code
Process 2
Remainder codeEntry CodeCritical SectionExit CodeRemainder Code
Mutual Exclusion
Entry/Exit code guaranty that onlyone process is in the critical section
42
Process 1
Remainder codeEntry codeCritical SectionExit codeRemainder Code
Process 2
Remainder codeEntry CodeCritical SectionExit CodeRemainder Code
Mutual Exclusion
43
Process 1
Remainder codeEntry codeCritical SectionExit codeRemainder Code
Process 2
Remainder codeEntry CodeCritical SectionExit CodeRemainder Code
Mutual Exclusion
44
Process 1
Remainder codeEntry codeCritical SectionExit codeRemainder Code
Process 2
Remainder codeEntry CodeCritical SectionExit CodeRemainder Code
Mutual Exclusion
45
Process 1
Remainder codeEntry codeCritical SectionExit codeRemainder Code
Process 2
Remainder codeEntry CodeCritical SectionExit CodeRemainder Code
Mutual Exclusion
46
Process 1
Remainder codeEntry codeCritical SectionExit codeRemainder Code
Process 2
Remainder codeEntry CodeCritical SectionExit CodeRemainder 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
1p test&setentry
v
52
1
1pcritical section
v0
53
0
1pexit
vreset
54
0
1p test&setentry
v
Two processors
2pentry
test&set
55
1
1pentry
v 2p
critical section
01
56
1
1pentry
v 2p
critical section
0test&set
57
1
1pentry
v 2p
critical section
01
58
0
1pentry
v 2pexit
resettest&set
59
1
1p
criticalsection
v0
60
0
1pexit
vreset
61
1
1pentry
v 2p
critical section
test&set
Problem: the algorithm doesn’t guaranty no lockout, a process may starve
Example: may never enter the critical section1p
62
1
1pentry
v
3p
critical section
test&set
Example: may never enter the critical section1p
63
1
1pentry
v 4p
critical section
test&set
Example: may never enter the critical section1p
432 ,, ppp : faster than 1p
64
Mutual exclusion
read-modify-write variables
65
v.first
v.last
Ticket of current process in critical section
Ticket of last processwaiting 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.last1
68
v.first 1
v.last1
p.last 1p
69
v.first 1
v.last2
entry
p.last 1 1p
70
v.first 1
v.last2
critical section
p.last 1 1p
71
v.first 2
v.last2
exit
p.last 1 1p
72
v.first 1
v.last1
Four processes
p.last 1p 2p 3p 4p
73
v.first 1
v.last2
p.last 1 1p 2p 3p 4pentry
74
v.first 1
v.last2
p.last 1 1p 2p 3p 4pentry
75
v.first 1
v.last3
p.last 1 1p 2 2p 0 3p 0 4pentry entry
76
v.first 1
v.last3
p.last 1 1p 2 2p 0 3p 0 4pentry entry
77
v.first 1
v.last4
p.last 1 1p 2 2p 3 3p 0 4pentry entry entry
78
v.first 1
v.last4
p.last 1 1p 2 2p 3 3p 0 4pentry entry entry
79
v.first 1
v.last5
p.last 1 1p 2 2p 3 3p 4 4pentry entry entry entry
80
v.first 1
v.last5
p.last 1 1p 2 2p 3 3p 4 4p
criticalsection entry entry entry
81
v.first 2
v.last5
p.last 1 1p 2 2p 3 3p 4 4pentry entry entryexit
82
v.first 2
v.last5
2 2p 3 3p 4 4p
criticalsection entry entry
83
v.first 3
v.last5
2 2p 3 3p 4 4pentry entryexit
84
v.first 3
v.last4
3 3p 4 4pentry entry
85
v.first i
v.last-1i+k
i 1ip
i+k 1kip
criticalsection entry
i+1 2ip
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
i i+1 … i+k …
88
Solution: a circular queue
……1 2 n
(for n processes)
v.first v.last-1
Only n different values are needed