我需要调用什么API才能使系统正常运行? [英] What API do I call to get the system uptime?
问题描述
我想从基于linux的系统上运行的C应用程序中获取系统正常运行时间.我不想调用uptime(1)并解析输出,我想调用我怀疑存在的基础C API.任何人都知道是否有这样的电话,或者uptime(1)只是处理从wtmp获得的记录吗?
I would like to get the system uptime from within a C application running on a linux-based system. I don't want to call uptime(1) and parse the output, I'd like to call the underlying C API I suspect exists. Anyone know if there is such a call, or does uptime(1) simply process records obtained from wtmp?
推荐答案
您要查找的系统调用是sysinfo().
The system call you're looking for is sysinfo().
它在sys/sysinfo.h中定义
It's defined in sys/sysinfo.h
它的签名是: int sysinfo(struct sysinfo * info)
Its signature is: int sysinfo(struct sysinfo *info)
从内核2.4开始,结构看起来像这样:
Since kernel 2.4, the structure has looked like this:
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 */
};
玩得开心!
这篇关于我需要调用什么API才能使系统正常运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!