Computer Science, asked by Mridul1477, 1 year ago

Write a program in c to stimulate distributed mutual exclusion

Answers

Answered by 12345Rohit
1
Lamport Logical Clock to find the ordering of events in a Distributed System. In Lamport Algorithm each event has its own timestamp which depends on the occuring of events in the order the message has been sent by which event of a process to whichever event.


if you see formula then link is here-
https://programcodelib.com/2013/09/c-program-for-lamport-logical-clock/amp/
Attachments:
Answered by Anonymous
0

Program in c to simulate distributed mutual exclusion:

  • Peterson's Algorithm for mutual exclusion by using only the shared memory. It uses two ideas in th algorithm:

      #include<stdio.h>

      #include<pthread.h>

      int flag[2];

      int turn;

      const int MAX=199;

      int ans=0;

      void lock_init()

      {

      flag[0]=flag[1]=0;

      turn=0;

      }

      void lock(int self)

      {

      flag[self]=1;

      turn=1-self;

      while(flag[1-self]==1  && turn==1-self);

      }

      void unlock(int self)

      {

       flag[self]=0;

      }

     void* func(void *s)

     {

     int i=0;

     int self=(int *)s;

     lock(self);

     for(int i=0;i<MAX;i++)

       ans++;

     unlock(self);

     }

Similar questions