Vous êtes sur la page 1sur 6

Aim: To study and implement clock synchronization.

Theory: Clock synchronization is a problem from computer science and engineering which deals with the idea that internal clocks of several computers may differ. Even when initially set accurately, real clocks will differ after some amount of time due to clock drift, caused by clocks counting time at slightly different rates. There are several problems that occur as a repercussion of rate differences and several solutions, some being more appropriate than others in certain contexts. Problems: Besides the incorrectness of the time itself, there are problems associated with clock skew that take on more complexity in a distributed system in which several computers will need to realize the same global time.For instance, in Unix systems the make command is used to compile new or modified code without the need to recompile unchanged code. The make command uses the clock of the machine it runs on to determine which source files need to be recompiled. If the sources reside on a separate file server and the two machines have unsynchronized clocks, the make program might not produce the correct results. Solutions: In a centralized system the solution is trivial; the centralized server will dictate the system time. Cristian's algorithm and the Berkeley Algorithm are some solutions to the clock synchronization problem in a centralized server environment. In a distributed system the problem takes on more complexity because a global time is not easily known. The most used clock synchronization solution on the Internet is the Network Time Protocol (NTP) which is a layered client-server architecture based on UDP message passing. Lamport timestamps and Vector clocks are concepts of the logical clocks in distributed systems. Cristians Algorithm: Cristian's Algorithm is a method for clock synchronisation which can be used in many fields of distributive computer science but is primarily used in low-latency intranets. Cristian observed that this simple algorithm is probabilistic, in that it only achieves synchronisation if the round-trip time (RTT) of the request is short compared to required accuracy. It also suffers in implementations using a single server, making it unsuitable for many distributive applications where redundancy may be crucial.

Cristian's Algorithm works between a process P and a time server S connected to a source of UTC (Coordinated Universal Time). Put simply: 1. P requests the time from S 2. After receiving the request from P, S prepares a response and appends the time T from its own clock. 3. P then sets its time to be T + RTT/2 P needs to record the Round Trip Time (RTT) of the request it made to S so that it can set its clock to T + RTT/2. This method assumes that the RTT is split equally between both request and response, which may not always be the case but is a reasonable assumption on a LAN connection. Further accuracy can be gained by making multiple requests to S and using the response with the shortest RTT. We can estimate the accuracy of the system as follows. Let min be the minimum time to transmit a message one-way. The earliest point at which S could have placed the time T was min after P sent its request. Therefore the time at S when the message is received by P is in the range (T + min) to (T + RTT - min). The width of this range is (RTT - 2*min). This gives an accuracy of (RTT/2 - min). Conclusion: Thus we have studied and implemented Clock Synchronization.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,n,j=1; int t[10]; int c[10],s,r; cout<<"enter the number of processes"<<endl; cin>>n; for(i=1;i<=n;i++) { cout<<"enter the id of process \t"<<i<<endl; cin>>c[i]; cout<<"Enter the timestamp of process \t"<<i<<endl; cin>>t[i]; } cout<<"\t Processes"<<"\t Timestamp"<<endl; for(i=1;i<=n;i++) { cout<<"\t "<<c[i]<<"\t\t "<<t[i]<<endl; } while(j==1)

{ cout<<"Enter the process who wants to send data"<<endl; cin>>s; cout<<"Enter the receiver of the message "<<endl; cin>>r; if(t[r]>t[s]) { cout<<"Process "<<r<<"receives the message " <<endl; } else { t[r]=t[s]+1; cout<<"the new timestamp of the process "<<r<<" is"<<t[r]<<endl; cout<<"The message is now received by the process "<<r<<endl; cout<<"\t Processes"<<"\t Timestamp"<<endl; for(i=1;i<=n;i++) { cout<<"\t "<<c[i]<<"\t\t "<<t[i]<<endl; } } cout<<"Do you want to continue (0/1) "<<endl; cin>>j; if(j==1)

continue; else break; } getch(); }

/*OUTPUT: enter the number of processes 3 enter the id of process 1 Enter the timestamp of process 1 4 enter the id of process 2 Enter the timestamp of process 2 6 enter the id of process 3 Enter the timestamp of process 3 8 Processes 1 4 Timestamp 3 2 1

2 3

6 8

Enter the process who wants to send data 1 Enter the receiver of the message 2 Process 2receives the message Do you want to continue (0/1) 1 Enter the process who wants to send data 2 Enter the receiver of the message 1 the new timestamp of the process 1 is7 The message is now received by the process 1 Processes 1 2 3 7 6 8 Timestamp

Do you want to continue (0/1) 0 */

Vous aimerez peut-être aussi