我想在基于 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/