小编典典

我需要调用什么API才能使系统正常运行?

linux

我想从基于linux的系统上运行的C应用程序中获得系统正常运行时间。我不想调用uptime(1)并解析输出,我想调用我怀疑存在的基础C
API。任何人都知道是否有这样的电话,或者uptime(1)只是处理从wtmp获得的记录吗?


阅读 270

收藏
2020-06-02

共1个答案

小编典典

您要查找的系统调用是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 */
};

玩得开心!

2020-06-02