我想从基于Linux的系统上运行的C应用程序中获得系统正常运行时间。我不想调用uptime(1)并解析输出,我想调用我怀疑存在的底层C API。任何人都知道是否有这样的呼叫,或者正常运行时间(1)只是处理从wtmp获得的记录?我打电话来获得系统正常运行时间的API?
回答
系统调用你要找的是SYSINFO()。
它在sys/sysinfo.h定义
它的签名是: INT SYSINFO(结构SYSINFO *资讯)
由于2.4内核,该结构是这样的:
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 */
unsigned long totalhigh; /* Total high memory size */
unsigned long freehigh; /* Available high memory size */
unsigned int mem_unit; /* Memory unit size in bytes */
char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */
};
玩得开心!
我已经按照bdonlan上面的建议读过/ proc/uptime,但是调用API和读取“文件”正是我想要的。谢谢! – 2009-10-11 06:01:06
是否可以获得纳秒级别的信息(用于正常运行时间)? – Jeyaram 2014-06-10 05:31:22
阅读文件/proc/uptime
并以秒为单位取第一个十进制数作为正常运行时间。
/proc/uptime
This file contains two numbers: the uptime of the system (sec‐
onds), and the amount of time spent in idle process (seconds).
还有clock_gettime(可能需要-lrt
)。我见过的行为(我不会声称它是有保证的),但给出CLOCK_MONOTONIC
,因为clk_id
是它返回给定的struct timespec *
参数中的系统正常运行时间。
#include <stdio.h>
#include <time.h>
int main(int argc, char* argv[]) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
printf("tv_sec=%llu tv_nsec=%llu\n",
(unsigned long long)t.tv_sec,
(unsigned long long)t.tv_nsec);
return 0;
}
不,它不;单调时钟具有任意时期。你联系你的手册就是这样说的。 – 2017-05-17 18:55:52
@BoundaryImposition问题标签为linux,而在Linux中,行为是(并且[仍然是](http://elixir.free-electrons.com/linux/latest/ident/CLOCK_MONOTONIC),正如我所描述的那样)。 POSIX说这个时代是任意的,Linux的实现与正常运行时间相关。参见[kernel/time/posix-timers.c](http://elixir.free-electrons.com/linux/latest/source/kernel/time/posix-timers.c)和[fs/proc/uptime.c ](http://elixir.free-electrons.com/linux/latest/source/fs/proc/uptime.c)。 [这里是一个相关的LKML线程](https://lkml.org/lkml/2014/1/2/39)。 – opello 2017-05-23 03:51:10
您链接到实施文件。这可能随时改变。问题是关于API,API说时代是任意的。任意并不意味着没有一个时代,它只是意味着选择不被依赖。您链接到我的LKML线程实际说明了这一点。 – 2017-05-23 09:19:34
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <grp.h>
int main() {
struct sysinfo sys_info;
struct group* grp;
gid_t gid;
char** users;
int days, hours, mins, x = 1;
system("clear");
printf("\033[4;40m Systems information \033[0;m \n");
if(sysinfo(&sys_info) != 0)
perror("sysinfo");
days = sys_info.uptime/86400;
hours = (sys_info.uptime/3600) - (days * 24);
mins = (sys_info.uptime/60) - (days * 1440) - (hours * 60);
printf("\033[1;33m Uptime: \033[0;36m %ddays, %dhours, %dminutes, %ldseconds \033[0;m \n",
days, hours, mins, sys_info.uptime % 60);
printf("\033[1;33m Load Avgs: \033[0;m 1min(%ld) 5min(%ld) 15min(%ld) \n",
sys_info.loads[0], sys_info.loads[1], sys_info.loads[2]);
printf("\033[1;33m Total Ram: \033[0;m %ldk \t Free: %ldk \n", sys_info.totalram/1024, sys_info.freeram/1024);
printf(" \033[1;33m Shared Ram: \033[0;m %ldk ", sys_info.sharedram/1024);
printf(" Buffered Ram: %ldk \n", sys_info.bufferram/1024);
printf("\033[1;33m Total Swap: \033[0;m %ldk \t Free swap: %ldk \n", sys_info.totalswap/1024, sys_info.freeswap/1024);
printf("\033[1;33m Total High Memory: \033[0;m %ldk Free high memory: %ldk \033[0;m \n", sys_info.totalhigh/1024, sys_info.freehigh/1024);
printf(" \n");
printf("\033[1;44m Total Number of processes: %d \033[0;m \n", sys_info.procs);
gid = getgid();
printf(" Group ID: \033[031m %d", gid);
if((grp = getgrgid(gid)) == NULL) return 1;
printf("\033[0;m Group %s ", grp->gr_name);
printf("\n Users in your group ");
for(users = grp->gr_mem; *users != NULL; users++,++x); printf("%d", ++x);
if(strcmp(grp->gr_passwd,"x") == 0) printf(" Password is protected by shadow file. \n");
else printf("Password: %s ", grp->gr_passwd);
return 0;
}
一个好的答案应该不仅包括代码,还包括一些解释它如何工作以及它为什么回答这个问题。这是特别真实的,因为已经有一个被接受的答案。 – Blackwood 2016-01-07 20:28:06
- 1. 更改Linux时间来欺骗系统正常运行时间
- 2. 我如何获得Linux系统的正常运行?
- 3. 如何在过去24小时内获得系统正常运行时间
- 4. 获得程序正常运行时间
- 5. 从RTC获得正常运行时间
- 6. 最快的方式获得系统正常运行时间在Python在Linux中
- 7. 在Java中获取系统正常运行时间
- 8. 使用ontap API和Java可以查询哪些API以获得系统正常运行时间?
- 9. 谁打电话Linux系统
- 10. Android应用程序来获得系统的电话号码
- 11. 如何在系统运行时获得真正的CPU频率?
- 12. 在Scala.JS有没有打电话来获取我正在运行的平台?
- 13. JavaScript来获得系统双击间隔?
- 14. 获得pid的优先级和正常运行时间? (iOS)
- 15. 如何在Java中获得UNIX正常运行时间?
- 16. linux正常运行时间
- 17. 直接使用系统的时间来获得随机数
- 18. 正在运行的系统
- 19. Javascript,我无法获得video.getCurrentTime()来显示视频时间运行
- 20. 如何在运行时获得ruby系统()调用的STDOUT?
- 21. 如何设置系统来告诉我cron作业是否运行不正常?
- 22. 如何获得来电电话号码?
- 23. ksoap2 android打电话,得到null异常
- 24. 如何检测我在运行Unity的系统上的时间?
- 25. Python中的电子邮件正常运行时间
- 26. 检索电池的正常运行时间为android
- 27. 当我拨打电话与多行插入科尔马我得到的异常
- 28. 运行时立即获得系统输出(更新ProgressBar)Java
- 29. .Net应用程序正常运行时,打破预定时间
- 30. 在我的系统中找不到JavaFX运行时间
是有可能得到的正常运行时间在纳秒???你有没有尝试过 ? – Jeyaram 2014-06-10 05:32:58