鍍金池/ 教程/ Linux/ shell 學習五十八天----/proc 文件系統(tǒng)
shell 學習四十一天----列出文件 ls 和 od 命令
shell 學習小結
shell 學習第二十八天----case 語句
shell 學習四十四天----尋找文件
shell 學習三十五天----波浪號展開與通配符
shell 學習三十八天----執(zhí)行順序和 eval
shell 學習四十八天----文件校驗和匹配
shell 學習四十天----awk 的驚人表現(xiàn)
shell 學習第十一天----sed 正則的精確控制
shell 學習第十七天----awk 命令
shell 學習三十九天----內建命令
shell 學習四十五天----xargs
shell 學習三十七天----引用
shell 學習第十六天----join 練習
shell 學習第二十四天----提取開頭或結尾數(shù)行
shell 學習第九天----分組
shell 學習第五天----基本的 I/O 重定向
shell 學習第十五天----使用 cut 選定字段
shell 學習四十六天----文件系統(tǒng)的空間信息 df 和 du 命令
shell 學習第八天----擴展正則表達式 (ERE)
shell 學習第十九天----文本塊排序
shell 學習第二十一天----重新格式化段落
shell 學習三十六天----命令替換
shell 學習第二十天----sort 的其他內容以及 uniq 命令
shell 學習四十二天----使用 touch 更新文件時間
shell 學習五十一天----top 命令查看進程列表
shell 學習第一天
shell 學習五十四天----進程系統(tǒng)調用的追蹤 strace
shell 學習五十天----查看進程 ps 命令
shell 學習第二十七天----退出狀態(tài)和 if 語句
shell 學習第二十三天----打印
shell 學習三十三天----關于重定向
shell 學習四十九天----進程建立
shell 學習第三十天----break,continue,shift,getopts
shell 學習五十五天----進程記賬
shell 學習總結一
shell 學習第二十六天----變量與算數(shù)
shell 學習第二十二天----計算行數(shù), 字數(shù)以及字符數(shù)
shell 學習五十八天----/proc 文件系統(tǒng)
shell 學習第二十五天----神器的管道符
shell 學習第三十二天----read 讀取一行
sheel 學習第三天
shell 學習----小結
shell 學習第十八天----文本排序
shell 學習第三十一天----函數(shù)問題
shell 學習第十天----sed 查找與替換
shell 學習四十三天----臨時性文件的建立與使用
shell 學習四十七天----文件比較 cmp,diff,patch
shell 學習三十四天----printf 詳解
shell 學習五十七天 ----linux 任務管理,針對上一講的總結和擴展
shell 學習第六天----小結
shell 學習第十三天----sed 案例分析
shell 學習第七天----基礎正則表達式 (BRE)
shell 學習第十二天----行與字符串
shell 學習小結四
shell 學習第二十九天----循環(huán)
shell 學習五十二天----刪除進程 kill 命令
shell 學習五十六天----延遲進程調度
shell 學習第四天----華麗的 printf 輸出
shell 學習第十五天----join 連接字段
shell 學習完結篇----希望你能看到
shell 學習第二天
shell 學習五十三天----捕獲信號 trap

shell 學習五十八天----/proc 文件系統(tǒng)

/proc 文件系統(tǒng)

前言:linux 中的 /proc 文件系統(tǒng),由一組目錄和文件組成,掛載 (mount)/proc 目錄下。 /proc 文件系統(tǒng)是一種虛擬文件系統(tǒng),以文件系統(tǒng)目錄和文件形式,提供一個指向內核數(shù)據(jù)結構的接口。 這為查看和改變各種系統(tǒng)屬性開啟了方便之門。 此外,還能通過一組以 /proc/PID 形式命名的目錄 (PID 是進程的 ID) 查看系統(tǒng)匯總運行各進程的相關信息。

通常,/proc 目錄下的文件內容都采取可讀的文本形式,shell 腳本也能對其進行解析。 程序可以打開,讀取和寫入 /proc 目錄下的既定文件。 大多數(shù)情況下,只有特權進程才能修改 /proc 目錄下的文件內容。

  1. proc 文件系統(tǒng)初步

    • /proc 文件系統(tǒng) /proc 文件系統(tǒng)是一種特殊的,由軟件創(chuàng)建的文件系統(tǒng),內核使用它向外界到處信息。 /proc 下面的每個文件都綁定一個內核文件,用戶讀取其中的文件時,該函數(shù)動態(tài)的生成文件的“內容”。

      由于 `/proc` 文件系統(tǒng)已經(jīng)被添加了大量的信息。因此,最好的辦法是使用 `sysfs` 而不是 `/proc` 文件系統(tǒng)想歪導出信息。
      
      `/proc` 文件不僅可以用于讀數(shù)據(jù),也可以用于寫數(shù)據(jù),不過寫數(shù)據(jù)比較麻煩一些,這里只描述數(shù)據(jù)的用法。 寫數(shù)據(jù)的方法可以在看完讀數(shù)據(jù)的過程后參考 kernel 源碼
    • 創(chuàng)建 /proc 文件的函數(shù) 前面說了 /proc 下的文件都是在訪問實時生成文件內容的,那么為了創(chuàng)建 /proc 下的一個只讀的文件,我們必須實現(xiàn)一個函數(shù)用于在讀取文件時生成數(shù)據(jù),萬幸,該函數(shù)接口設計好了,我們只要按照函數(shù)接口實現(xiàn)自己需要的功能就可以了。 函數(shù)原型如下:
      int (*read_proc)(char *page,char **start,off_t offset,int count,int *eof,void *data);

      參數(shù)說明:
      ```參數(shù)名
      說明
      page
      用來寫入數(shù)據(jù)的緩沖區(qū); 也就是說從 /proc 文件中獨到的數(shù)據(jù)都寫入到 page 指向的緩沖區(qū)中
      start
      用于指定事跡的數(shù)據(jù)寫入到 page 指向的內存也的具體的那個位置
      offset
      和 read 函數(shù)中的參數(shù)意義相同
      count
      和 read 函數(shù)中的參數(shù)意義相同
      eof
      當沒有數(shù)據(jù)返回時,必須設置該參數(shù)為一個整數(shù),例如:*eof=1;
      data
      該參數(shù)是內核提供給驅動程序的專用指針,可以用于內部記錄```
      
      **創(chuàng)建制度的 `/proc` 文件的函數(shù)**
      ```struct proc_dir_entry *create_proc_read_entry(const char *name,mode_t mode,struct proc_dir_entry *base,read_proc_t *read_proc,void * data)``` 
      參數(shù)說明:
      
      ```參數(shù)名
      說明
      name
      要創(chuàng)建 `/proc` 下的文件名
      mode
      創(chuàng)建的文件權限的掩碼,若為 0,則使用系統(tǒng)默認的權限
      base
      該文件所在的父目錄,若該參數(shù)為 null,則該文件將會被創(chuàng)建在 /proc 的根目錄下
      read_proc
      讀取 /proc 下的文件時調用的函數(shù),也就是前面講解的那個函數(shù)
      data
      內核會忽略 date,但會把該參數(shù)傳遞給 read_proc 函數(shù)```
      **刪除 /proc 系統(tǒng)文件的函數(shù)**:
      `void remove_proc_entry(const char *name,struct proc_dir_entry *parent)`
      
      參數(shù)說明:
      
      ```參數(shù)名
      說明
      name
      在 /proc 文件系統(tǒng)中創(chuàng)建的文件名
      parent
      父目錄名```
    • 使用 /proc 文件系統(tǒng)的缺點

      1. 刪除調用可能在 `/proc` 文件系統(tǒng)的文件正在被使用時發(fā)生
      2. 同一個文件名可能注冊兩次,這將會發(fā)生錯誤
  2. 創(chuàng)建簡單的 /proc 文件
\#cd /proc ; vi read_proc  //read_proc 的內容如下:
\#include <linux/kernel.h>
\#include <linux/init.h>
\#include <linux/module.h>
\#include <linux/proc_fs.h>
\ 
int read_proc(char *page,char **start,off_t offset,int count,int *eof,void *data);
\ 
static int __init test_proc_init(void)
{
    create_proc_read_entry("read_proc",0,NULL,read_proc,NULL);
    return 0;
}
\ 
static void __exit test_proc_exit(void)
{
    remove_proc_entry("read_proc",NULL);
}
\ 
int read_proc(char *page,char **start,off_t offset,int count,int *eof,void *data)
{
    int len = sprintf(page,"%s\n","hello world");
    return len;
}
\ 
module_init(test_proc_init);
module_exit(test_proc_exit);
\ 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("wangxq");
\ 
\#cat /proc/read_proc
hello world

/proc 目錄的應用

對此文件系統(tǒng)的訪問同一般文件相同。

例:

  1. 統(tǒng)計 cpu 個數(shù):
    cat /proc/cpuinfo | grep'physical id'|uniq -c|wc –l
  2. cpu 型號
    cat /proc/cpuinfo|grepname|cut -f2 -d:|uniq
  3. 計算每個 cpu 的內核數(shù)
    cat /proc/cpuinfo | grep'physical id'|awk -F':' '{count[$2]++;}END{sum=0;for(a in count){cc++;sum+=count[a]}printsum/cc;}'
  4. 內核版本
    cat /proc/version|cut-f1 -d'('
  5. 內核執(zhí)行的上下文轉換次數(shù)
    cat /proc/stat|grep ctxt|awk'{print $2}'
  6. 系統(tǒng)創(chuàng)建的進程數(shù)
    cat /proc/stat|grep processes|awk'{print $2}'
  7. 當前可用的內存數(shù)量
    cat /proc/meminfo|grep MemFree