proc文件系统

来源:互联网 发布:无人机用什么语言编程 编辑:程序博客网 时间:2024/05/21 02:34

proc文件系统是一个虚拟的文件系统,用使用man proc命令上对proc的描述来说就是一个伪文件系统,它提供了一些内核数据结构的接口。通常该文件系统被挂载在/proc目录下,通常情况下他是只读的,但是一些文件也允许一些内核变量去修改。

下面我列举几个例子:

//获取CPU的信息cat /proc/couinfo

这里是我的一些输出:
这里写图片描述

//获取内存信息cat /proc/meminfo

下面是输出:
这里写图片描述

proc文件系统中还有很多可以获取一些有用信息的文件
可以使用man proc对proc文件系统进行一个整体的了解。

下面基于对proc文件系统的了解,我使用C语言来获取当前系统中的一些信息,包括:
| 问题A |
| 1:CPU类型 |
| 2:内核版本 |
| 问题B |
| 1:系统运行时间 |
| 问题C |
| 1:cpu状态 |
| 2:磁盘请求数 |
| 3:上下文切换次数 |
| 4:进程总数 |
| 问题D |
| 1:内存总量 |
| 2:可用内存 |
| 3:系统平均负荷 |

下面是我的代码:

/**********************************************    Function    : 观察 linux 内核行为/***********************************************/#include <stdio.h>#include <sys/time.h>#include <string.h>#include <stdlib.h>//文件指针,用于读取proc文件系统中的一些文件FILE *thisProcFile;  //获取CPU类型void CPUinfo(){    char s[1000];    char gu[10] = "model name";    //打开文件并进行读取    if((thisProcFile = fopen("/proc/cpuinfo","r")) == NULL)    {        printf("%s\n","Open file failed!!");        exit(0);        }    while(fgets(s,100,thisProcFile) != NULL)    {        char *temp = strtok(s,":");        if(strncmp(s,gu,10) == 0)        {            //以":"为分割符将字符串分割开来            temp = strtok(NULL,":");            printf("CPU类型: %s\n",temp);            fclose(thisProcFile);            return;        }    }}//获取内核版本void Kernel_version(){    char s[1000];    if((thisProcFile = fopen("/proc/version","r")) == NULL)    {        printf("%s\n","Open file failed!!");        exit(0);        }    printf("内核版本信息为: \n");    while(!feof(thisProcFile))    {        if(fgets(s,100,thisProcFile) != NULL)            printf("%s",s);    }    fclose(thisProcFile);}//获取系统运行时间void time_run(){    char s[100];    if((thisProcFile = fopen("/proc/uptime","r")) == NULL)    {        printf("%s\n","Open file failed!!");        exit(0);        }    if(fgets(s,100,thisProcFile) != NULL)    {        char *temp = strtok(s," ");        int times = atoi(temp);        //进行时间的转换        int run_days = times / 86400;        int run_hours = (times % 86400) / 3600;        int run_minutes = (times % 3600) / 60;        int run_seconds = (times % 60);        printf("系统运行时间为: %d天%d时%d分%d秒\n",run_days,run_hours,run_minutes,run_seconds);    }    fclose(thisProcFile);}void sampleLoadAvg(){ //观察系统负荷    char s[100];    if((thisProcFile = fopen("/proc/loadavg","r")) == NULL)    {        printf("%s\n","Open file failed!!");        exit(0);        }    fgets(s,100,thisProcFile);    fclose(thisProcFile);    char *temp = strtok(s," ");    char *array[5];    int m = 0;    while(temp != NULL){        array[m] = temp;        temp = strtok(NULL," ");        m++;    }    printf("一分钟之内的平均进程数为: %s\n",array[0]);    printf("五分钟之内的平均进程数为: %s\n",array[1]);    printf("十五分钟之内的平均进程数为: %s\n",array[2]);    printf("正在运行进程数/进程总数: %s\n",array[3]);    printf("最近运行的进程ID: %s\n",array[4]);    fclose(thisProcFile);}//获取CPU状态void cpu_state(){    char s[1000];    if((thisProcFile = fopen("/proc/stat","r")) == NULL)    {        printf("%s\n","Open file failed!!");        exit(0);        }    if(fgets(s,1000,thisProcFile) != NULL)    {        char *temp = strtok(s," ");        char *array[11];        int m = 0;        while(temp != NULL){            array[m] = temp;            temp = strtok(NULL," ");            m++;        }        printf("CPU执行用户态所用的时间: %s jiffies\n",array[1]);        printf("CPU执行系统态所用的时间: %s jiffies\n",array[3]);        printf("CPU执行空闲态所用的时间: %s jiffies\n",array[4]);    }    fclose(thisProcFile);}//获取上下文切换次数void ctxt(){    char s[1000];    char gu[4] = "ctxt";    if((thisProcFile = fopen("/proc/stat","r")) == NULL)    {        printf("%s\n","Open file failed!!");        exit(0);        }    while(fgets(s,100,thisProcFile) != NULL)    {        char *temp = strtok(s," ");        if(strncmp(s,gu,4) == 0)        {            temp = strtok(NULL," ");            printf("上下文切换的次数为: %s\n",temp);            fclose(thisProcFile);            return;        }    }}//获取创建的进程数void processes(){    char s[1000];    char gu[9] = "processes";    if((thisProcFile = fopen("/proc/stat","r")) == NULL)    {        printf("%s\n","Open file failed!!");        exit(0);        }    while(fgets(s,100,thisProcFile) != NULL)    {        char *temp = strtok(s," ");        if(strncmp(s,gu,9) == 0)        {            temp = strtok(NULL," ");            printf("创建的进程数为: %s\n",temp);            fclose(thisProcFile);            return;        }    }}//获取内存总量void MemTotal(){    char s[100];    char gu[8] = "MemTotal";    if((thisProcFile = fopen("/proc/meminfo","r")) == NULL)    {        printf("%s\n","Open file failed!!");        exit(0);        }    while(fgets(s,100,thisProcFile) != NULL)    {        char *temp = strtok(s,":");        if(strncmp(s,gu,8) == 0)        {            temp = strtok(NULL,":");            printf("内存总量为: %s\n",temp);            fclose(thisProcFile);            return;        }    }}//获取活动内存void MemFree(){    char s[100];    char gu[7] = "MemFree";    if((thisProcFile = fopen("/proc/meminfo","r")) == NULL)    {        printf("%s\n","Open file failed!!");        exit(0);        }    while(fgets(s,100,thisProcFile) != NULL)    {        char *temp = strtok(s,":");        if(strncmp(s,gu,7) == 0)        {            temp = strtok(NULL,":");            printf("可用内存为: %s\n",temp);            fclose(thisProcFile);            return;        }    }}int main(int argc,char *argv[]){    //sampleLoadAvg();  均衡负载    //CPUinfo(); // CPU信息    //Kernel_version();  //内核版本信息    //time_run(); //获取系统运行时间    //cpu_state();  //获取CPU状态    //ctxt();    //processes();    //MemTotal();    //MemFree();    char in;    printf("=========================================\n");    printf("|     请输入要查看的问题:                |\n");    printf("|     问题A                              |\n");    printf("|       1:CPU类型                       |\n");    printf("|       2:内核版本                      |\n");    printf("|     问题B                              |\n");    printf("|       1:系统运行时间                  |\n");    printf("|     问题C                              |\n");    printf("|       1:cpu状态                       |\n");    printf("|       2:磁盘请求数                    |\n");    printf("|       3:上下文切换次数                |\n");    printf("|       4:进程总数                     |\n");    printf("|     问题D                             |\n");    printf("|       1:内存总量                     |\n");    printf("|       2:可用内存                     |\n");    printf("|       3:系统平均负荷                 |\n");    printf("=========================================\n");    scanf("%c",&in);    if(in == 'A'){        CPUinfo();        Kernel_version();  //内核版本信息    }else if(in == 'B'){        time_run(); //获取系统运行时间    }else if(in == 'C'){        cpu_state();  //获取CPU状态        ctxt();        processes();    }else if(in == 'D'){        MemTotal();  //内存总量        MemFree();  //可用内存        sampleLoadAvg();  //系统平均负载    }}

下面是我的几个输出截图:

程序运行提示
查看CPU类型和内核版本
查看系统运行时间
查看问题C
查看问题D

0 0