linux /proc/cpuinfo /proc/meminfo... 杂项收集

本文介绍了一种通过C语言程序获取嵌入式系统的内存和CPU信息的方法,并展示了如何读取系统文件来获取这些信息。此外,还介绍了如何使用sysinfo结构体来获取系统资源信息,以及如何查看当前系统打开的文件句柄数量和应用程序所使用的设备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 内存信息举例

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.系统内存、CPU信息

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. 获取系统中打开的文件句柄个数

[root@szclou /proc/sys/fs]#ls
aio-max-nr           file-nr              nr_open              protected_symlinks
aio-nr               inode-nr             overflowgid          suid_dumpable
dentry-state         inode-state          overflowuid
epoll                lease-break-time     pipe-max-size
file-max             leases-enable        protected_hardlinks

其中file-nr的内容如下

[root@szclou /proc/sys/fs]#cat file-nr 
85      0       8192

参数分解:

85--表示当前系统打开的文件描述符fd

0--???

8192--表示当前支持最大的文件描述符fd

8. 文件系统查看应用程序打开的设备

列举进程

[root@szclou /proc]#ps
PID   USER     COMMAND
    1 root     init
    2 root     [kthreadd]
    3 root     [ksoftirqd/0]
    4 root     [kworker/0:0]
    5 root     [kworker/0:0H]
    6 root     [kworker/u2:0]
    7 root     [rcu_preempt]
    8 root     [rcu_bh]
    9 root     [rcu_sched]
   10 root     [khelper]
   11 root     [kdevtmpfs]
   12 root     [netns]
  147 root     [writeback]
  150 root     [bioset]
  151 root     [crypto]
  153 root     [kblockd]
  165 root     [khubd]
  265 root     [kswapd0]
  408 root     [nuc970-spi0]
  412 root     [nuc970-spi1]
  470 root     [deferwq]
  471 root     [kworker/0:1H]
  486 root     [yaffs-bg-1]
  488 root     [yaffs-bg-1]
  493 root     /sbin/syslogd -n
  496 root     /sbin/klogd -n
  519 root     /usr/sbin/telnetd -F
  520 root     -/bin/sh
  529 root     tcpsvd 0 21 ftpd -w /mnt
  536 root     {.config} /bin/sh /mnt/app/.config
  546 root     [yaffs-bg-1]
  584 root     [kworker/u2:2]
  640 root     /clou/clmain
  903 root     [kworker/0:2]
 1132 root     -sh
 1564 root     [kworker/u2:1]
 2005 root     [kworker/u2:3]
 2072 root     ps

可以看到640进程,进入该目录,在该目录下有个task,进入到里面,看到很多目录,这些目录分别代表不同的进程号,继续进入640

[root@szclou /proc/640/task]#ls
640  659  660  661  662  663  664  665  666  667  668  669  670  671  672  673  674  676  678  679
[root@szclou /proc/640/task/640]#ls
auxv           cpuset         fdinfo         mounts         pagemap        stat
cgroup         cwd            limits         ns             personality    statm
clear_refs     environ        maps           oom_adj        root           status
cmdline        exe            mem            oom_score      sched          syscall
comm           fd             mountinfo      oom_score_adj  smaps          wchan

在该目录下有个fd目录,进入到里面,列举如下信息

[root@szclou /proc/640/task/640/fd]#ls
0   1   10  11  12  13  14  15  16  17  19  2   3   4   5   6   7   8   9
[root@szclou /proc/640/task/640/fd]#ls -la
total 0
dr-x------    2 root     root             0 Jun 10 10:44 .
dr-xr-xr-x    5 root     root             0 Jun 10 10:44 ..
lrwx------    1 root     root            64 Jun 10 10:44 0 -> /dev/console
lrwx------    1 root     root            64 Jun 10 10:44 1 -> /dev/console
lrwx------    1 root     root            64 Jun 10 10:44 10 -> /dev/nuc972_timer
lr-x------    1 root     root            64 Jun 10 10:44 11 -> /dev/nuc972_key
lrwx------    1 root     root            64 Jun 10 10:44 12 -> /dev/nuc972_adc
lrwx------    1 root     root            64 Jun 10 10:44 13 -> /dev/nuc972_timer
lrwx------    1 root     root            64 Jun 10 10:44 14 -> /dev/nuc972_adc
lrwx------    1 root     root            64 Jun 10 10:44 15 -> /dev/esam
lrwx------    1 root     root            64 Jun 10 10:44 16 -> /dev/nuc972_timer
lrwx------    1 root     root            64 Jun 10 10:44 17 -> /dev/ttyS6
lrwx------    1 root     root            64 Jun 10 10:44 19 -> socket:[2128]
lrwx------    1 root     root            64 Jun 10 10:44 2 -> /dev/console
lrwx------    1 root     root            64 Jun 10 10:44 3 -> socket:[1987]
lrwx------    1 root     root            64 Jun 10 10:44 4 -> /sys/devices/platform/nuc970-i2c0/i2c-0/0-0050/fram
lrwx------    1 root     root            64 Jun 10 10:44 5 -> /dev/ttyS5
lrwx------    1 root     root            64 Jun 10 10:44 6 -> /dev/ttyS10
lrwx------    1 root     root            64 Jun 10 10:44 7 -> /dev/ttyS2
lrwx------    1 root     root            64 Jun 10 10:44 8 -> /dev/nuc972_lcd
lrwx------    1 root     root            64 Jun 10 10:44 9 -> /dev/att7022
可以看到当前应用程序打开的设备。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值