4 * sht.c - Testprogram for shared memory refclock
5 * read/write shared memory segment; see usage
21 #define sleep(x) Sleep(x*1000)
25 int mode
; /* 0 - if valid set
29 * if count before and after read of values is equal,
34 time_t clockTimeStampSec
;
35 int clockTimeStampUSec
;
36 time_t receiveTimeStampSec
;
37 int receiveTimeStampUSec
;
50 int shmid
=shmget (0x4e545030+unit
, sizeof (struct shmTime
), IPC_CREAT
|0777);
56 struct shmTime
*p
=(struct shmTime
*)shmat (shmid
, 0, 0);
57 if ((int)(long)p
==-1) {
66 LPSECURITY_ATTRIBUTES psec
=0;
67 sprintf (buf
,"NTP%d",unit
);
68 SECURITY_DESCRIPTOR sd
;
69 SECURITY_ATTRIBUTES sa
;
72 assert (InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
));
73 assert (SetSecurityDescriptorDacl(&sd
,1,0,0));
74 sa
.nLength
=sizeof (SECURITY_ATTRIBUTES
);
75 sa
.lpSecurityDescriptor
=&sd
;
77 shmid
=CreateFileMapping ((HANDLE
)0xffffffff, 0, PAGE_READWRITE
,
78 psec
, sizeof (struct shmTime
),buf
);
80 shmid
=CreateFileMapping ((HANDLE
)0xffffffff, 0, PAGE_READWRITE
,
81 0, sizeof (struct shmTime
),buf
);
82 cout
<<"CreateFileMapping with psec!=0 failed"<<endl
;
87 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
,
88 0, GetLastError (), 0, mbuf
, sizeof (mbuf
), 0);
89 int x
=GetLastError ();
90 cout
<<"CreateFileMapping "<<buf
<<":"<<mbuf
<<endl
;
94 struct shmTime
*p
=(struct shmTime
*) MapViewOfFile (shmid
,
95 FILE_MAP_WRITE
, 0, 0, sizeof (struct shmTime
));
98 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
,
99 0, GetLastError (), 0, mbuf
, sizeof (mbuf
), 0);
100 cout
<<"MapViewOfFile "<<buf
<<":"<<mbuf
<<endl
;
116 volatile struct shmTime
*p
=getShmTime(2);
118 printf ("usage: %s r[c][l]|w|snnn\n",argv
[0]);
119 printf (" r read shared memory\n");
120 printf (" c clear valid-flag\n");
121 printf (" l loop (so, rcl will read and clear in a loop\n");
122 printf (" w write shared memory with current time\n");
123 printf (" snnnn set nsamples to nnn\n");
124 printf (" lnnnn set leap to nnn\n");
125 printf (" pnnnn set precision to -nnn\n");
128 switch (argv
[1][0]) {
130 p
->nsamples
=atoi(&argv
[1][1]);
134 p
->leap
=atoi(&argv
[1][1]);
138 p
->precision
=-atoi(&argv
[1][1]);
142 char *ap
=&argv
[1][1];
148 case 'l' : loop
=1; break;
149 case 'c' : clear
=1; break;
154 printf ("mode=%d, count=%d, clock=%d.%d, rec=%d.%d,\n",
155 p
->mode
,p
->count
,p
->clockTimeStampSec
,p
->clockTimeStampUSec
,
156 p
->receiveTimeStampSec
,p
->receiveTimeStampUSec
);
157 printf (" leap=%d, precision=%d, nsamples=%d, valid=%d\n",
158 p
->leap
, p
->precision
, p
->nsamples
, p
->valid
);
163 printf ("cleared\n");
174 p
->clockTimeStampSec
=time(0)-20;
175 p
->clockTimeStampUSec
=0;
176 p
->receiveTimeStampSec
=time(0)-1;
177 p
->receiveTimeStampUSec
=0;
178 printf ("%d %d\n",p
->clockTimeStampSec
, p
->receiveTimeStampSec
);
182 printf ("p->valid still set\n"); /* not an error! */