linux --> 获取系统启动时间
获取系统启动时间
一、前言
时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同。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 --> 获取系统启动时间的更多相关文章
- linux获取系统启动时间
1.前言 时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同.linux内核里面用一个名为jiffes的常量来计算时间戳.应用层有time.getdaytime等函数.今天需要 ...
- [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& ...
随机推荐
- servlet上传文件报错(二)
1.具体报错如下: java.io.FileNotFoundException: D:\MyEclipse\workspace\FileUpload\WebRoot\upload (拒绝访问.) at ...
- 百度地图JavaScript API本地搜索的结果面板
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- directX根据设备类GUID查询所属的filter
hr = m_pSysDevEnum->CreateClassEnumerator(*clsid, &pEnumCat, 0); ASSERT(SUCCEEDED(hr)); ...
- java 堆 栈 常量池
java 堆中保存new 出来的对象(每个对象都包含一个与之对应的class的信息,[class信息存放在方法区]),堆中分配的内存,有虚拟机的自动垃圾回收器管理,栈内存只对其所属线程可见. java ...
- mini设计模式
创建型 单例模式 饿汉式 public class Singleton () { private static Singleton instance = new Singleton(); privat ...
- GridView中使用 jQuery DatePicker (UpdatePanel)
1.无UpdatePanel 1.代码 <script> $(function () { $('.myDatePickerClass').datepicker({ dateFormat ...
- NOIP2017+停课总结
注意 此文章禁止一切含虚伪内容的评论,违者删除评论 其删除解释权归博主所有 Part1 论yyb NOIP 如何炸裂 前言 离NOIP已有三个星期,忘却的救主快要降临了吧,我正有写一篇总结的必要了.. ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- POJ 1087 A Plug for UNIX (网络流,最大流)
题面 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...
- 1.4 如何在main()方法之前执行输出“hello world”
public class Test{ static{ System.out.println("hello world"); } public static void main(St ...