函数
#include <sys/sysinfo.h>
int sysinfo(struct sysinfo *info);
结构体
在Linux 2.3.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 */
char _f[22]; /* Pads structure to 64 bytes */
};
在Linux 2.3.23(i386), 2.3.48 (all architectures)中,结构体的信息是(单位是mem_unit字节):
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 to 64 bytes */
};
返回
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
本质是读取的/dev/kmem.
sysinfo() provides a simple way of getting overall system statistics.
This is more portable than reading /dev/kmem.
示例
#include <stdio.h>
#include <linux/unistd.h> /* 包含调用 _syscallX 宏等相关信息*/
#include <linux/kernel.h> /* 包含sysinfo结构体信息*/
#include <sys/sysinfo.h>
#include <string.h>
//
int get_info()
{
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(byte): total %d / free %d /shared%d\n"
"Memory in buffers(byte) = %d\nSwap(byte):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 );
}
int main(int argc, char *agrv[])
{
printf("get_info();\n");
get_info();
return 0;
}
方法2
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int agrc, char **argv)
{
char buf[64] = {'\0'};
FILE *Stream;
int MemFree_info;
// popen执行shell命令并保存返回结果内容
// 1、先执行cat /proc/meminfo 获取系统内存信息
// 2、在步骤1的返回结果上运行 grep MemFree 命令查找对应项
// 3、在步骤2的返回结果上运行 awk {'print $2'} 命令打印第二个参数,以空格作为分割,如字符串“MemFree: 1431496 kB”,则$2 为“1431496”
Stream = popen("cat /proc/meminfo | grep MemFree | awk {'print $2'}", "r");
fread(buf, sizeof(char), sizeof(buf), Stream); // 读出Stream文件描述符中的流数据,不足64字节,剩余空间内容不变
MemFree_info = atoi(buf); //注意MemFree_info需要定义int类型,否则赋值出错
printf("MemFree size:%d(kb)\n", MemFree_info);
pclose(Stream);
return 0;
}