100% found this document useful (1 vote)
407 views

C Program For Lamport Logical Clock - Program Code Lib

The document describes an implementation of Lamport's logical clock algorithm in C programming language. The algorithm assigns timestamps to events in a distributed system to determine the ordering of events. The C program takes the dependency of events as input, calculates the timestamps using Lamport's algorithm, and outputs the timestamps for each process. It uses a dependency matrix and max() function to update timestamps based on causal dependencies between events.

Uploaded by

faisal siddiqui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
407 views

C Program For Lamport Logical Clock - Program Code Lib

The document describes an implementation of Lamport's logical clock algorithm in C programming language. The algorithm assigns timestamps to events in a distributed system to determine the ordering of events. The C program takes the dependency of events as input, calculates the timestamps using Lamport's algorithm, and outputs the timestamps for each process. It uses a dependency matrix and max() function to update timestamps based on causal dependencies between events.

Uploaded by

faisal siddiqui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C Distributed System

C program for Lamport Logical


Clock
Harshit Tandon • September 22, 2013 6  39,641  Less than a minute

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.

#include<stdio.h>
#include<conio.h>
int max1(int a, int b) //to find the maximum timestamp betwee
{
if (a>b)
return a;
else
return b;
}

int main()
{
int i,j,k,p1[20],p2[20],e1,e2,dep[20][20];
printf("enter the events : ");
scanf("%d %d",&e1,&e2);
for(i=0;i<e1;i++)
p1[i]=i+1;
for(i=0;i<e2;i++)
p2[i]=i+1;
printf("enter the dependency matrix:\n");
printf("\t enter 1 if e1->e2 \n\t enter -1, if e2->e1 \n\t else
for(i=0;i<e2;i++)
printf("\te2%d",i+1);
for(i=0;i<e1;i++)
{
printf("\n e1%d \t",i+1);
for(j=0;j<e2;j++)
scanf("%d",&dep[i][j]);
}
for(i=0;i<e1;i++)
{
for(j=0;j<e2;j++)
{
if(dep[i][j]==1) //change the timestamp if dependency ex
{ p2[j]=max1(p2[j],p1[i]+1);
for(k=j;k<e2;k++)
p2[k+1]=p2[k]+1;
}
if(dep[i][j]==-1) //change the timestamp if dependency ex
{
p1[i]=max1(p1[i],p2[j]+1);
for(k=i;k<e1;k++)
p2[k+1]=p1[k]+1;
}

}
}
printf("P1 : "); //to print the outcome of Lamport Logical C
for(i=0;i<e1;i++)
{
printf("%d",p1[i]);
}
printf("\n P2 : ");
for(j=0;j<e2;j++)
printf("%d",p2[j]);

getch();
return 0 ;
}

OUTPUT:

Tags: C program for the implementation of Lamport Logical Clock, C program


for Lamport Timestamp Algorithm
 Tags Lamport logical clock

6 Comments
abc
August 6, 2015 at 12:53 pm

thanks for the program

Reply

manvitha
February 23, 2017 at 10:26 pm

Really helpful, i am going to take an assignment on this. Thank you for


sharing and i am going to take this as a sample and develop the code.

Reply

Mayank Kashyap
September 19, 2017 at 9:41 am

Check for the I/P:


45
00100
00000
0 0 -1 0 0
00000

Output should be
P1:1245
P2:12356

But it’s showing wrong output!!!!


Serious Flaw in the code.

Reply

Anonymous
February 23, 2018 at 9:42 am

there is serious flaw in u.

Reply
mahesh
February 26, 2018 at 7:30 pm

thanx for post admin

Reply
lahsuk
November 26, 2018 at 2:16 pm

#include
#include

// Function to calculate maximum timestamp between two events


int max(int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}

// Main Function implementing Lamport’s Algorithm


int main()
{
int process_count, process_id, event_type, sending_clock, sending_pid,
sending_eid, lamport_clock[5][20];
int event_count[5];
int i, j;
printf(“nEnter the number of processes : “);
scanf(“%d”, &process_count);
// Initialization of lamport clock and event_count
for (i=0; i<5; i++)
{
event_count[i] = 0;
for (j=0; j0 && process_id=0 && event_type<=2)
{
event_count[process_id-1]++;
if (event_type == 0 || event_type == 1)
{
if (lamport_clock[process_id-1][0] == 0)
{
lamport_clock[process_id-1][event_count[process_id-1]-1] =
lamport_clock[process_id-1][event_count[process_id-1]-1] + 1;
}
else
{
lamport_clock[process_id-1][event_count[process_id-1]-1] =
lamport_clock[process_id-1][event_count[process_id-1]-2] + 1;
}
}
else
{
printf("Enter the process id and event number of message sending event :
");
scanf("%d%d", &sending_pid, &sending_eid);
sending_clock = lamport_clock[sending_pid-1][sending_eid-1];
if (sending_clock == 0)
{
printf("Invalid message sending event… Try Again… n");
event_count[process_id-1]–;
continue;
}
lamport_clock[process_id-1][event_count[process_id-1]-1] =
max(lamport_clock[process_id-1][event_count[process_id-1]-2]+1,
sending_clock+1);
}
}
else
{
printf("Event type mismatch…. Try Again…. n");
}
}
else
{
printf("Process id is not available….. try again n");
continue;
}
}
printf("%dn", event_count[0]);
printf("%dn", event_count[1]);
// Display the Lamport's Logical Clock
for (i=0; i<process_count; i++)
{
printf("Lamport's Clock Value for Process %d n", i+1);
printf("tEventtTimen");
for (j=0; j<event_count[i]; j++)
{
printf("t%dt%dn", j+1, lamport_clock[i][j]);
}
}
return 0;
}

Reply

You might also like