Linux C 时间函数clock_gettime()的使用
函数: int clock_gettime(clockid_t clk_id, struct timespec *tp);
clockid_t: 用于指定计时时钟的类型,有以下几种类型:
CLOCK_REALTIME: 系统实时时间,从Epoch计时,可被设置更改。
CLOCK_MONOTONIC: 系统运行时间,从系统启动时开始计时,系统休眠时不再计时(NTP与硬件时钟有问题时会影响其频率,没有验证过)。
CLOCK_PROCESS_CPUTIME_ID: 本进程启动到此刻使用CPU的时间,当使用sleep等函数时不再计时。
CLOCK_THREAD_CPUTIME_ID: 本线程启动到此刻使用CPU的时间,当使用sleep等函数时不再计时。
CLOCK_MONOTONIC_RAW : 系统运行时间,从系统启动时开始计时,系统休眠时不再计时(NTP与硬件时钟有问题时不会影响其频率,没有验证过)。
CLOCK_REALTIME_COARSE: 系统实时时间,从Epoch计时,可被设置更改,速度更快精度更低。
CLOCK_MONOTONIC_COARSE: 系统运行时间,从系统启动时开始计时,速度更快精度更低,系统休眠时不再计时(NTP与硬件时钟有问题时会影响其频率,没有验证过)。
CLOCK_BOOTTIME: 与CLOCK_MONOTONIC类似
CLOCK_REALTIME_ALARM : 闹钟时间(应该休眠后继续计时,没验证过),系统实时时间,从Epoch计时,可被设置更改。
CLOCK_BOOTTIME_ALARM: 闹钟时间(应该休眠后继续计时,没验证过),系统运行时间,从系统启动时开始计时。
CLOCK_TAI: 原子钟的时间,与CLOCK_REALTIME类似,不可被更改,没有闰秒。
struct timespec:
struct timespec
{
time_t tv_sec; //秒
long tv_nsec; //纳秒
};
例子:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#include <unistd.h>
static void clock_gettime_test()
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
printf("CLOCK_REALTIME(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_REALTIME, &ts);
printf("CLOCK_REALTIME(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_MONOTONIC, &ts);
printf("CLOCK_MONOTONIC(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_MONOTONIC, &ts);
printf("CLOCK_MONOTONIC(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
printf("CLOCK_PROCESS_CPUTIME_ID(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
printf("CLOCK_PROCESS_CPUTIME_ID(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
printf("CLOCK_THREAD_CPUTIME_ID(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
printf("CLOCK_THREAD_CPUTIME_ID(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
printf("CLOCK_MONOTONIC_RAW(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
printf("CLOCK_MONOTONIC_RAW(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_REALTIME_COARSE, &ts);
printf("CLOCK_REALTIME_COARSE(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_REALTIME_COARSE, &ts);
printf("CLOCK_REALTIME_COARSE(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
printf("CLOCK_MONOTONIC_COARSE(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
printf("CLOCK_MONOTONIC_COARSE(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_BOOTTIME, &ts);
printf("CLOCK_BOOTTIME(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_BOOTTIME, &ts);
printf("CLOCK_BOOTTIME(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_REALTIME_ALARM, &ts);
printf("CLOCK_REALTIME_ALARM(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_REALTIME_ALARM, &ts);
printf("CLOCK_REALTIME_ALARM(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_BOOTTIME_ALARM, &ts);
printf("CLOCK_BOOTTIME_ALARM(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_BOOTTIME_ALARM, &ts);
printf("CLOCK_BOOTTIME_ALARM(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_TAI, &ts);
printf("CLOCK_TAI(1) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
usleep(1000 * 1000);
clock_gettime(CLOCK_TAI, &ts);
printf("CLOCK_TAI(2) sec = %lu, nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
}
int main()
{
clock_gettime_test();
return 0;
}
上一篇: Vue中全局变量Global
下一篇: 支付宝蚂蚁积分怎么兑换1元线下红包?