Sie sind auf Seite 1von 3

RICART-AGRAWALA-Mutex ALGORITHM

KRISHNA VINAY
201501222

I implemented the ricart-agrawala algorithm in java using java RMIs and message passing by
google protobuf and generalised vector clocks.

Ricart-agrawala algorithm:-
This is one of the many algorithms to satisfy mutual exclusion on a distributed system.
We should ensure that one processor must enter critical section at a particular instant or time.
So to satisfy that, we have so many algorithms to ensure this. One of those algorithms are
Ricart-agrawala algorithm.
Here mutex ensures that in every node file, it locks a particular piece of code once. ( cant
access a variable in two different functions once.)

Implementation details:-

Let us say we have n nodes.


For every node i, Initializations are:-
i) bool in_critical_section = false; ( determines whether node in critical section or not)
ii) bool needed_lock = false; ( determines whether node needed lock means whether it wants
to enter the critical section or not)
iii) int my_id = i; ( my_id is equals to my processor number)
iv) Vector_clock my_clock,lock_clock = empty vector_clock ;
( Here my_clock is for updating the clock values of all the processors which it senses through
request messages or send_ack messages and lock_clock will update when the processor wants
to get enter into critical section)
v) Hash_Set queue = empty Hash_set;
( Here queue is a hash set which stores the processor ids which our processor defers to send
reply)

After initializations, we have to start RMI server for this node with a fixed name.

Internal Functions:-

After initializations node wants to enter into the critical section thus it calls for lock() and
changes needed_lock = true;
i) Lock()
So, Here Lock functions calls to Lock_request function all other N-1 processors to give
access to enter the critical section and make wait_count=N-1;
Here the message is serialized through google protobuf and it send to desired process in
protobuf format only. Message is just {my_id,my_clock}
And here In the need_ack hash_table it pushes all the processors which it has to receive
the lock to enter CS.
Here, function calling of other nodes is through RMI. It just gets the remote object and calls
lock_request.
After it sends all requests, it waits for acknowledgment from all the processes.
After, that it waits until wait_cont == 0 and after it gets access ti enter into critical section it
changes in_critical_section = true;
And executes critical section
Next it calls unlock function:-
ii) unLock():-
It just sends the acknowledgments to all the processors which are in queue stating that i am
done with my critical section execution.
It sends the id and clock in protobuf format only.
It just sends to all the processors which are in queue.

** Also there are some extra functions which handles the serialization part of a message and
deserialization one. Ex:- getbute()

Functions which can be accessed through a node RMI server:-


i) Lock_Request function:-

This function handles the lock_requests which it gets from a different node. If it gets a
message from a node first it will deserialize the message and gets the id and its clock.
Next it checks the function is prioritized to enter into critical section than the other processors
already sent its requests and itself. If it is allowed it just sends the reply by sending
acknowledgment by calling send_ack_request.
Otherwise , it will add the processor id into the queue.

ii) Send_ack_request function:-

This function handles receiving acknowledgments to self from other processors.


If it needed lock and the message it got has id and clock then it will just reduces the wait_count
by 1 if in need_ack array has the received my_id. Then it will remove the received id from the
need_ack after reducing the wait_count.

Files attached are :-

Node.java which has every node functions.


Nodedef.java Interface function.
Vector_Clock file which handles efficient functions to put,update,search cases.
A file named Message.proto has the schema of the messages. For the more fast
serialization and deserialization I select protobuf concept.

Anaylisis:-

Ricart-Agrawala algorithm needs 2(N-1) messages for a node to enter into the critical section.
So, Here our code also needs 2*(N-1) messages for every node to enter into critical section. For
every node a file outputs its results into a individual file. And finally, when a node enter into the
critical section, log file goes to output file. This algorithm is completely distributed and
decentralized. But it still requires O(n). If we leave our code after started without interrupting,
every node tries to enter critical section 100 times.