Prerequisite – Mutual exclusion in distributed systems
Raymond’s tree based algorithm is lock based algorithm for mutual exclusion in a distributed system in which a site is allowed to enter the critical section if it has the token. In this algorithm, all sites are arranged as a directed tree such that the edges of the tree are assigned direction towards the site that holds the token. Site which holds the token is also called root of the tree.
Data structure and Notations:
- Every site Si keeps a FIFO queue, called request_q
This queue stores the requests of all neighbouring sites that have sent a request for the token to site Si but have not yet been sent token. A non-empty request_q at any site indicates that the site has sent a REQUEST message to the root node.
- Every site Si has a local variable, called holder
This variable points to an immediate neighbour node on a directed path to the root node.
- To enter Critical section:
- When a site Si wants to enter the critical section it sends a REQUEST message to the node along the directed path to the root, provided it does not hold the token and its request_q is empty. After sending REQUEST message it add its request to its request_q.
- when a site Sj on the path to the root receives the REQUEST message of site Si, it places the REQUEST in its request_q and sends the REQUEST message along the directed path to the root, if it has not sent any REQUEST message for previously received REQUEST message.
- When the root site Sr( having token) receives the REQUEST message, it sends the token to the requesting site and sets its holder variable to point at that site.
- On receiving the token, Site Sj deletes the top entry from its request_q and sends the token to the site indicated by deleted entry. holder variable of Site Sj is set to point at that site.
After deleting the topmost entry of the request_q, if it is still non-empty Site Sj sends a REQUEST message to the site indicated by holder variable in order to get token back.
- To execute the critical section:
- Site Si executes the critical section if it has received the token and its own entry is at the top of its request_q.
- To release the critical section:
After finishing the execution of the critical section, site Si does the following:
- If its request_q is non-empty, then it deletes the top msot entry from its <request_q and then it sends the token to that site indicated by deleted entry and also its holder variable is set to point at that site.
- After performing above action, if the request_q is still non-empty, then site Si sends a REQUEST message to the site pointed by holder variable in order to get token back
In the worst case, the algorithm requires 2 * ( Longest path length of the tree ) message invocation per critical section entry. If all nodes are arranged in a straight line then the longest path length will be N – 1 and thus the algorithm will require 2 * (N -1) message invocation for critical section entry. However, if all nodes generates equal number of REQUEST messages for the privilege, the algorithm will require approximately 2*N / 3 messages per critical section entry.
Drawbacks of Raymond’s tree based algorithm:
- can cause starvation: Raymond’s algorithm uses greedy strategy as a site can executes the critical section on receiving the token even when its request is not on the top of the request queue. This affect the fairness of the algorithm and thus can cause in starvation.
- Synchronization delay is (T * log N )/ 2, because the average distance between two sites to successively execute the criticsl section is (Log N)/2. Here T is maximum message transmission time.
- In heavy load conditions, the synchronization delay become T beacuse a site executes the critical section every time the token is transferred.
- The message complexity of this algorithm is O(log N) as the average distance between any two nodes in a tree with N nodes is log N
- Deadlock is impossible
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.