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

摘要:
Linux内核使用一个名为jiffes的常量来计算时间戳。今天,我们需要获取应用程序中系统的启动时间。百度看了一下,我们可以通过sysinfo中的正常运行时间来计算系统的启动时间。Mansyinfo获得以下结果:structsysinfo{longuptime;/*秒sinceboot*/unsigned longloads[3] ;/* 1,5和15分钟平均负荷*/无符号长总图;/*总问题内存大小*/未签名长freeram;/*可用问题内存大小*/未签名的共享内存;/*Amountofs共享内存*/unsignedlongbufferram;/*内存由缓冲区*/未签名的longtotalswap;/*总swapspacesize*/unsignedlongfreeswap;/*swapspace仍然可用*/unsignedshortprocs;/*当前进程数*/char_ f[22];/*Padstructureto64bytes*/};3、 获取系统启动时间获取系统通过sysinfo启动后的秒数。从当前时间中减去此秒数以获得系统启动时间。
获取系统启动时间

一、前言

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

二、sysinfo结构

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

struct sysinfo {
               long uptime;             /* Seconds since boot */
               unsigned long loads[3];  /* 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[22];             /* 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 = 0;
    time_t boot_time = 0;
    struct tm *ptm = NULL;
    if (sysinfo(&info)) {
      fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s
", errno, strerror(errno));
      return -1;
    }
    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
", ptm->tm_year + 1900, ptm->tm_mon + 1, 
        ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec); return 0; } int main() { if (print_system_boot_time() != 0) {   return -1; } return 0; }

测试结果如下所:

linux --&gt; 获取系统启动时间第1张

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

免责声明:文章转载自《linux --&amp;gt; 获取系统启动时间》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇小记 TypeScript 中的循环引用问题web常用自动化库——selenium总结下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vi快捷操作 vim配置【shell文件格式从windows转换为linux】

vim配置 http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html gg 首行 dd 删除当前行 :.,$d  删除全部内容 :set number 显示行号 SHELL syntax error:unexpected end of file 提示错误 DOS下文件和Linux下文件格式差...

树莓派OpenEuler安装

目录 树莓派openEuler安装 安装准备 获取安装源 镜像完整性校验 简介 前提条件 操作指导 安装要求 硬件兼容支持 最小硬件要求 刷写镜像 登录 配置系统 更新系统软件 管理用户 扩展根目录分区 连接 WIFI(可选) 安装桌面(可选) UKUI 桌面环境 XFCE 桌面环境 结语 树莓派openEule...

Docker安全扫描工具之Anchore

本篇简单介绍一款Docker安全扫描工具Anchore的安装和使用。 前言 下述过程是在CentOS 7.6的虚拟机上进行的。 [root@localhost ~]# cat /etc/redhat-release...

Linux系统重启network服务失败

  问题描述使用KVM通过修改配置文件配置好网卡IP,使用命令行service network restart 重启网络服务失败。 如图: 使用图形化管理工具配置IP,在系统界面右上角可以看到网卡状态为未连接,配置好IP以后使用service network restart命令时报同样的错。 如图: 告警信息使用ping命令查看路由是否通畅,系统提...

V3s录音 交叉编译alsa linux

1.下载alsa-lib和alsa-utils  www.alsa-project.org/main/index.php/Download 2.编译alsa-lib tar xvf alsa-lib-1.1.5.tar.bz2 #解压 sync cd alsa-lib-1.1.5/ MyDIR="/usr/local/alsa" ./configure...

linux MD5 SHA1 等 文件校验方法

为解决官方发布的软件包被别人更改或者软件在传输过程中出现传输错误等问题,软件官方在提供软件包的同时,还提供一个保存MD5校验码的文件。 Linux/unix中可以使用 md5sum 文件名   sha1sum 文件名 将会直接输出校验值 也可以将校验值存入文件夹中 md5sum 文件名  > 文件名.txt sha1sum 文件名  > 文件名...