492 Chapter 23
The CLOCK_REALTIME clock is a system-wide clock that measures wall-clock time. By
contrast with the CLOCK_MONOTONIC clock, the setting of this clock can be changed.
SUSv3 specifies that the CLOCK_MONOTONIC clock measures time since some
“unspecified point in the past” that doesn’t change after system startup. This clock
is useful for applications that must not be affected by discontinuous changes to the
system clock (e.g., a manual change to the system time). On Linux, this clock mea-
sures the time since system startup.
The CLOCK_PROCESS_CPUTIME_ID clock measures the user and system CPU time con-
sumed by the calling process. The CLOCK_THREAD_CPUTIME_ID clock performs the analogous
task for an individual thread within a process.
All of the clocks in Table 23-1 are specified in SUSv3, but only CLOCK_REALTIME is
mandatory and widely supported on UNIX implementations.
Linux 2.6.28 adds a new clock type, CLOCK_MONOTONIC_RAW, to those listed in
Table 23-1. This is a nonsettable clock that is similar to CLOCK_MONOTONIC, but it
gives access to a pure hardware-based time that is unaffected by NTP adjust-
ments. This nonstandard clock is intended for use in specialized clock-
synchronization applications.
Linux 2.6.32 adds two more new clocks to those listed in Table 23-1:
CLOCK_REALTIME_COARSE and CLOCK_MONOTIC_COARSE. These clocks are similar to
CLOCK_REALTIME and CLOCK_MONOTONIC, but intended for applications that want to
obtain lower-resolution timestamps at minimal cost. These nonstandard clocks
don’t cause any access to the hardware clock (which can be expensive for some
hardware clock sources), and the resolution of the returned value is the jiffy
(Section 10.6).
23.5.2 Setting the Value of a Clock: clock_settime().............................................
The clock_settime() system call sets the clock specified by clockid to the time supplied
in the buffer pointed to by tp.
If the time specified by tp is not a multiple of the clock resolution as returned by
clock_getres(), the time is rounded downward.
A privileged (CAP_SYS_TIME) process may set the CLOCK_REALTIME clock. The initial
value of this clock is typically the time since the Epoch. None of the other clocks in
Table 23-1 are modifiable.
According to SUSv3, an implementation may allow the CLOCK_PROCESS_CPUTIME_ID
and CLOCK_THREAD_CPUTIME_ID clocks to be settable. At the time of writing, these
clocks are read-only on Linux.
#define _POSIX_C_SOURCE 199309
#include <time.h>
int clock_settime(clockid_t clockid, const struct timespec *tp);
Returns 0 on success, or –1 on error