Linux上的/proc目录是一种文件系统,称为proc文件系统(虚拟文件系统),它存储内核状态信息,包括cpu、内存以及进程等信息。proc文件系统有很多优点:应用程序获取内核数据不用切换到内核态,增加了系统的安全性(像ps命令就是通过proc获取进程信息);应用程序可以通过proc直接改变内核参数,这样不用重新编译内核就可以改变和优化内核行为。总之,proc为用户应用程序获取系统内部信息提供了一个安全、方便的界面。proc存在内存中,不占用外存。
下面是/proc目录下的文件:
apm | 高级电源管理信息 |
cmdline | 内核命令行 |
Cpuinfo | 关于 Cpu 信息 |
Devices | 可以用到的设备(块设备/字符设备) |
Dma | Used DMS channels |
Filesystems | 支持的文件系统 |
Interrupts | 中断的使用 |
Ioports | I/O 端口的使用 |
Kcore | 内核核心印象 |
Kmsg | 内核消息 |
Ksyms | 内核符号表 |
Loadavg | 负载均衡 |
Locks | 内核锁 |
Meminfo | 内存信息 |
Misc | Miscellaneous |
Modules | 加载模块列表 |
Mounts | 加载的文件系统 |
Partitions | 系统识别的分区表 |
Rtc | Real time clock |
Slabinfo | Slab pool info |
Stat | 全面统计状态表 |
Swaps | 对换空间的利用情况 |
Version | 内核版本 |
Uptime | 系统正常运行时间 |
下面是我自己写得一个查看cpu和内核版本信息以及启动时间的程序:
因为要涉及文件读写,先介绍一下几个文件读写函数。
1)fgetc从流中读取一个字符,并增加文件指针的位置,常与EOF配合使用,将流中的字符全部读出。
2)putchar向终端输出一个字符。
3)fgets(char *s, int size, FILE *stream),从文件流中读取一行到s中,size一般为数组s的大小,且s以字符‘/0‘结束。
4)int feof(FILE *stream)判断是否遇到文件结束,如果遇到则返回非零值,否则为0。
5)int fscanf(FILE *stream, const char *format, ...)从文件流(或标准输入)中进行格式化输入,用法如下:
#include<stdio.h> int main() { int i; char s[5]; fscanf(stdin, "%d %5[a-z]%*s", &i, s); printf("%d %s \n", i, s); return 0; }
执行结果(命令行输入99 abcdefghijk,由于fscanf遇到空格和换行时结束,则99存到i,abcde存到s):
#include<stdio.h> #include<sys/time.h> #define LB_SIZE 80 enum TYPE{STANDARD, SHORT, LONG}; FILE *thisProcFile; //Proc open file pointer struct timeval now; //system time date enum TYPE reportType; //the type of observation report char repTypeName[16]; char *lineBuf; //read out buffer int interval; //system overload detect interval int duration; //system overload detect duration int iteration; char c1, c2; //character handle uint void getTwoTime(FILE *fp) { long uptime, idletime; int day, hour,minute, second; int m, n; char temp[80]; m = n = 0; int ret; int i = 0; char s[100][100]; int j; while((ret=fscanf(fp, "%s", s[i])) != EOF) i++; //print for(j = 0; j < i; j++) { printf("%s\n", s[j]); } uptime = atol(s[0]); idletime = atol(s[1]); printf("<<---------------------___________------------------------->\n"); printf("the uptime of system -----------------------------> %ld\n", uptime); printf("the idletime of process -----------------------------> %ld\n", idletime); printf("<<---------------------___________------------------------->\n"); /* time_t uptime_timet = uptime; printf("xixixixixi%ld\n", uptime_timet); char *result = ctime(&uptime_timet); printf("hahahahahahah %s\n", result); */ int days; int hours; int minutes; int seconds; //uptime of system days = (int)uptime/86400; hours = (int)uptime%86400/3600; minutes = (int)uptime%3600/60; seconds = (int)uptime%3600%60; printf("the uptime of system-------------days %d----hours %d-----minutes %d-----seconds %d----\n", days, hours, minutes, seconds); //idletime days = (int)idletime/86400; hours = (int)idletime%86400/3600; minutes = (int)idletime%3600/60; seconds = (int)idletime%3600%60; printf("the idletime of system-------------days %d----hours %d-----minutes %d-----seconds %d----\n", days, hours, minutes, seconds); } //get time from starting void sampleTime() { //open timer file FILE *fp; if((fp=fopen("/proc/uptime", "r")) == NULL) { printf("not open /proc/uptime"); exit(0); } getTwoTime(fp); fclose(fp); } void getCPUinfo() { FILE *cpuinfo; char ch; if((cpuinfo=fopen("/proc/cpuinfo","r")) == NULL) { printf("not open /proc/cpuinfo"); exit(0); } /* while((ch=fgetc(cpuinfo)) != EOF) putchar(ch); fclose(cpuinfo); */ printf("*********************cpu*******************\n"); while(!feof(cpuinfo)) { fgets(lineBuf, LB_SIZE+1, cpuinfo); printf("%s", lineBuf); } fclose(cpuinfo); } void getKernelVersion() { FILE *version; char ch; if((version=fopen("/proc/version","r")) == NULL) { printf("not open /proc/version"); exit(0); } printf("*****************version*************************\n"); while(!feof(version)) { fgets(lineBuf, LB_SIZE+1, version); printf("%s", lineBuf); } fclose(version); } int main(int argc, char *argv[]) { lineBuf = (char *)malloc(LB_SIZE + 1); reportType = STANDARD; strcpy(repTypeName, "Standard"); if(argc > 1) { sscanf(argv[1], "%c%c", &c1, &c2); if(c1 != ‘-‘) exit(1); if(c2 == ‘b‘) { printf("********************************Memory Info*********************************\n"); //open memory info FILE *meminfo; char ch; if((meminfo=fopen("/proc/meminfo","r")) == NULL) { printf("not open /proc/meminfo"); exit(0); } while((ch=fgetc(meminfo)) != EOF) putchar(ch); fclose(meminfo); printf("********************************TIME*********************************\n"); //cat the start time sampleTime(); } else if(c2 == ‘c‘) { //get cpu info and kernel version printf("*************************CPU & kernel*************************\n"); getCPUinfo(); getKernelVersion(); } } }执行:
./main -b 查看内存信息以及经过格式化的启动时间。
./main -c查看cpu类型和kernel版本。