2 * sht.c - Testprogram for shared memory refclock
3 * read/write shared memory segment; see usage
19 #define sleep(x) Sleep(x*1000)
23 int mode; /* 0 - if valid set
27 * if count before and after read of values is equal,
32 time_t clockTimeStampSec;
33 int clockTimeStampUSec;
34 time_t receiveTimeStampSec;
35 int receiveTimeStampUSec;
48 int shmid=shmget (0x4e545030+unit, sizeof (struct shmTime), IPC_CREAT|0777);
54 struct shmTime *p=(struct shmTime *)shmat (shmid, 0, 0);
55 if ((int)(long)p==-1) {
64 LPSECURITY_ATTRIBUTES psec=0;
65 sprintf (buf,"NTP%d",unit);
66 SECURITY_DESCRIPTOR sd;
67 SECURITY_ATTRIBUTES sa;
70 assert (InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION));
71 assert (SetSecurityDescriptorDacl(&sd,1,0,0));
72 sa.nLength=sizeof (SECURITY_ATTRIBUTES);
73 sa.lpSecurityDescriptor=&sd;
75 shmid=CreateFileMapping ((HANDLE)0xffffffff, 0, PAGE_READWRITE,
76 psec, sizeof (struct shmTime),buf);
78 shmid=CreateFileMapping ((HANDLE)0xffffffff, 0, PAGE_READWRITE,
79 0, sizeof (struct shmTime),buf);
80 cout <<"CreateFileMapping with psec!=0 failed"<<endl;
85 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
86 0, GetLastError (), 0, mbuf, sizeof (mbuf), 0);
87 int x=GetLastError ();
88 cout <<"CreateFileMapping "<<buf<<":"<<mbuf<<endl;
92 struct shmTime *p=(struct shmTime *) MapViewOfFile (shmid,
93 FILE_MAP_WRITE, 0, 0, sizeof (struct shmTime));
96 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
97 0, GetLastError (), 0, mbuf, sizeof (mbuf), 0);
98 cout <<"MapViewOfFile "<<buf<<":"<<mbuf<<endl;
114 volatile struct shmTime *p=getShmTime(2);
116 printf ("usage: %s r[c][l]|w|snnn\n",argv[0]);
117 printf (" r read shared memory\n");
118 printf (" c clear valid-flag\n");
119 printf (" l loop (so, rcl will read and clear in a loop\n");
120 printf (" w write shared memory with current time\n");
121 printf (" snnnn set nsamples to nnn\n");
122 printf (" lnnnn set leap to nnn\n");
123 printf (" pnnnn set precision to -nnn\n");
126 switch (argv[1][0]) {
128 p->nsamples=atoi(&argv[1][1]);
132 p->leap=atoi(&argv[1][1]);
136 p->precision=-atoi(&argv[1][1]);
140 char *ap=&argv[1][1];
146 case 'l' : loop=1; break;
147 case 'c' : clear=1; break;
152 printf ("mode=%d, count=%d, clock=%d.%d, rec=%d.%d,\n",
153 p->mode,p->count,p->clockTimeStampSec,p->clockTimeStampUSec,
154 p->receiveTimeStampSec,p->receiveTimeStampUSec);
155 printf (" leap=%d, precision=%d, nsamples=%d, valid=%d\n",
156 p->leap, p->precision, p->nsamples, p->valid);
161 printf ("cleared\n");
172 p->clockTimeStampSec=time(0)-20;
173 p->clockTimeStampUSec=0;
174 p->receiveTimeStampSec=time(0)-1;
175 p->receiveTimeStampUSec=0;
176 printf ("%d %d\n",p->clockTimeStampSec, p->receiveTimeStampSec);
180 printf ("p->valid still set\n"); /* not an error! */