linux 获取启动时间不对,linux获取系统启动时间

1、前言

时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同。linux内核里面用一个名为jiffes的常量来计算时间戳。应用层有time、getdaytime等函数。今天需要在应用程序获取系统的启动时间,百度了一下,通过sysinfo中的uptime可以计算出系统的启动时间。

2、sysinfo结构

sysinfo结构保持了系统启动后的信息,主要包括启动到现在的时间,可用内存空间、共享内存空间、进程的数目等。man sysinfo得到结果如下所示:

struct sysinfo {

long uptime; /* Seconds since boot */

unsigned long loads[]; /* 1, 5, and 15 minute load averages */

unsigned long totalram; /* Total usable main memory size */

unsigned long freeram; /* Available memory size */

unsigned long sharedram; /* Amount of shared memory */

unsigned long bufferram; /* Memory used by buffers */

unsigned long totalswap; /* Total swap space size */

unsigned long freeswap; /* swap space still available */

unsigned short procs; /* Number of current processes */

char _f[]; /* Pads structure to 64 bytes */

};

3、获取系统启动时间

通过sysinfo获取系统启动到现在的秒数,用当前时间减去这个秒数即系统的启动时间。程序如下所示:

#include

#include

#include

#include

static int print_system_boot_time()

{

struct sysinfo info;

time_t cur_time = ;

time_t boot_time = ;

struct tm *ptm = NULL;

if (sysinfo(&info)) {

fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s\n",

errno, strerror(errno));

return -;

}

time(&cur_time);

if (cur_time > info.uptime) {

boot_time = cur_time - info.uptime;

}

else {

boot_time = info.uptime - cur_time;

}

ptm = gmtime(&boot_time);

printf("System boot time: %d-%-d-%d %d:%d:%d\n", ptm->tm_year + ,

ptm->tm_mon + , ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);

return ;

}

int main()

{

if (print_system_boot_time() != ) {

return -;

}

return ;

}

测试结果如下所:

fbbe605a1c299bf77590337f87eb3e7f.png

linux --> 获取系统启动时间

获取系统启动时间 一.前言 时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同.linux内核里面用一个名为jiffes的常量来计算时间戳.应用层有time.getdaytim ...

[Linux] 查看系统启动时间

查找系统最后启动时间 1. 使用 who 命令 who -b 输出: system boot 2015-10-14 00:51 2. 使用 last 命令 last reboot | head -1 ...

linux 获取系统屏幕分辨率

在Windows下可以使用GetSystemMetrics(SM_CXSCREEN);GetSystemMetrics(SM_CYSCREEN) 获取. 在Linux下可以使用XDisplayWi ...

linux查看系统启动时间

1.uptime命令 felix@felix-computer:~$ uptime :: up :, user, load average: 0.89, 0.74, 1.00 felix@felix- ...

Linux获取系统当前时间(精确到毫秒)

#include #include #include void sysLocalTime() { t ...

Linux下如何查看系统启动时间和运行时间

1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.002.查看/proc/uptime文件计算系 ...

Linux下如何查看系统启动时间和运行时间以及安装时间

1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.00 2.查看/proc/uptime文件计算 ...

Linux sysinfo获取系统相关信息

Linux中,可以用sysinfo来获取系统相关信息. #include #include #include

django 获取系统当前时间 和linux 系统当前时间不一致 问题处理。

问题场景: 在django admin models 实体对象添加一个属性最后修改时间,用户在添加.修改是系统自动修改操作时间. UpdateTime自动获取系统时间.并且自动修改. 代码设置如下. ...

随机推荐

VOC2007检测任务的评估标准

VOC2007数据集使用mAP值作为检测算法检测结果的性能评估得分.mAP意思是mean Average Precision,Precision是指精度,Average Precision是指11个等 ...

发送有序广播Ordered Broadcast

import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.vi ...

Extjs combox的详解

Extjs combox的详解 写了哈extjs当中的combox,第一次写,照着网上的例子抄.在上次的例子中,是实现了,可是有一个重大的错误.也就是自己根本没有理解combox从远程服务器获取数据, ...

Mysql 8个小时连接断开问题解析

wait_timeout — 指的是mysql在关闭一个非交互的连接之前所要等待的秒数,其取值范围为1-2147483(Windows),1-31536000(linux),默认值28800. int ...

Queue 消息的发送与接收(PTP 消息传递模型)

上篇博客写到了JMS两种消息模型(P2P.pub/sub).本篇博客通过一个实例来进一步了解P2P模型. Queue消息的发送与接收--PTP消息传递模型,样例: Q ...

thinkjs—控制器方法名不能大写

async updateInfoAction(){ ... } 上面的接口如果通过ajax访问,就会报404的错误.原因似乎在于访问updateInfo时,会自动转化成小写,而小写的updateinf ...

Nginx 优先选择连接最少的上游服务器

详见陶辉87课 upstream test { server ; server ; least_conn ; zone backends 64k; }

RenderTree渲染树

RenderTree渲染树对类中的静态成员有很重要的关系,这个和多态是有很重要的关系,举个简单的例子,在游戏中,马里奥需要渲染,蘑菇也需要渲染,怪兽也需要渲染,其是串在一个树上的,但是不同的类型怎么将 ...

[BZOJ2863]愤怒的元首

Description: Pty生活在一个奇葩的国家,这个国家有n个城市,编号为1~n. ​ 每个城市到达其他城市的路径都是有向的. ​ 不存在两个城市可以互相到达. 这个国家的元首现在很愤怒,他大喊 ...

STL中经常使用数据结构

STL中经常使用的数据结构: [1]  stack.queue默认的底层实现为deque结构. [2]  deque:用map管理多个size大小的连续内存块,方便头尾插入. [3]  vector: ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值