Download presentation
Presentation is loading. Please wait.
Published byAngel Craig Modified over 9 years ago
1
1 Shared Memory
2
2 processes
3
3 Types of Shared Variables Read/Write Test & Set Read-Modify-Write
4
4 Read/Write Variables Read(v)Write(v,a) return(v); v = a; Or simpler: x = v; v = x; (read v) (write v)
5
5 write 10
6
6 10
7
7 write 10 10 read
8
8 write 10 10 read 10
9
9 write 10 write 20 Simultaneous writes
10
10 write 10 write 20 Slower process Possibility 1 20 Simultaneous writes
11
11 write 10 write 20 Slower process Possibility 2 10 Simultaneous writes
12
12 write In general: write x Simultaneous writes
13
13 write Slower process: write Simultaneous writes
14
14 read Simultaneous Reads a
15
15 read Simultaneous Reads aa a All read the same value
16
16 Test&Set Variables Test&Set(v) temp = v; v = 1; return (temp); Reset(v) v = 0; v is a binary variable
17
17 0
18
18 test&set 0
19
19 test&set 0 1
20
20 test&set 1
21
21 test&set 11
22
22 1
23
23 reset 0
24
24 test&set 0 simultaneous accesses test&set
25
25 test&set 1 simultaneous accesses 0 test&set 1 1 Faster process
26
26 Read-Modify-Write Variables RMW(v, f) temp = v; v = f(v); return (temp); function on v
27
27 0
28
28 RMW(+5) 0
29
29 0 5 RMW(+5)
30
30 RMW(+8) 5
31
31 135 RMW(+8)
32
32 0 simultaneous accesses RMW(+5) RMW(+8)
33
33 13 simultaneous accesses RMW(+5) RMW(+8) 8 0 Faster process
34
34 0 simultaneous accesses RMW(+5) RMW(+8) RMW(+10)
35
35 23 simultaneous accesses RMW(+5) RMW(+8) RMW(+10) 0 8 13 fastest middle slower
36
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
37 Mutual Exclusion
38
38 Process 1 - program V = 1; For (x = 10; x< y; x++) { cout << “hello”; } If (t > 10) then while (x < 100) m = 20; ……
39
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
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
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
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
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
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
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
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
47 Mutual Exclusion Critical Exit Remainder Entry
48
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
49 Mutual Exclusion test&set variables
50
50 While (test&set(v) = 1) Critical section Reset (v) Entry: Exit:
51
51 0 test&set entry v
52
52 1 critical section v 0
53
53 0 exit v reset
54
54 0 test&set entry v Two processors entry test&set
55
55 1 entry v critical section 0 1
56
56 1 entry v critical section 0 test&set
57
57 1 entry v critical section 0 1
58
58 0 entry v exit reset test&set
59
59 1 critical section v 0
60
60 0 exit v reset
61
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
62 1 entry v critical section test&set Example: may never enter the critical section
63
63 1 entry v critical section test&set Example: may never enter the critical section : faster than
64
64 Mutual exclusion read-modify-write variables
65
65 v.first v.last Ticket of current process in critical section Ticket of last process waiting in entry section Shared variable v
66
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
67 v.first 1 v.last 1
68
68 v.first 1 v.last 1 p.last
69
69 v.first 1 v.last 2 entry p.last1
70
70 v.first 1 v.last 2 critical section p.last1
71
71 v.first 2 v.last 2 exit p.last1
72
72 v.first 1 v.last 1 Four processes p.last
73
73 v.first 1 v.last 2 p.last1 entry
74
74 v.first 1 v.last 2 p.last1 entry
75
75 v.first 1 v.last 3 p.last1200 entry
76
76 v.first 1 v.last 3 p.last1200 entry
77
77 v.first 1 v.last 4 p.last1230 entry
78
78 v.first 1 v.last 4 p.last1230 entry
79
79 v.first 1 v.last 5 p.last1234 entry
80
80 v.first 1 v.last 5 p.last1234 critical section entry
81
81 v.first 2 v.last 5 p.last1234 entry exit
82
82 v.first 2 v.last 5 234 critical section entry
83
83 v.first 3 v.last 5 234 entry exit
84
84 v.first 3 v.last 4 34 entry
85
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
86 Good features of algorithm: Guarantees no lockout (any process will eventually enter the critical section) Uses only one shared variable (v)
87
87 A problem: values can grow unbounded ii+1 … i+k …
88
88 Solution: a circular queue …… 1 2 n (for n processes) v.firstv.last-1 Only n different values are needed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.