/* file: ClockSyncServer.c */ /* author: MLehr */ /* * Copyright (C) 1994 * by the Rector and Board of Visitors of * the University of Virginia * * for more information: * Sanghyuk Son * Department of Computer Science * School of Engineering and Applied Science * University of Virginia * Charlottesville, VA 22903 */ #include "ClockSync.h" /* This program is inspired by "Time, Clocks, and the Ordering of Events in a Distributed System" by Lamport. Start up a server on the two hosts to be synchronized, then start up the client portion of this program on each host. */ static ClockSyncReplyMessage reply; static ClockSyncRequestMessage request; static mach_port_t clock; static mach_port_t servicePort; static rt_priority_data_t priority; void main () { kern_return_t status; rt_mach_port_attr_t portAttrs; OPEN_RT_CLOCK (clock, status); /* here we don't care what values we initialize with since RT-IPC will perform priority handoff for us */ bzero ((char *) &priority, sizeof (rt_priority_data_t)); INIT_RT_MESSAGE_HEADER (request, request.header, request.descriptor, 0, priority.rt_deadline, priority.rt_period, CRITICALITY); INIT_RT_MESSAGE_HEADER (reply, reply.header, reply.descriptor, 0, priority.rt_deadline, priority.rt_period, CRITICALITY); /* create the service port and register it with Mach's network name server */ INIT_RT_PORT_ATTR (portAttrs, sizeof (ClockSyncRequestMessage), 2, DISP_PRI, PRI_BPI, HANDOFF_MSG); CREATE_RT_PORT (servicePort, portAttrs, status); RT_PORT_ASSOCIATE (mach_thread_self (), servicePort, priority, status); status = netname_check_in (name_server_port, "ClockSyncServer", mach_task_self (), servicePort); if (status != KERN_SUCCESS) MACH_CRASH ("netname_check_in", status); while (TRUE) { RECEIVE_RT_MESSAGE (servicePort, request.header, status); /* "IR2: If event a is the sending of a message m by process P[i], then the message m contains a timestamp T[m] = C[i]." */ READ_TIME (clock, reply.timeStamp, status); SEND_RT_MESSAGE (request.header.msgh_remote_port, reply.header, status); } exit (-1); }