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 <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 ;
}
测试结果如下所:

linux获取系统启动时间的更多相关文章
- 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 <stdio.h> #include <time.h> #include <sys/time.h> void sysLocalTime() { t ...
- 获取Win和Linux系统启动时间,类似uptime功能,用于判断是否修改过系统时间
目录 前言 测试代码 Win测试 Linux测试 总结 前言 有时候需要判断系统是否有修改过时间,最简单的方法就是获取当前时间A,然后sleep X秒,然后获取 时间B,如果 时间B - 时间A ≠ ...
- 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 <stdio.h> #include <stdlib.h> #include <errno.h& ...
随机推荐
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- Safari 前端开发调试 iOS 完美解决方案
转http://www.2cto.com/kf/201403/283404.html afari 前端开发调试 iOS 完美解决方案 2014-03-05 0个评论 来源:Safari ...
- HEX格式数据转换成十六进制字符串
/** * Hex格式数据转换成十六进制字符串 * @param src */ public void bytesToHexString(byte[] by){ StringBuilder strin ...
- PHPSTORM支持dwt文件设置方法
- Android学习七:new Date使用
1.例子 学习时间函数,并实现了简单的多个按钮监听同一个事件的方法 2.代码 代码很简单,也很清晰 package com.example.datetime; import java.text.Sim ...
- flash cs6导入某些mp3不能的解决办法
安装最新的quicktime 另外还有一个很恶心的办法,可以不用装quicktime. 1.用adobe audio打开一个没问题的mp3, 2.再打开有问题的MP3,全选,复制: 3.切换到没问题的 ...
- 调Windows 7的图片浏览器查看图片
public static void viewPicFromWindows(string pPicPath) { if (pPicPath!="" && Syste ...
- eclipse美化
// */ // ]]> eclipse美化 Table of Contents 1 中文字体 2 皮肤 3 emacs+ 1 中文字体 win7下打开eclipse3.7中文字体很小,简直 ...
- RabbitMQ介绍6 - 其它
深入话题 Exchange实现路由的方法 http://www.rabbitmq.com/blog/2010/09/14/very-fast-and-scalable-topic-routing-pa ...
- time.h-------日期与时间函数
1.clock函数----返回CPU计时单元.函数返回开启这个程序进程到程序中调用clock函数时之间的CPU时钟计时单元(返回毫秒). (计的是占用cpu的时间) 函数原型:long clock() ...