mirror of https://github.com/mkerrisk/man-pages
clock_getres.2: Add an example program
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
parent
a48d19162d
commit
16fa57813e
|
@ -1,5 +1,6 @@
|
|||
.\" Copyright (c) 2003 Nick Clifford (zaf@nrc.co.nz), Jan 25, 2003
|
||||
.\" Copyright (c) 2003 Andries Brouwer (aeb@cwi.nl), Aug 24, 2003
|
||||
.\" Copyright (c) 2020 Michael Kerrisk <mtk.manpages@gmail.com>
|
||||
.\"
|
||||
.\" %%%LICENSE_START(VERBATIM)
|
||||
.\" Permission is granted to make and distribute verbatim copies of this
|
||||
|
@ -392,6 +393,90 @@ and
|
|||
.BR CLOCK_THREAD_CPUTIME_ID ,
|
||||
on systems that provide such an implementation
|
||||
(i.e., Linux 2.6.12 and later).
|
||||
.SH EXAMPLE
|
||||
The program below demonstrates the use of
|
||||
.BR clock_gettime ()
|
||||
and
|
||||
.BR clock_getres ()
|
||||
with various clocks.
|
||||
This is an example of what we might see when running the program:
|
||||
.PP
|
||||
.in +4n
|
||||
.EX
|
||||
$ \fB./clock_times x\fP
|
||||
CLOCK_REALTIME : 1585985459.446 (18356 days + 7h 30m 59s)
|
||||
resolution: 0.000000001
|
||||
CLOCK_TAI : 1585985496.447 (18356 days + 7h 31m 36s)
|
||||
resolution: 0.000000001
|
||||
CLOCK_MONOTONIC : 52395.722 (14h 33m 15s)
|
||||
resolution: 0.000000001
|
||||
CLOCK_BOOTTIME : 72691.019 (20h 11m 31s)
|
||||
resolution: 0.000000001
|
||||
.EE
|
||||
.in
|
||||
.SS Program source
|
||||
\&
|
||||
.EX
|
||||
/* clock_times.c
|
||||
|
||||
Licensed under GNU General Public License v2 or later.
|
||||
*/
|
||||
#define _XOPEN_SOURCE 600
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SECS_IN_DAY (24 * 60 * 60)
|
||||
|
||||
static void
|
||||
displayClock(clockid_t clock, char *name, bool showRes)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
if (clock_gettime(clock, &ts) == \-1) {
|
||||
perror("clock_gettime");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("%\-22s: %10ld.%03ld (", name,
|
||||
(long) ts.tv_sec, ts.tv_nsec / 1000000);
|
||||
|
||||
long days = ts.tv_sec / SECS_IN_DAY;
|
||||
if (days > 0)
|
||||
printf("%ld days + ", days);
|
||||
|
||||
printf("%2ldh %2ldm %2lds", (ts.tv_sec % SECS_IN_DAY) / 3600,
|
||||
(ts.tv_sec % 3600) / 60, ts.tv_sec % 60);
|
||||
printf(")\en");
|
||||
|
||||
if (clock_getres(clock, &ts) == \-1) {
|
||||
perror("clock_getres");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (showRes)
|
||||
printf(" resolution: %10ld.%09ld\en",
|
||||
(long) ts.tv_sec, ts.tv_nsec);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bool showRes = argc > 1;
|
||||
|
||||
displayClock(CLOCK_REALTIME, "CLOCK_REALTIME", showRes);
|
||||
#ifdef CLOCK_TAI
|
||||
displayClock(CLOCK_TAI, "CLOCK_TAI", showRes);
|
||||
#endif
|
||||
displayClock(CLOCK_MONOTONIC, "CLOCK_MONOTONIC", showRes);
|
||||
#ifdef CLOCK_BOOTTIME
|
||||
displayClock(CLOCK_BOOTTIME, "CLOCK_BOOTTIME", showRes);
|
||||
#endif
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
.EE
|
||||
.SH SEE ALSO
|
||||
.BR date (1),
|
||||
.BR gettimeofday (2),
|
||||
|
|
Loading…
Reference in New Issue