Download presentation
Presentation is loading. Please wait.
1
Midterm Exam 06: Exercise 2 Atomic Shared Memory
2
Midterm Exam: Exercise 2 – Atomic shared memorySlide 2 Atomic register Every failed (write) operation appears to be either complete or not to have been invoked at allEvery failed (write) operation appears to be either complete or not to have been invoked at allAnd Every complete operation appears to be executed at some instant between its invocation and reply time eventsEvery complete operation appears to be executed at some instant between its invocation and reply time events In other words, atomic register is:In other words, atomic register is: Regular (READ returns the latest value written, or one of the values written concurrently), and READ rd’ that follows some (complete) read rd does not return an older value (than rd)
3
Midterm Exam: Exercise 2 – Atomic shared memorySlide 3 Non-Atomic Execution 1 P2 P1 W(5) W(6) R 1 () -> 5R 2 () -> 0R 3 () -> 25
4
Midterm Exam: Exercise 2 – Atomic shared memorySlide 4 Non-Atomic Execution 2 P2 P1 W(5) W(6) R 1 () -> 5R 2 () -> 6R 3 () -> 5
5
Midterm Exam: Exercise 2 – Atomic shared memorySlide 5 Non-Atomic Execution 3 P2 P1 W(5) R() -> 6 P2 P1 W(5) R() -> 5 W(6) crash
6
Midterm Exam: Exercise 2 – Atomic shared memorySlide 6 Atomic Execution 1 P2 P1 W(5) W(6) R 1 () -> 5R 2 () -> 5R 3 () -> 5
7
Midterm Exam: Exercise 2 – Atomic shared memorySlide 7 Atomic Execution 2 P2 P1 W(5) W(6) R 1 () -> 5R 2 () -> 6R 3 () -> 6
8
Midterm Exam: Exercise 2 – Atomic shared memorySlide 8 P2 P1 W(5) R() -> 5 W(6) crash Atomic Execution 3
9
Midterm Exam: Exercise 2 – Atomic shared memorySlide 9 P2 P1 W(5) R() -> 5 P2 P1 W(5) R() -> 6 W(6) crash Atomic Execution 4
10
Midterm Exam: Exercise 2 – Atomic shared memorySlide 10 Best case complexity We build algorithms for the worst case (or unlucky) situationsWe build algorithms for the worst case (or unlucky) situations Asynchrony Concurrency Many failures However, very frequently situation is not that bad (lucky executions)However, very frequently situation is not that bad (lucky executions) Synchrony No concurrency Few failures (or none at all) Practical algorithms should take advantage of the lucky executionsPractical algorithms should take advantage of the lucky executions
11
Midterm Exam: Exercise 2 – Atomic shared memorySlide 11 Exercise 2 Give a 1-writer n-reader atomic register implementation (n=5, majority of processes is correct) in whichGive a 1-writer n-reader atomic register implementation (n=5, majority of processes is correct) in which L2: all read/write operations* complete in at most 2 round- trips In every round-trip, a client (writer or reader) sends a message to all processes and awaits response from some subset of processes L1: all lucky read/write operations* should complete in a single round-trip A read/write operation op is lucky if: The system is synchronous: messages among correct processes delivered within the time (known to all correct processes) op is not concurrent with any write operation At most one process is faulty *invoked by a correct client
12
Midterm Exam: Exercise 2 – Atomic shared memorySlide 12 The majority algorithm [ABD95] All reads and writes complete in a single round-tripAll reads and writes complete in a single round-trip A client sends a message to all processes and waits for response from a majority However, this algorithm implements only regular register (not atomic)However, this algorithm implements only regular register (not atomic) To make the algorithm atomic:To make the algorithm atomic: readers impose a value with a highest timestamp to a majority of processes (requires a second round-trip)
13
Midterm Exam: Exercise 2 – Atomic shared memorySlide 13 Lucky operations If the operation is lucky, the client will be able to receive (at least) 4 (out of 5) responsesIf the operation is lucky, the client will be able to receive (at least) 4 (out of 5) responses timer=2 t t+2 t+
14
Midterm Exam: Exercise 2 – Atomic shared memorySlide 14 Solution In the following slides we modify the majority algorithm of [ABD95]In the following slides we modify the majority algorithm of [ABD95] [ABD95] is given in slides 30-32, Regular Register Algorithms lecture notes – in Shared Memory part 2
15
Midterm Exam: Exercise 2 – Atomic shared memorySlide 15 Algorithm - Write() Write(v) at p1 (the writer)Write(v) at p1 (the writer) ts1++ trigger(timer=2 ) send [W,ts1,v] to all when receive [W,ts1,ack] from majority Wait for expiration of timer If received 4 acks then Return ok else Send [W2,ts1,v] to all when receive [W2,ts1,ack] from majority Return ok At piAt pi when receive [W,ts1, v] from p1 If ts1 > sni then vi := v sni := ts1 send [W,ts1,ack] to p1 when receive [W2,ts1, v] from p1 If ts1 > sni2 then vi2:= v sni2 := ts1 send [W2,ts1,ack] to p1
16
Midterm Exam: Exercise 2 – Atomic shared memorySlide 16 How the (lucky) write works p1(writer) p2 p3 p4 p5 write(v) ts1++ %ts1=1 trigger(timer=2 ) send [W,ts1,v] to all vi,sniv2i,sn2i vi,sniv2i,sn2i vi,sniv2i,sn2i vi,sniv2i,sn2i v0,0v0,0 v0,0 v0,0 v0,0 v0,0 v0,0 v0,0 v0,0 v0,0 v1,1 v1,1 v1,1 v1,1 ACK
17
Midterm Exam: Exercise 2 – Atomic shared memorySlide 17 How the (unlucky) write works p1(writer) p2 p3 p4 p5 write(v) ts1++ %ts1=1 trigger(timer=2 ) send [W,ts1,v] to all vi,sniv2i,sn2i vi,sniv2i,sn2i vi,sniv2i,sn2i vi,sniv2i,sn2i v0,0v0,0 v0,0 v0,0 v0,0 v0,0 v0,0 v0,0v0,0 v1,1 v1,1 v1,1 ACK v0,0 send[W2,ts1,v] to all wait for majority of acks v1,1 v1,1 v1,1
18
Midterm Exam: Exercise 2 – Atomic shared memorySlide 18 Algorithm - Read() Read() at piRead() at pi rsi++ trigger(timer=2 ) send [R,rsi] to all when receive [R,rsi,snj,vj] from majority v := vj with the largest snj Return v At piAt pi when receive [R,rsj] from pj send [R,rsj,sni,vi] to pj
19
Midterm Exam: Exercise 2 – Atomic shared memorySlide 19 Algorithm - Read() Read() at piRead() at pi rsi++ trigger(timer=2 ) send [R,rsi] to all when receive [R,rsi,snj,vj,sn2j,v2j] from majority Wait for expiration of timer v := vj with the largest snj Return v At piAt pi when receive [R,rsj] from pj send [R,rsj,sni,vi,sn2i,v2i] to pj
20
Midterm Exam: Exercise 2 – Atomic shared memorySlide 20 Algorithm - Read() Read() at piRead() at pi rsi++ trigger(timer=2 ) send [R,rsi] to all when receive [R,rsi,snj,vj,sn2j,v2j] from majority Wait for expiration of timer v := vj or v2j with the largest snj or sn2j If v is some v2j or there are 3 responses where vj=v and snj=snMAX then Return v else Send [W,ts1,v] to all when receive [W,ts1,ack] from majority Return ok At piAt pi when receive [R,rsj] from pj send [R,rsj,sni,vi,sn2i,v2i] to pj
21
Midterm Exam: Exercise 2 – Atomic shared memorySlide 21 How the lucky read works p2 p3 p4 p5 p1 Following the lucky write p5 knows that a value with the largest timestamp has already been imposed to a majority (in our case p2,p3 and p4)
22
Midterm Exam: Exercise 2 – Atomic shared memorySlide 22 How the lucky read works p2 p3 p4 p5 p1 Following the unlucky (2 round-trip) write p5 saw a (at least one) « green » value with the largest timestamp: hence, this value has already been imposed to a majority in the 1st round-trip of the write
23
Midterm Exam: Exercise 2 – Atomic shared memorySlide 23 Unlucky read Must impose a value with the largest timestamp to a majority of processesMust impose a value with the largest timestamp to a majority of processes If v is some v2j or there are 3 responses where vj=v and snj=snMAX then Return v else Send [W,ts1,v] to all when receive [W,ts1,ack] from majority Return ok W not W2!W not W2! Readers impose a value on « yellow » not « green » variables Only the writer writes into the « green » variables (v2i,sn2i)
24
Midterm Exam: Exercise 2 – Atomic shared memorySlide 24 An offline exercise Try to rigorously prove correctness of this algorithmTry to rigorously prove correctness of this algorithm Proving correctness may appear on the final examProving correctness may appear on the final exam
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.