zoukankan      html  css  js  c++  java
  • Linux不同时钟的区别

    今天发现项目中调用 clock_gettime 函数传入时钟类型参数时有 CLOCK_MONOTONIC、CLOCK_MONOTONIC_RAW、CLOCK_BOOTTIME、CLOCK_REALTIME 几种,对这几种时钟类型的区别有些疑惑,于是查阅了函数的说明文档,编写代码测试,以加深对clock_gettime 函数的理解

    测试代码

    #include <time.h>
    #include<iostream>
    using namespace std;
    
    void mono_time_test()
    {
      struct timespec sp;
      clock_gettime(CLOCK_MONOTONIC, &sp);
      std::cout << "mono_time...sec:" << sp.tv_sec << std::endl;
    }
    
    void real_time_test()
    {
       struct timespec sp;
       clock_gettime(CLOCK_REALTIME, &sp);
       std::cout << "real_time...sec:" << sp.tv_sec << std::endl;
    
    }
    void boot_time_test()
    {
        struct timespec sp;
        clock_gettime(CLOCK_BOOTTIME, &sp);
        std::cout <<"boot_time..sec:" << sp.tv_sec << std::endl;
    }
    
    void mono_raw_time_test()
    {
       struct timespec sp;
       clock_gettime(CLOCK_MONOTONIC_RAW, &sp);
       std::cout << "mono_raw_time...sec:" << sp.tv_sec << std::endl;
    }
    
    int main(int argc, char* argv[])
    {
            mono_time_test();
            mono_raw_time_test();
            boot_time_test();
            real_time_test();
            
            return 1;
    }
    
    
    • 使用以下命令编译代码,生成测试程序
    [wl@ecs-centos-7 my]$ g++ -o main main.cpp 
    [wl@ecs-centos-7 my]$ ll
    total 16
    -rwxrwxr-x 1 wl wl 9264 Apr 10 23:30 main
    -rw-rw-r-- 1 wl wl  848 Apr 10 23:23 main.cpp
    

    机器时间测试

    • 修改机器时间前,执行测试程序,输出如下结果
    [wl@ecs-centos-7 my]$ ./main 
    mono_time...sec:71
    mono_raw_time...sec:71
    boot_time..sec:71
    real_time...sec:1586532575
    [wl@ecs-centos-7 my]$ date -d "1970-01-01 UTC 1586532575 seconds" "+%F %T"          
    2020-04-10 23:29:35
    
    • 执行以下命令,修改机器时间
    [root@ecs-centos-7 my]$ date -s "2020-04-11 23:30:08"
    
    • 机器时间修改完成,执行测试程序,输出如下结果
    [root@ecs-centos-7 my]# date
    Sat Apr 11 23:30:01 CST 2020
    [wl@ecs-centos-7 my]$ ./main 
    mono_time...sec:178
    mono_raw_time...sec:178
    boot_time..sec:178
    real_time...sec:1586619008
    [wl@ecs-centos-7 ~]$ date -d "1970-01-01 UTC 1586619008 seconds" "+%F %T"          
    2020-04-11 23:30:08
    

    测试结果

    • CLOCK_MONOTONIC、CLOCK_MONOTONIC_RAW、CLOCK_BOOTTIME 都不随物理机器时间改变而改变

    • CLOCK_REALTIME 是随物理机器时间改变而改变的

    系统休眠测试

    下面测试系统休眠对几种时钟的影响,为了对比休眠测试的结果,休眠前后都需要执行一次测试程序

    • 休眠前,执行测试程序,下面是输出结果
    [wl@ecs-centos-7 my]$ ./main 
    mono_time...sec:45
    mono_raw_time...sec:45
    boot_time..sec:45
    real_time...sec:1586535342
    
    
    • 执行以下命令,使系统进入休眠

    注意:systemctl suspend 命令需要慎重,为避免执行后无法唤醒系统,建议先确保机器可以唤醒休眠。我是通过云服管理后台唤醒系统的

    [wl@ecs-centos-7 my]$ systemctl suspend
    ==== AUTHENTICATING FOR org.freedesktop.login1.suspend ===
    Authentication is required for suspending the system.
    Authenticating as: root
    Password: 
    ==== AUTHENTICATION COMPLETE ===
    
    • 唤醒系统,执行测试程序,下面是输出结果
    [wl@ecs-centos-7 my]$ ./main 
    mono_time...sec:237
    mono_raw_time...sec:237
    boot_time..sec:367
    real_time...sec:1586535664
    

    测试结果

    • CLOCK_MONOTONIC、CLOCK_MONOTONIC_RAW 不包含系统休眠期间的时间

    • CLOCK_BOOTTIME、CLOCK_REALTIME 包含系统休眠期间的时间

    物理机器重启测试

    • 重启机器之前,执行测试程序,下面是输出结果
    [wl@ecs-centos-7 my]$ ./main 
    mono_time...sec:2510
    mono_raw_time...sec:2510
    boot_time..sec:2640
    real_time...sec:1586537938
    
    • 执行以下命令重启机器
    [wl@ecs-centos-7 my]$ reboot
    ==== AUTHENTICATING FOR org.freedesktop.login1.reboot ===
    Authentication is required for rebooting the system.
    Authenticating as: root
    Password: 
    ==== AUTHENTICATION COMPLETE ===
    
    • 重启完成,执行测试程序,下面是输出结果
    [wl@ecs-centos-7 my]$ ./main 
    mono_time...sec:41
    mono_raw_time...sec:41
    boot_time..sec:41
    real_time...sec:1586537997
    

    测试结果

    • 重启物理机器对 CLOCK_REALTIME 没有任何影响
    • 重启物理机器后, CLOCK_MONOTONIC、CLOCK_MONOTONIC_RAW、CLOCK_BOOTTIME 时间全部重新计数

    参考

    https://linux.die.net/man/2/clock_gettime
    
  • 相关阅读:
    ajaxfileupload.js类实现异步文件上传
    设计模式代理
    状态码的分析
    图片的格式的分析、 图片优化
    BFC
    css3 的新属性
    二列布局、三列布局总结
    position的定义
    new Vue 发生了什么
    数据驱动
  • 原文地址:https://www.cnblogs.com/wanng/p/12677441.html
Copyright © 2011-2022 走看看