Linux获取系统启动经过时间

记录一下获取从Linux系统启动开始计算的时间的方法。

在ubuntu环境下测试可行。

#include <stdio.h>
#include <time.h>

/************************************************************************
 ** 函数名:     get_sys_runtime
 ** 函数描述:   返回系统运行时间
 ** 参数:       [in]  1 - 秒,2 - 毫秒
 ** 返回:       秒/毫秒
 ************************************************************************/
static long get_sys_runtime(int type)
{
    struct timespec times = {0, 0};
    long time;

    clock_gettime(CLOCK_MONOTONIC, &times);
    printf("CLOCK_MONOTONIC: %lu, %lu\n", times.tv_sec, times.tv_nsec);

    if (1 == type){
        time = times.tv_sec;
    }else{
        time = times.tv_sec * 1000 + times.tv_nsec / 1000000;
    }

    printf("time = %ld\n", time);
    return time;
}

int main(int argc,char *argv[])
{
    long sec, millisecond;
    sec = get_sys_runtime(1);
    millisecond = get_sys_runtime(2);
    printf("sec = %ld, millisecond = %ld\n", sec, millisecond);
    return 0;
}
发布了62 篇原创文章 · 获赞 106 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/lang523493505/article/details/80697240
今日推荐