基于/proc伪文件系统的读取系统常见内核状态信息

时间:2021-01-15 16:06:01

linux实验,需要学生掌握linux下文件的读写。我做了一个基于/proc伪文件系统的读取系统内核信息的一个小程序。

读取cpu信息-------/proc/cpuinfo

读取内存信息-------/proc/meminfo

读取挂载设备-----/proc/mounts

读取已经加载的设备并分类---------/proc/devices

读取支持的文件系统--------/proc/filesystems

读取加载的模块----------/proc/modules

读取系统的版本信息-----------/proc/version

启动时传递给kernel的参数----------/proc/cmdline


这个程序用到了自己写的一个按行读取的函数readline

------------------》》》》》》》》》》》》》按行读取的readline


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#define CPU_INFO_FILE "/proc/cpuinfo"
#define MEMORY_INFO_FILE "/proc/meminfo"
#define MOUNTS_INFO_FILE "/proc/mounts"
#define DEVICES_INFO_FILE "/proc/devices"
#define FILESYSTEM_INFO_FILE "/proc/filesystems"
#define MODULES_INFO_FILE "/proc/modules"
#define VERSION_INFO_FILE "/proc/version"
#define CMDLINE_INFO_FILE "/proc/cmdline"

void display_help();
int readline(int fd,char** buff);

int main(int argc,char* argv[])
{
intvfile;//file point
int rel;// the length of read from file
int count=0;
charoption,*buff=NULL;
//charge the number of arguments
//we need a argument to help choose a operate
//if the number of arguments is less two ,there is no arguments
//if the number of arguments is greater than two ,some arguments are superluous
if(argc<2)
{
printf("\033[31m the arguments too less \033[0m \n");
display_help();
exit(1);
}
if(argc>2)
{
printf("\033[31m the arguments too much \033[0m \n");
display_help();
exit(1);
}
//get option
option=argv[1][0];
switch(option)
{
case 'c':
vfile=open(CPU_INFO_FILE,O_RDONLY);
break;
case 'm':
vfile=open(MEMORY_INFO_FILE,O_RDONLY);
break;
case 'n':
vfile=open(MOUNTS_INFO_FILE,O_RDONLY);
break;
case 'd':
vfile=open(DEVICES_INFO_FILE,O_RDONLY);
break;
case 'f':
vfile=open(FILESYSTEM_INFO_FILE,O_RDONLY);
break;
case 'l':
vfile=open(MODULES_INFO_FILE,O_RDONLY);
break;
case 'v':
vfile=open(VERSION_INFO_FILE,O_RDONLY);
break;
case 'e':
vfile=open(CMDLINE_INFO_FILE,O_RDONLY);
break;
case 'h':
display_help();
exit(0);
break;
default:
display_help();
exit(0);
}
if(vfile<0)
{
printf("open file error\n");
exit(1);
}
while((rel=readline(vfile,&buff))>0)
{
printf("%s",buff);
free(buff);
count++;
if(count%10==0)
{
printf(":");
option=getchar();
printf("\033[1A");
if(option=='q')
{
close(vfile);
exit(0);
}

}
}
close(vfile);
return 0;

}

void display_help()
{
printf("--------------------------------\n");
printf("welcome to use the system info viewer\n");
printf("--------------------------------\n");
printf("\e[1mNAME\e[0mget host info\n\n");
printf("\e[1mSYNOPSIS\e[0mgethostinfo option\n\n");
printf("\e[1mOPTION\e[0m\n\n");
printf("\e[1mc\e[0m \nget the cpu info\n");
printf("m\nget the memory info\n");
printf("n\nget the list of mounts info\n");
printf("d\nget the list of devices info\n");
printf("f\nget the list of filesystem supported\n ");
printf("l\nget the modules\n");
printf("v\nget the system version\n");
printf("e\nget the cmdline info\n\n");
printf("h\nhelp\n");
printf("--------------------------------\n");

}

int readline(int fd,char** buff)
{
int rl=-1;
char c;
long maxlength=128;
long count=0;
if(fd<0)
{
printf("open file error\n");
exit(0);
}
*buff=(char*)malloc(sizeof(char)*maxlength);
if(*buff==NULL)
{
printf("allocate memory error");
close(fd);
exit(1);
}
while((rl=read(fd,&c,1))>0)
{
if(count==maxlength)
{
maxlength+=128;
*buff=(char*)realloc(*buff,maxlength);
if(buff==NULL)
{
printf("allocate memory error\n");
close(fd);
exit(0);
}
}
(*buff)[count++]=c;
if(c=='\n' || c==EOF)
{
break;
}

}
(*buff)[count]='\0';
return count;
}


当程序不带参数的时候会输出帮助,有语法提示和选项提示。

基于/proc伪文件系统的读取系统常见内核状态信息


运行结果:

基于/proc伪文件系统的读取系统常见内核状态信息