获取系统启动时间

一、前言

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

二、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 */
};

三、获取系统启动时间

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

#include <stdio.h>
#include <sys/sysinfo.h>
#include <time.h>
#include <errno.h> 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 ;
}

测试结果如下所:

参考:http://www.cnblogs.com/Anker/p/3527609.html

linux --> 获取系统启动时间的更多相关文章

  1. linux获取系统启动时间

    1.前言 时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同.linux内核里面用一个名为jiffes的常量来计算时间戳.应用层有time.getdaytime等函数.今天需要 ...

  2. [Linux] 查看系统启动时间

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

  3. linux 获取系统屏幕分辨率

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

  4. linux查看系统启动时间

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

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

    #include <stdio.h> #include <time.h> #include <sys/time.h> void sysLocalTime() { t ...

  6. 获取Win和Linux系统启动时间,类似uptime功能,用于判断是否修改过系统时间

    目录 前言 测试代码 Win测试 Linux测试 总结 前言 有时候需要判断系统是否有修改过时间,最简单的方法就是获取当前时间A,然后sleep X秒,然后获取 时间B,如果 时间B - 时间A ≠ ...

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

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

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

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

  9. Linux sysinfo获取系统相关信息

    Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...

随机推荐

  1. 【html5】html学习笔记1

    html5语法规则 1.标签要小写 2.省略标签 如 <tr> <td> <tr><td> 3.属性不加” 如 <div  id=div1> ...

  2. 查看dmp文件

    1.查看dmp文件,首先要通过以下的链接地址进行下载 http://msdl.microsoft.com/download/symbols/debuggers/dbg_x86_6.11.1.404.m ...

  3. Error Code: 1175. You are using safe update mode and you tried to update a table

    错误描述 11:14:39 delete from t_analy_yhd Error Code: 1175. You are using safe update mode and you tried ...

  4. OpenStack_I版 4.Dashboard部署

    由python的DjangoWeb框架开发的   使用keystone默认的角色来访问各种服务   Dashboard安装       Dashboard是openstack的Web管理界面,需要将它 ...

  5. 石子归并 51Nod - 1021

    N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的代价.计算将N堆石子合并成一堆的最小代价.   例如: 1 2 3 4,有 ...

  6. 异常-----freemarker.template.TemplateException: Error executing macro: write

    freemarker自定义标签 1.错误描述 六月 05, 2014 11:31:35 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...

  7. Win Form不能响应键盘事件

    在窗体属性中,将KeyPreview设置为true

  8. ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction

    1.Action.RenderAction加载办法的视图,履行Controller → Model → View的次序,然后把产生的页面带回到本来的View中再回传.而Partial.RenderPa ...

  9. CentOS7安装dnf报错:No package dnf available

    1.百度中提供的安装办法 yum install epel-release -y yum install dnf 2. No package dnf available解决办法 运行一下几个命令: w ...

  10. RobotFramework下的http接口自动化Follow Response关键字的使用

    Follow Response 关键字用于处理http中的重定向请求,常见的http 重定向请求包含http code为301和302 两种重定向请求,代表着某个URL地址发生了转移. http co ...