基于/proc伪文件系统的读取系统常见内核状态信息
来源:互联网 发布:linux个人目录 编辑:程序博客网 时间:2024/06/05 16:40
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 pointint rel;// the length of read from fileint 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 superluousif(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 optionoption=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;}
当程序不带参数的时候会输出帮助,有语法提示和选项提示。
运行结果:
0 0
- 基于/proc伪文件系统的读取系统常见内核状态信息
- 重要的伪文件系统/proc
- Linux的/proc伪文件系统
- proc伪文件系统
- /proc 伪文件系统
- 通过proc文件系统输出必要的Linux内核信息(上)
- 通过proc文件系统输出必要的Linux内核信息(中)
- 通过proc文件系统输出必要的Linux内核信息(下)
- 通过proc文件系统输出必要的Linux内核信息(上)
- 通过proc文件系统输出必要的Linux内核信息(中)
- 通过proc文件系统输出必要的Linux内核信息(下)
- 内核proc文件系统简介
- proc文件系统读取
- Proc 文件系统信息
- 使用/proc文件系统和内核打交道(2)-确定系统的CPU情况
- /proc虚拟文件系统与系统内核参数修改方法
- Linux系统下的/proc/文件系统
- 通过读proc等方式获取Linux系统状态信息的一些方法
- 6.6 Git 工具 - 子模块
- 客户端主动断开webSocket
- python入门:对象的建立和使用(不涉及继承)
- 自定义View实现联系人快速查找
- 第四章 复合类型(4)
- 基于/proc伪文件系统的读取系统常见内核状态信息
- 第四章 复习题
- Hard 273题 Integer to English Words
- 【PHP】Window7环境下配置PHP7+Apache2.4 环境
- 第四章 编程练习
- python3.4.4 配置opencv3.1 解决pip版本不够的问题
- ARM ELF 镜像结构
- SQLite
- Django模板系统(非常详细)