搜索
查看: 1207|回复: 0

LinuxProc文件系统实验

[复制链接]

165

主题

5

回帖

1123

积分

金牌会员

积分
1123
发表于 2014-9-18 15:19:46 | 显示全部楼层 |阅读模式

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

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

apm高级电源管理信息
cmdline内核命令行
Cpuinfo关于 Cpu 信息
Devices可以用到的设备(块设备/字符设备)
DmaUsed DMS channels
Filesystems支持的文件系统
Interrupts中断的使用
IoportsI/O 端口的使用
Kcore内核核心印象
Kmsg内核消息
Ksyms内核符号表
Loadavg负载均衡
Locks内核锁
Meminfo内存信息
MiscMiscellaneous
Modules加载模块列表
Mounts加载的文件系统
Partitions系统识别的分区表
RtcReal time clock
SlabinfoSlab 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 uintvoid 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)) != 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 startingvoid 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版本。


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

 
 
大数据行业交流
大数据行业交流
大数据求职招聘
大数据求职招聘
站长电话:
15010106923
微信联系:
hb-0310
站长邮箱:
ab12-120@163.com
大数据中国微信

QQ   

版权所有: Discuz! © 2001-2013 大数据.

GMT+8, 2024-4-29 10:48 , Processed in 0.117750 second(s), 24 queries .

快速回复 返回顶部 返回列表