我想从基于Linux的系统上运行的C应用程序中获得系统正常运行时间.我不想调用uptime(1)并解析输出,我想调用我怀疑存在的底层C API.任何人都知道是否有这样的电话,或者正常运行时间(1)只是处理从wtmp获得的记录?
Kyl*_*ith 37
您正在寻找的系统调用是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 */
};
Run Code Online (Sandbox Code Playgroud)
玩得开心!
Joh*_*han 17
那将是这样的.
#include <stdio.h>
#include <errno.h>
#include <linux/unistd.h> /* for _syscallX macros/related stuff */
#include <linux/kernel.h> /* for struct sysinfo */
#include <sys/sysinfo.h>
long get_uptime()
{
struct sysinfo s_info;
int error = sysinfo(&s_info);
if(error != 0)
{
printf("code error = %d\n", error);
}
return s_info.uptime;
}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅"man sysinfo".
bdo*_*lan 14
读取文件/proc/uptime并将第一个十进制数作为正常运行时间,以秒为单位.
来自man 5 proc:
/proc/uptime
This file contains two numbers: the uptime of the system (sec?
onds), and the amount of time spent in idle process (seconds).
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35833 次 |
| 最近记录: |