linux获取系统启动时间

简介:

1、前言

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

2、sysinfo结构

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

复制代码
 1 struct sysinfo {
 2                long uptime;             /* Seconds since boot */
 3                unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
 4                unsigned long totalram;  /* Total usable main memory size */
 5                unsigned long freeram;   /* Available memory size */
 6                unsigned long sharedram; /* Amount of shared memory */
 7                unsigned long bufferram; /* Memory used by buffers */
 8                unsigned long totalswap; /* Total swap space size */
 9                unsigned long freeswap;  /* swap space still available */
10                unsigned short procs;    /* Number of current processes */
11                char _f[22];             /* Pads structure to 64 bytes */
12            };
复制代码

3、获取系统启动时间

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

复制代码
 1 #include <stdio.h>
 2 #include <sys/sysinfo.h>
 3 #include <time.h>
 4 #include <errno.h>
 5 
 6 static int print_system_boot_time()
 7 {
 8     struct sysinfo info;
 9     time_t cur_time = 0;
10     time_t boot_time = 0;
11     struct tm *ptm = NULL;
12     if (sysinfo(&info)) {
13     fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s\n",
14         errno, strerror(errno));
15     return -1;
16     }
17     time(&cur_time);
18     if (cur_time > info.uptime) {
19     boot_time = cur_time - info.uptime;
20     }
21     else {
22     boot_time = info.uptime - cur_time;
23     }
24     ptm = gmtime(&boot_time);
25     printf("System boot time: %d-%-d-%d %d:%d:%d\n", ptm->tm_year + 1900,
26         ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
27    return 0; 
28 }
29 
30 int main()
31 {
32     if (print_system_boot_time() != 0) {
33     return -1;
34     }
35     return 0;
36 }
复制代码

测试结果如下所:

相关文章
|
6天前
|
资源调度 JavaScript 搜索推荐
Linux系统之部署envlinks极简个人导航页
【4月更文挑战第11天】Linux系统之部署envlinks极简个人导航页
40 2
|
9天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
58 0
|
9天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
26 0
|
16天前
|
存储 前端开发 Linux
Linux系统之部署ToDoList任务管理工具
【4月更文挑战第1天】Linux系统之部署ToDoList任务管理工具
61 1
|
17天前
|
存储 传感器 运维
linux系统资源统计工具
【4月更文挑战第1天】Linux系统监控工具如dstat、htop、glances、vmstat、top、iostat、mpstat、sar和atop,用于跟踪CPU、内存、磁盘I/O、网络和进程性能。这些工具提供实时、交互式和历史数据分析,助力管理员优化系统性能和故障排查。例如,dstat是vmstat等工具的增强版,htop提供彩色界面的进程管理,而atop则结合了多种功能并记录历史数据。
27 5
linux系统资源统计工具
|
7天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
20 6
|
17天前
|
Ubuntu 架构师 Java
Linux系统常用命令非常详细建议收藏
Linux系统常用命令非常详细建议收藏
48 0
|
23天前
|
存储 算法 Linux
【Linux系统编程】Linux 文件系统探究:深入理解 struct dirent、DIR 和 struct stat结构
【Linux系统编程】Linux 文件系统探究:深入理解 struct dirent、DIR 和 struct stat结构
36 0
|
2天前
|
运维 网络协议 Unix
18.系统知识-Linux常用命令
18.系统知识-Linux常用命令
|
11天前
|
Prometheus 监控 Cloud Native
【Linux】查看系统内存命令(详细讲解)
【Linux】查看系统内存命令(详细讲解)