您的位置:首页 > 运维架构 > Linux

linux /proc/cpuinfo /proc/meminfo

2017-12-04 15:48 316 查看
1.头文件

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <linux/rtc.h>
#include <termios.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <pthread.h>
#include <linux/serial.h>


2. 获取内存信息

int GetSystemMemory(int *piTotalMem, int *piFreeMem)
{
char* file = "/proc/meminfo";
FILE *fd;
char name[32];
char line_buff[256] = {0};

fd = fopen (file, "r");
if (fd < 0)
return -1;

//总内存
memset(name, 0, sizeof(name));
memset(line_buff, 0, sizeof(line_buff));
fgets (line_buff, sizeof(line_buff), fd);
sscanf(line_buff, "%s %d", name, piTotalMem);

//剩余内存
memset(name, 0, sizeof(name));
memset(line_buff, 0, sizeof(line_buff));
fgets (line_buff, sizeof(line_buff), fd);
sscanf (line_buff, "%s %d", name, piFreeMem);
fclose(fd);

return 1;
}


3. 获取CPU信息

int GetSystemCpuInfo(char *pszCpuCoreName, char *pszCpuFactoryName)
{
char *pszCmpInfo[2] = {"model name", "Hardware"};
char* file = "/proc/cpuinfo";
FILE *fd;
char name[32];
char line_buff[1024] = {0};
int i;
char *p;

fd = fopen(file, "r");
if (fd < 0)
return -1;

while (fgets (line_buff, sizeof(line_buff), fd) != NULL)
{
for (i=0; i<sizeof(pszCmpInfo)/sizeof(pszCmpInfo[0]); i++)
{
if (memcmp(line_buff, pszCmpInfo[i], strlen(pszCmpInfo[i])) == 0)
{
p = strchr(line_buff, ':')+2;
printf("%s\n", p);
if (i==0)
strcpy(pszCpuCoreName, p);
else
strcpy(pszCpuFactoryName, p);
//printf("i=%d, p=%s, pszCpuCoreName=%s, pszCpuFactoryName=%s.\n", i, p, pszCpuCoreName, pszCpuFactoryName);
}
}
}

fclose(fd);
}


4. main()

void main(void)
{
int iTotalMem, iFreeMem;
char szCpuCoreName[128], szCpuFactoryName[128];

GetSystemMemory(&iTotalMem, &iFreeMem);
printf("iTotalMem=%.02fM, iFreeMem=%.02fM, Use percent=%%%.02f.\n", \
(float)iTotalMem/1024, (float)iFreeMem/1024, (float)(iTotalMem-iFreeMem)/iTotalMem*100);

memset(szCpuCoreName, 0, sizeof(szCpuCoreName));
memset(szCpuFactoryName, 0, sizeof(szCpuFactoryName));
GetSystemCpuInfo(szCpuCoreName, szCpuFactoryName);
printf("szCpuCoreName=%s, szCpuFactoryName=%s.\n", szCpuCoreName, szCpuFactoryName);
}


5.调试信息

iTotalMem=59.04M, iFreeMem=42.37M, Use percent=28.24%.

szCpuCoreName=ARM926EJ-S rev 5 (v5l), szCpuFactoryName=NUC970.


6. struct sysinfo结构体

该结构体是另外一种获取系统资源的结构体,比上面的功能简单!

struct sysinfo {
__kernel_long_t uptime;		/* Seconds since boot */
__kernel_ulong_t loads[3];	/* 1, 5, and 15 minute load averages */
__kernel_ulong_t totalram;	/* Total usable main memory size */
__kernel_ulong_t freeram;	/* Available memory size */
__kernel_ulong_t sharedram;	/* Amount of shared memory */
__kernel_ulong_t bufferram;	/* Memory used by buffers */
__kernel_ulong_t totalswap;	/* Total swap space size */
__kernel_ulong_t freeswap;	/* swap space still available */
__u16 procs;		   	/* Number of current processes */
__u16 pad;		   	/* Explicit padding for m68k */
__kernel_ulong_t totalhigh;	/* Total high memory size */
__kernel_ulong_t freehigh;	/* Available high memory size */
__u32 mem_unit;			/* Memory unit size in bytes */
char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)];	/* Padding: libc5 uses this.. */
};
可参见某位大神的中文注解:点击打开链接

7. 获取系统中打开的文件句柄个数

cat /proc/sys/fs/file-nr
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: