linux sysinfo

linux sysinfo 结构体。
Linux中,sysinfo是用来获取系统相关信息的结构体。
函数声明和原型:
#include
int sysinfo(struct sysinfo *info);

#ifndef _LINUX_KERNEL_H
#define _LINUX_KERNEL_H
    
/*
 * 'kernel.h' contains some often-used function prototypes etc
 */
    
    
#define SI_LOAD_SHIFT   16
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 short pad;     /* explicit padding for m68k */
    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: libc5 uses this.. */
};
    
/* Force a compilation error if condition is true */
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
    
/* Force a compilation error if condition is true, but also produce a
   result (of value 0 and type size_t), so the expression can be used
   e.g. in a structure initializer (or where-ever else comma expressions
   aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
    
/* Trap pasters of __FUNCTION__ at compile-time */
#define __FUNCTION__ (__func__)
    
#endif

Example:

#include <stdio.h>
    
#include <linux/unistd.h>     /* 包含调用 _syscallX 宏等相关信息*/
#include <linux/kernel.h>     /* 包含sysinfo结构体信息*/
    
//_syscall1(int, sysinfo, struct sysinfo*, info);
//using namespace std;
int main(int argc, char *agrv[])
{
struct sysinfo s_info;
int error;
    
error = sysinfo(&s_info);
printf("\n\ncode error=%d\n",error);
printf("Uptime = %ds\nLoad: 1 min%d / 5 min %d / 15 min %d\n"
          "RAM: total %d / free %d /shared%d\n"
          "Memory in buffers = %d\nSwap:total%d/free%d\n"
          "Number of processes = %d\n",
          s_info.uptime, s_info.loads[0],
          s_info.loads[1], s_info.loads[2],
          s_info.totalram, s_info.freeram,
          s_info.totalswap, s_info.freeswap,
         s_info.procs );
   return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>