在linux系统中,我们可以使用sysinfo()获取一些系统统计信息。我们在终端命令行中输入“man 2 sysinfo”即可获取sysinfo()的详细信息,如下图所示。
从上图可以知道,使用uname需要包含头文件"#include <sys/sysinfo.h>"
sysinfo的原型是:int sysinfo(struct sysinfo *info);
参数info:这是个struct sysinfo结构体类型的指针,它指向一个struct sysinfo结构体类型对象。
返回值:成功返回0,失败则返回-1。获取到的信息会保存在info中,我们可以打印info的成员变量来查看相关的信息。
最后再看看struct utsname结构体当中的内容。
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-2sizeof(long)-sizeof(int)]; /* Padding to 64 bytes */
};
需要查看哪个成员变量的信息,只需在调用sysinfo()之后将其打印出来即可。