Linux Proc文件系统实验(代码)

Linux上的/proc目录是一种文件系统,称为proc文件系统(虚拟文件系统),它存储内核状态信息,包括cpu、内存以及进程等信息。proc文件系统有很多优点:应用程序获取内核数据不用切换到内核态,增加了系统的安全性(像ps命令就是通过proc获取进程信息);应用程序可以通过proc直接改变内核参数,这样不用重新编译内核就可以改变和优化内核行为。总之,proc为用户应用程序获取系统内部信息提供了一个安全、方便的界面。proc存在内存中,不占用外存。

下面是/proc目录下的文件:

apm高级电源管理信息

cmdline内核命令行

Cpuinfo关于 Cpu 信息

Devices可以用到的设备(块设备/字符设备)

DmaUsed 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 80enum TYPE{STANDARD, SHORT, LONG};FILE *thisProcFile; //Proc open file pointerstruct timeval now; //system time dateenum TYPE reportType; //the type of observation reportchar repTypeName[16];char *lineBuf; //read out bufferint interval; //system overload detect intervalint duration; //system overload detect durationint 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++;//printfor(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;

记录沿途的心情。那样的生活才是我想要的。

Linux Proc文件系统实验(代码)

相关文章:

你感兴趣的文章:

标签云: