DISTRIBUTED SYSTEMS LAB PROGRAMS GENERAL INSTRUCTIONS:1.YOU SHOULD USE PUNCHED FILE FOR DISTRIBUTED LAB LAB MANU MANUAL AL.2.FRON .2.FRON T PAGE OF THE FIL E AND THE INDE X PAGE SHOU SHOULD LD BE PRIN PRINT T OUT3.IND OUT3.IND EX SHO ULD CONT AIN (SR . N O. , N AME OF THE PRAC PRACTI TICA CAL, L, DATE DATE , GRAD GRADE E AND AND SIGN SIGN)4.YO )4.YO U W ILL USE PRAC TIC AL SHE ET i.e. ONE SID EPLAIN EPLAIN AND AND ONE ONE SIDE SIDE RULED5.THEORY WILL BE WRITTERN ON THE LEFT SIDE OFTHE PAGE WITH PENC PENCIL IL6.C 6.C ODE OF THE PROG RAM WILL BE WRIT TER N ONRIGHT ONRIGHT SIDE SIDE WI WITH TH PEN. PEN. PROGRAM NO. 1Simulate the functioning of Lamport’s of Lamport’s Logical clock in ‘C’.Theory: Lamport proposed a scheme to order the events in a distributedsystem 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 simula simulation tion takes takes place place by using using the the following following concep concepts.1)H ts.1)H ap pe ne d Befo Be fo re Rel at io ns hi p2 ) L o g i c a l c l o c k s For For any any two event eventss a & b, b, a is said said to to hap happen pened ed befor beforee b is denote denotedas das a
b, if they are are in same same process.If process.If events occur occur at different different processes processes then then for any message message (m)Send (m)Send (m) Receive (m)
If a b and b c then a c i.e. is transitive.If events casually affects each other then they are said to becasually related events a bTwo events are concurrent 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]. Ci[b]. b)Clock Ci is implemented implemented between between any any two events events of the same same process 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
#inclu #include#includev devoid oid 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",&m msg");scanf("%d",&m2); 2); p[1]=1; p[2]=1;for(i=2;i<=e;i++){if(i!=m p[2]=1;for(i=2;i<=e;i++){if(i!=m2){ 2){
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 #include#include#include#include#incl udevoid *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){