Linux Clock & Time

来源:互联网 发布:linux文件重命名 编辑:程序博客网 时间:2024/06/05 03:24
<span style="font-family:System;font-size:14px;">#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>static void clock_info(clockid_t clk_id){    int result = 0;    struct timespec tsp;    memset((void*)(&tsp), 0, sizeof(struct timespec));    result = clock_getres(clk_id, &tsp);    printf("clock resolution, seconds is: %ld\n", (long)(tsp.tv_sec));    printf("clock resolution, nanoseconds is: %ld\n", (long)(tsp.tv_nsec));    memset((void*)(&tsp), 0, sizeof(struct timespec));    result = clock_gettime(clk_id, &tsp);    printf("get time, seconds is: %ld\n", (long)(tsp.tv_sec));    printf("get time, nanoseconds is: %ld\n", (long)(tsp.tv_nsec));}int main(int argc, char** argv){int result = 0;struct timespec tsp;    printf("CLOCK_REALTIME*****************************************************\n");    clock_info(CLOCK_REALTIME);    printf("CLOCK_REALTIME_COARSE**********************************************\n");    clock_info(CLOCK_REALTIME_COARSE);    printf("CLOCK_MONOTONIC****************************************************\n");    clock_info(CLOCK_MONOTONIC);    printf("CLOCK_MONOTONIC_COARSE*********************************************\n");    clock_info(CLOCK_MONOTONIC_COARSE);    printf("CLOCK_MONOTONIC_RAW************************************************\n");    clock_info(CLOCK_MONOTONIC_RAW);    printf("CLOCK_BOOTTIME*****************************************************\n");    clock_info(CLOCK_BOOTTIME);    printf("CLOCK_PROCESS_CPUTIME_ID*******************************************\n");    clock_info(CLOCK_PROCESS_CPUTIME_ID);    printf("CLOCK_THREAD_CPUTIME_ID********************************************\n");    clock_info(CLOCK_THREAD_CPUTIME_ID);return 0;}</span>
<span style="font-family:System;font-size:14px;">Linux 终端下 man clock_gettime</span>
<span style="font-family:System;font-size:14px;">CLOCK_REALTIME              System-wide clock that measures real (i.e., wall-clock) time.  Setting this clock </span>
<span style="font-family:System;font-size:14px;">requires appropriate privileges.  This clock is affected by discontinu‐ous jumps in the system time </span>
<span style="font-family:System;font-size:14px;">(e.g., if the system administrator manually changes the clock), and by the incremental adjustments </span>
<span style="font-family:System;font-size:14px;">performed  by  adjtime(3) and NTP.</span>


CLOCK_MONOTONIC
              Clock  that  cannot be set and represents monotonic time since some unspecified starting 

point.  This clock is not affected by discontinuous jumps in the system time (e.g., if the system 

administrator manually changes the clock), but is affected by the  incremental  adjustments  performed  

by adjtime(3) and NTP.


Reference Site:

http://www.it165.net/os/html/201309/6136.html

0 0