linux 读取cpu和内存信息函数

函数
#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值