operating systems, 2011, danny hendler & amnon meisels 1 distributed mutual exclusion ...
Post on 21-Dec-2015
215 views
TRANSCRIPT
![Page 1: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/1.jpg)
Operating Systems, 2011, Danny Hendler & Amnon Meisels 1
Distributed Mutual Exclusion
Introduction
Ricart and Agrawala's algorithm
Raymond's algorithm
This presentation is based on the book: “Distributed operating-systems & algorithms” by Randy Chow and Theodore Johnson
![Page 2: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/2.jpg)
2Operating Systems, 2011, Danny Hendler & Amnon Meisels
Distributed mutual exclusion required (e.g.) for transaction processing on replicated data
We assume there are no failureso Processors do not failo Communication links do not fail
It is easy to implement mutual exclusion using totally-ordered timestampso The first algorithm we show may use Lamport's timestamps
Distributed mutual exclusion: introduction
![Page 3: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/3.jpg)
3Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: high-level ideas
When you want to enter your CSo Record your timestampo Ask everyone else whether they “permit”
When asked for a permissiono Halt response if in CSo Halt response if in entry code with a smaller timestamp
(we need total order between timestamps)o Otherwise, “permit”
Upon exit from CSo Send halted responses (if any)
![Page 4: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/4.jpg)
4Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: data-structures
Per processor variables
timestamp current_timeTimestamp my_timestampinteger reply_countboolean isRequestingboolean reply_deferred[M]
Processor’s current Lamport timestamp
![Page 5: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/5.jpg)
5Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: data-structures
Per processor variables
timestamp current_timeTimestamp my_timestampinteger reply_countboolean isRequestingboolean reply_deferred[M]
The timestamp of the processor’s current request
![Page 6: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/6.jpg)
6Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: data-structures
Per processor variables
timestamp current_timeTimestamp my_timestampinteger reply_countboolean isRequestingboolean reply_deferred[M] The number of permissions
that the processor still need to collect before entering the CS
![Page 7: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/7.jpg)
7Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: data-structures
Per processor variables
timestamp current_timeTimestamp my_timestampinteger reply_countboolean isRequestingboolean reply_deferred[M]
True iff this processor is requesting or using the CS
![Page 8: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/8.jpg)
8Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: data-structures
Per processor variables
timestamp current_timeTimestamp my_timestampinteger reply_countboolean isRequestingboolean reply_deferred[M]
Entry j is true iff this processor deferred replying to processor j’s request
![Page 9: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/9.jpg)
9Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: entry-codeRequest_CS:1 my_timstamp current_time2 isRequesting TRUE3 Reply_count M-14 for every other processor j5 send(REMOTE_REQUEST; my_timestamp)6 wait until reply_count = 0
Set the (Lamport) timestamp of my request
![Page 10: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/10.jpg)
10Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: entry-codeRequest_CS:1 my_timstamp current_time2 isRequesting TRUE3 Reply_count M-14 for every other processor j5 send(REMOTE_REQUEST; my_timestamp)6 wait until reply_count = 0
Mark that this processor is requesting entry to CS
![Page 11: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/11.jpg)
11Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: entry-codeRequest_CS:1 my_timstamp current_time2 isRequesting TRUE3 Reply_count M-14 for every other processor j5 send(REMOTE_REQUEST; my_timestamp)6 wait until reply_count = 0
Need to receive replies from all other processors
![Page 12: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/12.jpg)
12Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: entry-codeRequest_CS:1 my_timstamp current_time2 isRequesting TRUE3 Reply_count M-14 for every other processor j5 send(REMOTE_REQUEST; my_timestamp)6 wait until reply_count = 0
Request permission from all other processors
![Page 13: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/13.jpg)
13Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: entry codeRequest_CS:1 my_timstamp current_time2 isRequesting TRUE3 Reply_count M-14 for every other processor j5 send(REMOTE_REQUEST; my_timestamp)6 wait until reply_count = 0
When all other processors reply – may enter the CS
![Page 14: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/14.jpg)
14Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: monitoringCS_monitoring:Wait until a REMOTE_REUQUEST or REPLY message is received
REMOTE_REQUEST(sender; request_time)1. Let j be the sender of the REMOTE_REQUEST message2. if (not is_requesting or my_timestamp > request_time)3. send(j, REPLY)4. else5. reply_deferred[j]=TRUE
REPLY4. reply_count reply_count-1
Listener thread to respond to protocol messages at all times
![Page 15: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/15.jpg)
15Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: monitoring
Upon receipt of remote request
CS_monitoring:Wait until a REMOTE_REUQUEST or REPLY message is received
REMOTE_REQUEST(sender; request_time)1. Let j be the sender of the REMOTE_REQUEST message2. if (not is_requesting or my_timestamp > request_time)3. send(j, REPLY)4. else5. reply_deferred[j]=TRUE
REPLY4. reply_count reply_count-1
![Page 16: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/16.jpg)
16Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: monitoring
If should grant processor j’th request
CS_monitoring:Wait until a REMOTE_REUQUEST or REPLY message is received
REMOTE_REQUEST(sender; request_time)1. Let j be the sender of the REMOTE_REQUEST message2. if (not is_requesting or my_timestamp > request_time)3. send(j, REPLY)4. else5. reply_deferred[j]=TRUE
REPLY4. reply_count reply_count-1
![Page 17: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/17.jpg)
17Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: monitoring
Send reply to processor j
CS_monitoring:Wait until a REMOTE_REUQUEST or REPLY message is received
REMOTE_REQUEST(sender; request_time)1. Let j be the sender of the REMOTE_REQUEST message2. if (not is_requesting or my_timestamp > request_time)3. send(j, REPLY)4. else5. reply_deferred[j]=TRUE
REPLY4. reply_count reply_count-1
![Page 18: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/18.jpg)
18Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: monitoring
Otherwise, defer replying to this request
CS_monitoring:Wait until a REMOTE_REUQUEST or REPLY message is received
REMOTE_REQUEST(sender; request_time)1. Let j be the sender of the REMOTE_REQUEST message2. if (not is_requesting or my_timestamp > request_time)3. send(j, REPLY)4. else5. reply_deferred[j]=TRUE
REPLY4. reply_count reply_count-1
![Page 19: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/19.jpg)
19Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: monitoring
Upon receiving a reply, decrement reply_count
CS_monitoring:Wait until a REMOTE_REUQUEST or REPLY message is received
REMOTE_REQUEST(sender; request_time)1. Let j be the sender of the REMOTE_REQUEST message2. if (not is_requesting or my_timestamp > request_time)3. send(j, REPLY)4. else5. reply_deferred[j]=TRUE
REPLY4. reply_count reply_count-1
![Page 20: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/20.jpg)
20Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: exit sectionRelease_CS_monitoring:1. is_requesting false2. For j=1 through M (other than this processor's ID)3. if reply_deferred[i]=TRUE4. send(j, REPLY)5. reply_deferred[j]=FALSE
No longer requesting CS
![Page 21: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/21.jpg)
21Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: exit sectionRelease_CS_monitoring:1. is_requesting false2. For j=1 through M (other than this processor's ID)3. if reply_deferred[i]=TRUE4. send(j, REPLY)5. reply_deferred[j]=FALSE
For each processor awaiting a reply from this processor, send reply and mark that there are no more deferred replies.
![Page 22: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/22.jpg)
22Operating Systems, 2011, Danny Hendler & Amnon Meisels
Ricart and Agrawal's algorithm: comments
What is the number of messages required for each
passage through the critical section?2(M-1) messages.
Let's see a more message-efficient algorithm…
![Page 23: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/23.jpg)
23Operating Systems, 2011, Danny Hendler & Amnon Meisels
Distributed Mutual Exclusion
Introduction
Ricart and Agrawala's algorithm
Raymond's algorithm
![Page 24: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/24.jpg)
24Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: high-level ideas
There is a single token in the systemo Only the holder of the token may enter the CS
Processors communicate by using a static tree structureo Requests for the token are sento The token itself is sent when available and requested
Processors maintain FIFO request queues to prevent starvation
If processors request entry “one at a time” – at most a logarithmic number of messages per entryo When there are many simultaneous requests, the number of messages
per entry typically decreases
![Page 25: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/25.jpg)
25Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: high-level ideas (cont'd) Algorithm invariant: tree is always oriented towards token
holder
Token holder
![Page 26: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/26.jpg)
26Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: data-structures
Per processor variables
Boolean token_holderBoolean inCScurrent_dirrequests_queue
True iff this processor currently holds the token
![Page 27: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/27.jpg)
27Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: data-structures
Per processor variables
Boolean token_holderBoolean inCScurrent_dirrequests_queue True iff this processor is
currently in the critical section
![Page 28: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/28.jpg)
28Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: data-structures
Per processor variables
Boolean token_holderBoolean inCScurrent_dirrequests_queue
The neighbor that is in the direction of the token (or self if this processor holds the token)
![Page 29: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/29.jpg)
29Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: data-structures
Per processor variables
Boolean token_holderBoolean inCScurrent_dirrequests_queue
FIFO queue holding IDs of neighbors from which requests for the token arrived (may also contain self)
![Page 30: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/30.jpg)
30Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: entry and exit codeRequest_CS:1 If not token_holder2 if requests_queue.isEmpty( )3 send(current_dir, REQUEST)4 requests_queue.enqueue(self)5 wait until token_holder is true6 inCS true
Release_CS:7. inCS false8. If not requests_queue.isEmpty( )9. current_dir requests_queue.dequeue( )10. send(current_dir, TOKEN)11. token_holder false12. if not requests_queue.isEmpty( )13. send(current_dir, REQUEST)
If this processor currently holds the token it immediately enters CS. Otherwise…
![Page 31: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/31.jpg)
31Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: entry and exit codeRequest_CS:1 If not token_holder2 if requests_queue.isEmpty( )3 send(current_dir, REQUEST)4 requests_queue.enqueue(self)5 wait until token_holder is true6 inCS true
Release_CS:7. inCS false8. If not requests_queue.isEmpty( )9. current_dir requests_queue.dequeue( )10. send(current_dir, TOKEN)11. token_holder false12. if not requests_queue.isEmpty( )13. send(current_dir, REQUEST)
If requests queue is empty, send a request for the token. (If queue is non-empty, a request for the token was already sent.)
![Page 32: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/32.jpg)
32Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: entry and exit codeRequest_CS:1 If not token_holder2 if requests_queue.isEmpty( )3 send(current_dir, REQUEST)4 requests_queue.enqueue(self)5 wait until token_holder is true6 inCS true
Release_CS:7. inCS false8. If not requests_queue.isEmpty( )9. current_dir requests_queue.dequeue( )10. send(current_dir, TOKEN)11. token_holder false12. if not requests_queue.isEmpty( )13. send(current_dir, REQUEST)
Enqueue ‘self’ to requests queue since this request is on behalf of this processor
![Page 33: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/33.jpg)
33Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: entry and exit codeRequest_CS:1 If not token_holder2 if requests_queue.isEmpty( )3 send(current_dir, REQUEST)4 requests_queue.enqueue(self)5 wait until token_holder is true6 inCS true
Release_CS:7. inCS false8. If not requests_queue.isEmpty( )9. current_dir requests_queue.dequeue( )10. send(current_dir, TOKEN)11. token_holder false12. if not requests_queue.isEmpty( )13. send(current_dir, REQUEST)
When token_holder is set, this processor has the token and may enter the CS
![Page 34: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/34.jpg)
34Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: entry and exit codeRequest_CS:1 If not token_holder2 if requests_queue.isEmpty( )3 send(current_dir, REQUEST)4 requests_queue.enqueue(self)5 wait until token_holder is true6 inCS true
Release_CS:7. inCS false8. If not requests_queue.isEmpty( )9. current_dir requests_queue.dequeue( )10. send(current_dir, TOKEN)11. token_holder false12. if not requests_queue.isEmpty( )13. send(current_dir, REQUEST)
No longer in critical section
![Page 35: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/35.jpg)
35Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: entry and exit codeRequest_CS:1 If not token_holder2 if requests_queue.isEmpty( )3 send(current_dir, REQUEST)4 requests_queue.enqueue(self)5 wait until token_holder is true6 inCS true
Release_CS:7. inCS false8. If not requests_queue.isEmpty( )9. current_dir requests_queue.dequeue( )10. send(current_dir, TOKEN)11. token_holder false12. if not requests_queue.isEmpty( )13. send(current_dir, REQUEST)
If requests are waiting…
![Page 36: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/36.jpg)
36Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: entry and exit codeRequest_CS:1 If not token_holder2 if requests_queue.isEmpty( )3 send(current_dir, REQUEST)4 requests_queue.enqueue(self)5 wait until token_holder is true6 inCS true
Release_CS:7. inCS false8. If not requests_queue.isEmpty( )9. current_dir requests_queue.dequeue( )10. send(current_dir, TOKEN)11. token_holder false12. if not requests_queue.isEmpty( )13. send(current_dir, REQUEST)
Dequeue the next hop for the earliest request and send the TOKEN to it. Also, update orientation of the token.
![Page 37: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/37.jpg)
37Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: entry and exit codeRequest_CS:1 If not token_holder2 if requests_queue.isEmpty( )3 send(current_dir, REQUEST)4 requests_queue.enqueue(self)5 wait until token_holder is true6 inCS true
Release_CS:7. inCS false8. If not requests_queue.isEmpty( )9. current_dir requests_queue.dequeue( )10. send(current_dir, TOKEN)11. token_holder false12. if not requests_queue.isEmpty( )13. send(current_dir, REQUEST) This processor no longer holds token
![Page 38: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/38.jpg)
38Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: entry and exit codeRequest_CS:1 If not token_holder2 if requests_queue.isEmpty( )3 send(current_dir, REQUEST)4 requests_queue.enqueue(self)5 wait until token_holder is true6 inCS true
Release_CS:7. inCS false8. If not requests_queue.isEmpty( )9. current_dir requests_queue.dequeue( )10. send(current_dir, TOKEN)11. token_holder false12. if not requests_queue.isEmpty( )13. send(current_dir, REQUEST)
If there are more requests in this processor’s queue, send another request for the token
![Page 39: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/39.jpg)
39Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
Listener thread to respond to protocol messages at all times
![Page 40: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/40.jpg)
40Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
Upon a request.If current processor holds token…
![Page 41: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/41.jpg)
41Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
If current processor in CS then request must wait, enqueue the direction of requesting processor
![Page 42: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/42.jpg)
42Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
Otherwise current processor holds the token but is not in CS, hence requests queue is empty.
Send token to where the request came from, mark that current processor no longer holds token, and the new orientation of the otken…
![Page 43: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/43.jpg)
43Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
Otherwise current processor does not hold the token…
![Page 44: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/44.jpg)
44Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
If requests queue is empty, send request in the direction of the token…
![Page 45: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/45.jpg)
45Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
Enqueue the direction of this request…
![Page 46: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/46.jpg)
46Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
Upon the arrival of the token…
![Page 47: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/47.jpg)
47Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
Dequeue oldest request and set new orientation of the token to its direction
![Page 48: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/48.jpg)
48Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
If request was by this processor, mark that it currently has the token and may enter the CS
![Page 49: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/49.jpg)
49Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
Otherwise, send the token in the direction of the request
![Page 50: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/50.jpg)
50Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: monitoringMonitor_CS:1 while (true)2 wait for a REQUEST or a TOKEN message REQUEST3. if token_holder4. if inCS5. requests_queue.enqueue(sender)6. else7. current_dir sender8. send(current_dir, TOKEN)9. token_holder false10. else11. if requests_queue.isEmpty()12. send(current_dir,REQUEST)13. requests_queue.enqueue(sender) TOKEN14. current_dir requests_queue.dequeue( )15. if current_dir = self16. token_holder true17. else18. send(current_dir, TOKEN)19. if not requests_queue.isEmpty( )20. send(current_dir, REQUEST)
If the queue is non-empty, send another request for the token
![Page 51: Operating Systems, 2011, Danny Hendler & Amnon Meisels 1 Distributed Mutual Exclusion Introduction Ricart and Agrawala's algorithm Raymond's algorithm](https://reader030.vdocuments.us/reader030/viewer/2022032521/56649d585503460f94a37e38/html5/thumbnails/51.jpg)
51Operating Systems, 2011, Danny Hendler & Amnon Meisels
Raymond's algorithm: execution scenario