Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Data Structures and Algorithms | Set 7
Next article icon

Raymond’s tree based algorithm

Last Updated : 09 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Algorithm:  

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 most 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

Message Complexity: 
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.

Performance:

Synchronization delay is (T * log N )/ 2, because the average distance between two sites to successively execute the critical section is (Log N)/2. Here T is maximum message transmission time.
In heavy load conditions, the synchronization delay become T because 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

The Code for visualising Raymond Tree Based Algorithm is given below:

C++




//Contributed by Anuj Kumar Sahu
#include <stdio.h>
#include <stdlib.h>
// Define Structure for Node
struct node {
    int id;
    int holderval;
    struct node* l;
    struct node* r;
    int require[20];
};
typedef struct node node;
//Function for inorder Traversal
void TraversalInorder(node* roonodeT)
{
 
    if (roonodeT == NULL) {
        return;
    }
    TraversalInorder(roonodeT->l);
    printf("%d  %d\n", roonodeT->id, roonodeT->holderval);
    TraversalInorder(roonodeT->r);
}
 
void token(node* roonodeT, int NodeCS)
{
 
    if (NodeCS == roonodeT->id) {
        printf("%d\n", roonodeT->id);
        roonodeT->holderval = roonodeT->id;
        return;
    }
    else if (NodeCS < roonodeT->id) {
        roonodeT->holderval = (roonodeT->l)->id;
        printf("%d->", roonodeT->id);
        roonodeT = roonodeT->l;
 
        token(roonodeT, NodeCS);
    }
    else if (NodeCS > roonodeT->id) {
        roonodeT->holderval = (roonodeT->r)->id;
        printf("%d->", roonodeT->id);
        roonodeT = roonodeT->r;
        token(roonodeT, NodeCS);
    }
}
 
// Function to Insert Node
void NodeTinsert(node* nodeNew, node* roonodeT)
{
    if (nodeNew->id > roonodeT->id) {
        if (roonodeT->r == NULL) {
            roonodeT->r = nodeNew;
            nodeNew->holderval = roonodeT->id;
        }
        else
            NodeTinsert(nodeNew, roonodeT->r);
    }
 
    if (nodeNew->id < roonodeT->id) {
        if (roonodeT->l == NULL) {
            roonodeT->l = nodeNew;
            nodeNew->holderval = roonodeT->id;
        }
        else
            NodeTinsert(nodeNew, roonodeT->l);
    }
}
 
// Driver Function
int main()
{
    node *roonodeT = NULL, *nodeNew = NULL, *node1;
    int i;
    // Value to be given below
    int n = 5;
    int nodeT = 3;
    int idValue;
    int arr[5] = { 1, 2, 3, 4, 5 };
    int NodeCS, option;
    roonodeT = (struct node*)malloc(sizeof(node));
    node1 = (struct node*)malloc(sizeof(node));
    roonodeT->id = nodeT;
    roonodeT->r = roonodeT->l = NULL;
    roonodeT->holderval = roonodeT->id;
    for (i = 0; i < n; i++) {
        idValue = arr[i];
        nodeNew = (struct node*)malloc(sizeof(node));
        nodeNew->l = nodeNew->r = NULL;
        if (i == nodeT)
            i++;
        nodeNew->id = idValue;
 
        NodeTinsert(nodeNew, roonodeT);
    }
    TraversalInorder(roonodeT);
    NodeCS = 2;
    token(roonodeT, NodeCS);
    return -1;
}
 
 
Output
1  3 2  1 3  3 4  3 3->1->2


Next Article
Data Structures and Algorithms | Set 7

I

ihritik
Improve
Article Tags :
  • Operating Systems
  • Distributed System

Similar Reads

  • Data Structures and Algorithms | Set 7
    Following questions have been asked in GATE CS 2006 exam. 1. In a binary max heap containing n numbers, the smallest element can be found in time (GATE CS 2006) (A) 0(n) (B) O(logn) (C) 0(loglogn) (D) 0(1) Answer (A) In a max heap, the smallest element is always present at a leaf node. So we need to
    3 min read
  • Data Structures and Algorithms | Set 4
    Following questions have been asked in GATE CS exam. 1. Consider the following C program segment C/C++ Code struct CellNode { struct CelINode *leftchild; int element; struct CelINode *rightChild; } int Dosomething(struct CelINode *ptr) { int value = 0; if (ptr != NULL) { if (ptr->leftChild != NUL
    3 min read
  • Data Structures and Algorithms | Set 13
    Following questions have been asked in GATE CS 2002 exam 1. The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is: a) n/2 b) (n-1)/3 c) (n-1)/2 d) (2n+1)/3 Answer(d) Let L be the number of leaf nodes and I be the number of internal nodes, then following relat
    3 min read
  • Data Structures and Algorithms | Set 10
    Following questions have been asked in GATE CS 2007 exam. 1. The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum number of nodes in a binary tree of height h is: (A) 2^h -1 (B) 2^(h-1) - 1 (C) 2^(h+1) -1 (D) 2*(h+1) Answer (C) Maximum number of nodes will
    4 min read
  • Data Structures and Algorithms | Set 12
    Following questions have been asked in GATE CS 2007 exam. 1. Consider the following C program segment where CellNode represents a node in a binary tree: C/C++ Code struct CellNode { struct CellNOde *leftChild; int element; struct CellNode *rightChild; }; int GetValue(struct CellNode *ptr) { int valu
    3 min read
  • Data Structures and Algorithms | Set 23
    Following questions have been asked in GATE CS 2005 exam. 1. Which one of the following is a key factor for preferring B-trees to binary search trees for indexing database relations? (a) Database relations have a large number of records (b) Database relations are sorted on the primary key (c) B-tree
    3 min read
  • Data Structures and Algorithms | Set 32
    Following questions have been asked in GATE CS 2014 exam. 1) Let G be a graph with n vertices and m edges. What is the tightest upper bound on the running time on Depth First Search of G? Assume that the graph is represented using adjacency matrix. (A) O(n) (B) O(m+n) (C) O(n2) (D) O(mn) Answer: (C)
    4 min read
  • Data Structures and Algorithms | Set 17
    Following questions have been asked in GATE CS 2006 exam. 1. An implementation of a queue Q, using two stacks S1 and S2, is given below: void insert(Q, x) { push (S1, x); } void delete(Q){ if(stack-empty(S2)) then if(stack-empty(S1)) then { print(“Q is empty”); return; } else while (!(stack-empty(S1
    2 min read
  • Data Structures and Algorithms | Set 15
    Following questions have been asked in GATE CS 2008 exam. 1. The most efficient algorithm for finding the number of connected components in an undirected graph on n vertices and m edges has time complexity. (A) Θ(n)(B) Θ(m)(C) Θ(m + n)(D) Θ(mn) Answer: (C) Explanation: Connected components can be fo
    2 min read
  • Data Structures and Algorithms | Set 28
    Following questions have been asked in GATE 2012 exam. 1) Let w(n) and A(n) denote respectively, the worst case and average case running time of an algorithm executed on an input of size n. which of the following is ALWAYS TRUE? (A) A(n) = Ω(W(n)) (B) A(n) = Θ(W(n)) (C) A(n) = O(W(n)) (D) A(n) = o(W
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences