Linux核心實驗(一):proc檔案系統__Linux

來源:互聯網
上載者:User
一、引言

Proc檔案系統,以檔案系統的形式向使用者提供系統目前狀態,動態地從同核心中讀出所需的資訊,只存在記憶體中,不佔用外存空間。 二、實驗內容

問題A:
1、cpu類型
2、核心版本

問題B:
1、系統啟動以來的時間,以dd:hh:mm:ss報告

問題C:
1、cpu執行使用者態、系統態、空閑態所用時間
2、多少次磁碟請求
3、多少次環境切換
4、啟動了多少次進程

問題D:
1、記憶體總量
2、可用記憶體
3、系統平均負荷 三、實驗代碼

/******************************************* Proc 檔案系統** Copyright: (C) 2018.3.31 by shaomingshan** Compile: gcc -o main main.c** Execute: ./main -a******************************************/#include <stdio.h>#include <sys/time.h>#include <unistd.h>#include <string.h>#include <stdlib.h>#define LB_SIZE 80enum TYPE{STANDARD,SHORT,LONG};FILE *thisProcFile; //Proc 開啟檔案指標struct timeval now; //系統時間日期enum TYPE reportType; //觀察報告類型char repTypeName[16];char *lineBuf; //proc 檔案讀出行緩衝int interval; //系統負荷監測時間間隔int duration; //系統負荷監測時段int iteration;char c1,c2; //字元處理單元void sampleLoadAvg() { //觀察系統負荷    int i=0;    //開啟負荷檔案    if ((thisProcFile = fopen("/proc/loadavg", "r")) == NULL)    {        printf("Open Failed\n");        return;    }    //讀出、處理讀出行,如去除前置空格和無用空格    fgets(lineBuf, LB_SIZE+1, thisProcFile);    char * c3 = strtok(lineBuf, " ");    //將讀出行分出不同欄位,按照欄位的不同含義處理為可閱讀格式    //列印處理好的資訊內容    while (c3 != NULL) {        i ++;        switch (i) {            case 1:                printf("1分鐘內平均負載: ");                break;            case 2:                printf("5分鐘內平均負載: ");                break;            case 3:                printf("15分鐘內平均負載: ");                break;            case 4:                printf("活動進程比: ");                break;            case 5:                printf("最近進程號: ");                break;        }        printf("%s\n", c3);        c3 = strtok(NULL, " ");    }    fclose(thisProcFile);}void sampleTime() {//觀察系統啟動時間    long uptime,idletime;    int day,hour,minute,second;    int i,j;    char temp[80];    i=j=0;    //開啟計時檔案    //讀出、處理讀出行,如去除前置空格和無用空格    thisProcFile = fopen("/proc/uptime", "r");    fgets(lineBuf, LB_SIZE+1, thisProcFile);    //將讀出行分出不同欄位,按照欄位的不同含義處理為可閱讀格式    //將啟動時間的秒數轉換為長整數    uptime = atol(strtok(lineBuf, " "));    //轉換成日時鐘秒    day = uptime/3600/24;    hour = uptime/3600%24;    minute = uptime/60%60;    second = uptime%60;    //列印處理好的資訊內容    printf("uptime: %2ldd:%2ldh:%2ldm:%2lds\n",            day, hour, minute, second);    //將啟動時間的空閑秒數轉換為長整數    idletime = atol(strtok(NULL, " "));    //轉換成日時鐘秒    day = idletime/3600/24;    hour = idletime/3600%24;    minute = idletime/60%60;    second = idletime%60;    //列印處理好的資訊內容    printf("idletime: %2ldd:%2ldh:%2ldm:%2lds\n",            day, hour, minute, second);}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 == 'a'){            //觀察部分 A            printf("**********PART A **********\n");            reportType = SHORT;            strcpy(repTypeName,"Short");            //取出並顯示系統目前時間            //讀出並顯示機器名            //讀出並顯示全部 CPU 資訊            //讀出並顯示系統版本資訊            thisProcFile = fopen("/proc/cpuinfo", "r");            while (!feof(thisProcFile)) {                fgets(lineBuf, LB_SIZE+1, thisProcFile);                if (strstr(lineBuf, "model name")) {                    printf("%s", lineBuf);                    break;                }            }            fclose(thisProcFile);            system("uname -r");        }        else if(c2 == 'b'){            //觀察部分 B            printf("**********PART B **********\n");            //開啟記憶體資訊檔            //讀出檔案全部的內容            //處理並用方便閱讀的格式顯示            //觀察系統啟動時間            sampleTime();        }        else if(c2 == 'c'){            //觀察部分 C            printf("**********PART C**********\n");            //開啟系統狀態資訊檔            thisProcFile = fopen("/proc/stat", "r");            //讀出檔案全部的內容            //處理並用方便閱讀的格式顯示            fgets(lineBuf, LB_SIZE+1, thisProcFile);            while (!feof(thisProcFile)) {                char *output = strtok(lineBuf, " ");                if (!strcmp(output, "cpu")) {                    int i = 0;                    while (i < 4) {                        output = strtok(NULL, " ");                        switch (i) {                            case 0:                                printf("使用者態時間: %s\n", output);                                break;                            case 1:                                break;                            case 2:                                printf("系統態時間: %s\n", output);                                break;                            case 3:                                printf("空閑態時間: %s\n", output);                                break;                        }                        i++;                    }                } else if (!strcmp(output, "intr")) {                    output = strtok(NULL, " ");                    printf("磁碟請求: %s\n", output);                } else if (!strcmp(output, "ctxt")) {                    output = strtok(NULL, " ");                    printf("環境切換: %s\n", output);                } else if (!strcmp(output, "processes")) {                    output = strtok(NULL, " ");                    printf("啟動進程數: %s\n", output);                }                fgets(lineBuf, LB_SIZE+1, thisProcFile);            }               fclose(thisProcFile);        }        else if(c2 == 'd'){            //觀察部分 D            printf("**********PART D **********\n");            if(argc<4){                printf("usage:observer [-b] [-c][-d int dur]\n");                exit(1);            }            thisProcFile = fopen("/proc/meminfo", "r");            int i = 0;            // 記憶體資訊檔            while (i++ < 3) {                fgets(lineBuf, LB_SIZE+1, thisProcFile);                strtok(lineBuf, " ");                char *output = strtok(NULL, " ");                switch (i) {                    case 1:                        printf("總記憶體: ");                        break;                    case 2:                        printf("空閑記憶體:");                        break;                    case 3:                        printf("可用記憶體:");                }                printf("%s\n", output);            }            fclose(thisProcFile);            reportType = LONG;            strcpy(repTypeName,"Long");            //用命令列參數指定的時間段和時間間隔連續的            interval = atol(argv[2]);            duration = atol(argv[3]);            //讀出系統負荷檔案的內容用方便閱讀的格式顯示            for (int i = 0; i < duration / interval; ++i) {                sampleLoadAvg();                // 系統負荷資訊                sleep(interval);            }        }    }}
四、運行結果




如有錯誤請指正

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.