c - 我应该调用什么 API 来获得系统正常运行时间?

标签 c linux uptime

我想在基于 linux 的系统上运行的 C 应用程序中获取系统正常运行时间。我不想调用 uptime(1) 并解析输出,我想调用我怀疑存在的底层 C API。有谁知道有没有这样的调用,或者 uptime(1) 只是简单地处理从 wtmp 获得的记录?

最佳答案

您要查找的系统调用是 sysinfo()。

在 sys/sysinfo.h 中定义

它的签名是: int sysinfo(struct sysinfo *info)

自内核 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 */
};

玩得开心!

关于c - 我应该调用什么 API 来获得系统正常运行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1540627/

相关文章:

c - __WORDSIZE 在编译中的作用

c++ - 尝试将两个相似的 printf 样式调试消息合并到一个函数中

java - java、linux开发机苹果推送通知服务

php - 使用 PHP 下载大型 "private"文件

emacs - 如何在 emacs 状态栏上显示正常运行时间?我正在寻找要在状态栏上显示的/usr/bin/uptime 输出

c - 是否可以混合使用多个声明/初始化?

使用链表的 malloc() 和 free 的正确方法

linux - 解析 laSTLog 的输出?

php - php 中的 Linux uptime 命令不显示任何内容

linux - 为什么HTOP在正常运行时间旁边显示感叹号