1CLOCK_GETRES(3) Linux Programmer's Manual CLOCK_GETRES(3)
2
3
4
6 clock_getres, clock_gettime, clock_settime - clock and time functions
7
9 #include <time.h>
10
11 int clock_getres(clockid_t clk_id, struct timespec *res);
12 int clock_gettime(clockid_t clk_id, struct timespec *tp);
13 int clock_settime(clockid_t clk_id, const struct timespec *tp);
14
16 The function clock_getres() finds the resolution (precision) of the
17 specified clock clk_id, and, if res is non-NULL, stores it in the
18 struct timespec pointed to by res. The resolution of clocks depends on
19 the implementation and cannot be configured by a particular process.
20 If the time value pointed to by the argument tp of clock_settime() is
21 not a multiple of res, then it is truncated to a multiple of res.
22
23 The functions clock_gettime() and clock_settime() retrieve and set the
24 time of the specified clock clk_id.
25
26 The res and tp arguments are timespec structs, as specified in
27 <time.h>:
28
29 struct timespec {
30 time_t tv_sec; /* seconds */
31 long tv_nsec; /* nanoseconds */
32 };
33
34 The clk_id argument is the identifier of the particular clock on which
35 to act. A clock may be system-wide and hence visible for all pro‐
36 cesses, or per-process if it measures time only within a single
37 process.
38
39 All implementations support the system-wide realtime clock, which is
40 identified by CLOCK_REALTIME. Its time represents seconds and nanosec‐
41 onds since the Epoch. When its time is changed, timers for a relative
42 interval are unaffected, but timers for an absolute point in time are
43 affected.
44
45 More clocks may be implemented. The interpretation of the corresponding
46 time values and the effect on timers is unspecified.
47
48 Sufficiently recent versions of GNU libc and the Linux kernel support
49 the following clocks:
50
51 CLOCK_REALTIME
52 System-wide realtime clock. Setting this clock requires appro‐
53 priate privileges.
54
55 CLOCK_MONOTONIC
56 Clock that cannot be set and represents monotonic time since
57 some unspecified starting point.
58
59 CLOCK_PROCESS_CPUTIME_ID
60 High-resolution per-process timer from the CPU.
61
62 CLOCK_THREAD_CPUTIME_ID
63 Thread-specific CPU-time clock.
64
66 clock_gettime(), clock_settime() and clock_getres() return 0 for suc‐
67 cess, or -1 for failure (in which case errno is set appropriately).
68
70 EFAULT tp points outside the accessible address space.
71
72 EINVAL The clk_id specified is not supported on this system.
73
74 EPERM clock_settime() does not have permission to set the clock indi‐
75 cated.
76
78 Most systems require the program be linked with the librt library to
79 use these functions.
80
82 The CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID clocks are
83 realized on many platforms using timers from the CPUs (TSC on i386,
84 AR.ITC on Itanium). These registers may differ between CPUs and as a
85 consequence these clocks may return bogus results if a process is
86 migrated to another CPU.
87
88 If the CPUs in an SMP system have different clock sources then there is
89 no way to maintain a correlation between the timer registers since each
90 CPU will run at a slightly different frequency. If that is the case
91 then clock_getcpuclockid(0) will return ENOENT to signify this condi‐
92 tion. The two clocks will then only be useful if it can be ensured that
93 a process stays on a certain CPU.
94
95 The processors in an SMP system do not start all at exactly the same
96 time and therefore the timer registers are typically running at an off‐
97 set. Some architectures include code that attempts to limit these off‐
98 sets on bootup. However, the code cannot guarantee to accurately tune
99 the offsets. Glibc contains no provisions to deal with these offsets
100 (unlike the Linux Kernel). Typically these offsets are small and there‐
101 fore the effects may be negligible in most cases.
102
104 On POSIX systems on which these functions are available, the symbol
105 _POSIX_TIMERS is defined in <unistd.h> to a value greater than 0. The
106 symbols _POSIX_MONOTONIC_CLOCK, _POSIX_CPUTIME, _POSIX_THREAD_CPUTIME
107 indicate that CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID,
108 CLOCK_THREAD_CPUTIME_ID are available. (See also sysconf(3).)
109
111 SUSv2, POSIX.1-2001.
112
114 date(1), adjtimex(2), gettimeofday(2), settimeofday(2), time(2),
115 ctime(3), ftime(3), sysconf(3)
116
117
118
119 2003-08-24 CLOCK_GETRES(3)