DISTRIBUTED SYSTEMS LAB PROGRAMS GENERAL INSTRUCTIONS:[Link] SHOULD USE PUNCHED FILE FOR DISTRIBUTED LAB MANUAL.2.
FRONT PAGE OF THE FILE AND THE INDEX PAGESHOULD BE PRINT [Link] SHOULD CONTAIN (SR. NO. , NAME OF THEPRACTICAL, DATE , GRADE AND SIGN)[Link] WILL USE PRACTICAL SHEET i.e. ONE SIDEPLAIN AND ONE SIDE [Link] WILL BE WRITTERN ON THE LEFT SIDE OFTHE PAGE WITH [Link] OF THE PROGRAM WILL BE WRITTERN ONRIGHT SIDE WITH PEN. PROGRAM NO. 1Simulate the functioning of Lamports Logical clock in [Link]: Lamport proposed a scheme to order the events in a distributedsystem by using logical clocks. Due to the absence of synchronizedclock and global time in a distributed system, the order in whichevents occur at two different machine is impossible to bedetermined based on the local time at which they occurred. Algorithm: This simulation takes place by using the following concepts.1)Happened Before Relationship2 ) L o g i c a l c l o c k s For any two events a & b, a is said to happened before b is denotedas a b, if they are in same [Link] events occur at different processes then for any message (m)Send (m) Receive (m)
If a b and b c then a c i.e. is [Link] events casually affects each other then they are said to becasually related events a bTwo events are concurrent if a b and b a i.e. a||bConditions satisfied by Logical Clocks. a) For any two events occurring on same process, a b if Ci[a]< Ci[b]. b)Clock Ci is implemented between any two events of the same process as Ci = Ci + d (d>0)c)If event a is sending message by process Pi and is received by process Pj, thentm = Ci(a)Cj = max(Cj+d, tm), d>0 CODE: #include<stdio.h>#include<conio.h>void main(){int p1[10],p2[10];int e,i,m1,m2; printf("enter the no of events in p1 & p2");scanf("%d",&e); printf("enter the event of p1 which will send msg");scanf("%d",&m1); printf("enter the event of p2 which will recieve the msg");scanf("%d",&m2); p[1]=1; p[2]=1;for(i=2;i<=e;i++){if(i!=m2){
p1[i]=p1[i-1]+1;p2[i]=p2[i-1]+1;}elsep2[i]=p1[m1]+1;} printf("event of p1 are");for(i=1;i<=e;i++){printf("%d",p1[i]);} printf("event of p2 are");for(i=1;i<=e;i++){printf("%d",p2[i]);}} PROGRAM NO. 2Simulate the Distributed Mutual Exclusion in C. | THEORY: If a collection of processes share a resource or collectionof resources, then often mutual exclusionis required to revent interferences and ensure consistency whenaccessing the resources. This iscritical section problem, familiar in the domain of operatingsystems. In a distributed system, however,neither sahred variables nor facilites supplied by a single localkernel can be used to solve it, ingeneral. We require a solution to distributed mutual exclusion: onethat is based solely on message passing. Algorithm: On initiation
state:= RELEASED;To enter the sectionstate:= WANTED;Multicast request to all processes;T:= request's timestamp;Wait until (number of replies receeived = (N-1));state:= HELD;On receipt of a request <T CODE: #include<stdio.h>#include<conio.h>#include<string.h>#include<pthread.h>#include<semaphore.h>#incl ude<unistd.h>void *thread_func(void *arg);thread_mutex_t_work_mutex;#define work_size 1024char work_area(work_size);int time_to_exit();int main(){int res; pthread=t;a=thread;void *thread_result;res=pthread_mutex_unit(&work_mutex,null);if(res!=0){perror("mutex initialization failed");exit(exit_failure);}res=pthread_create(&a_thread,null,thread function,null);if(res!=0){