深入理解proc檔案系統 概述:linux 核心提供了一種擷取其內部資料結構和在系統運行時改變核心參數設定的方法,這種方法就是憑藉proc檔案系統。 1.proc ——一個虛擬檔案系統 proc檔案系統用來提供給核心和核心模組發送訊息給進程,之所以說它是虛擬檔案系統,是因為1)虛擬是指的是它沒有對應具體的儲存介質,比如磁碟;2)檔案系統指的是它的確實現了必須的檔案系統介面。可以通過如下命令查詢:[cpp] $ mount -l /dev/sda10 on / type ext4 (rw,errors=remount-ro) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) none on /sys/fs/fuse/connections type fusectl (rw) none on /sys/kernel/debug type debugfs (rw) none on /sys/kernel/security type securityfs (rw) udev on /dev type devtmpfs (rw,mode=0755) 2.proc一級目錄概覽 /proc/cpuinfo/proc/meminfo/proc/mounts----mounted fs/proc/devices----avaiable devices/proc/filesystems----supported file systems/proc/modules----loaded modules/proc/version-----kernel version/proc/cmdline----parameters passed to the kernel at the time of starting 3. 特定進程的相關資訊 如果要知道某個進程的相關資訊:環境變數,對應的應用程式,cpu佔用時間,進程狀態等等,可以使用proc檔案系統進行查詢。例如:$ ll /proc/2184total 0dr-xr-xr-x 8 hyk hyk 0 6月 17 20:51 ./dr-xr-xr-x 214 root root 0 6月 18 04:50 ../dr-xr-xr-x 2 hyk hyk 0 6月 18 10:27 attr/-rw-r--r-- 1 hyk hyk 0 6月 18 10:27 autogroup-r-------- 1 hyk hyk 0 6月 17 20:51 auxv-r--r--r-- 1 hyk hyk 0 6月 18 10:27 cgroup--w------- 1 hyk hyk 0 6月 18 10:27 clear_refs-r--r--r-- 1 hyk hyk 0 6月 17 20:51 cmdline-rw-r--r-- 1 hyk hyk 0 6月 18 10:27 comm-rw-r--r-- 1 hyk hyk 0 6月 18 10:27 coredump_filter-r--r--r-- 1 hyk hyk 0 6月 18 10:27 cpusetlrwxrwxrwx 1 hyk hyk 0 6月 18 10:27 cwd -> /home/hyk/-r-------- 1 hyk hyk 0 6月 17 20:51 environlrwxrwxrwx 1 hyk hyk 0 6月 17 20:51 exe -> /usr/lib/gnome-settings-daemon/gnome-settings-daemon*dr-x------ 2 hyk hyk 0 6月 17 20:51 fd/dr-x------ 2 hyk hyk 0 6月 18 10:27 fdinfo/-r-------- 1 hyk hyk 0 6月 18 10:27 io-r--r--r-- 1 hyk hyk 0 6月 18 10:27 latency-r--r--r-- 1 hyk hyk 0 6月 18 10:27 limits-rw-r--r-- 1 hyk hyk 0 6月 18 10:27 loginuid-r--r--r-- 1 hyk hyk 0 6月 18 10:27 maps-rw------- 1 hyk hyk 0 6月 18 10:27 mem-r--r--r-- 1 hyk hyk 0 6月 18 10:27 mountinfo-r--r--r-- 1 hyk hyk 0 6月 17 20:51 mounts-r-------- 1 hyk hyk 0 6月 18 10:27 mountstatsdr-xr-xr-x 5 hyk hyk 0 6月 18 10:27 net/dr-x--x--x 2 hyk hyk 0 6月 18 10:27 ns/-rw-r--r-- 1 hyk hyk 0 6月 18 10:27 oom_adj-r--r--r-- 1 hyk hyk 0 6月 18 10:27 oom_score-rw-r--r-- 1 hyk hyk 0 6月 18 10:27 oom_score_adj-r--r--r-- 1 hyk hyk 0 6月 18 10:27 pagemap-r--r--r-- 1 hyk hyk 0 6月 18 10:27 personalitylrwxrwxrwx 1 hyk hyk 0 6月 18 10:27 root -> //-rw-r--r-- 1 hyk hyk 0 6月 18 10:27 sched-r--r--r-- 1 hyk hyk 0 6月 18 10:27 schedstat-r--r--r-- 1 hyk hyk 0 6月 18 10:27 sessionid-r--r--r-- 1 hyk hyk 0 6月 18 10:27 smaps-r--r--r-- 1 hyk hyk 0 6月 18 10:27 stack-r--r--r-- 1 hyk hyk 0 6月 17 20:51 stat-r--r--r-- 1 hyk hyk 0 6月 18 10:27 statm-r--r--r-- 1 hyk hyk 0 6月 17 20:51 status-r--r--r-- 1 hyk hyk 0 6月 18 10:27 syscalldr-xr-xr-x 7 hyk hyk 0 6月 18 10:27 task/-r--r--r-- 1 hyk hyk 0 6月 18 10:27 wchan相關選項的說明如下:cmdline:啟動該進程的命令列environ:所有與該進程有關的環境變數status:進程狀態exe:該進程對應的可執行檔cwd:current work directoryroot:該進程的根目錄,通常是/fd:指向某進程開啟檔案的描述符cpu:cpu佔用時間注意:如果某個進程訪問/proc/self,實際上是訪問它本身對應的proc目錄。 4.通過proc檔案系統,修改核心參數 proc檔案系統下的大部分內容是read-only的,但是在/proc/sys下面的內容大部分是可寫的。$ cat /proc/sys/kernel/hostname hyk-linuxroot@hyk-linux:/home/hyk# echo "hyk-pc" > /proc/sys/kernel/hostname $ cat /proc/sys/kernel/hostname hyk-pc 5.通過程式提取proc中的資訊[cpp] #include <stdio.h> #include <string.h> /* Returns the clock speed of the system’s CPU in MHz, as reported by /proc/cpuinfo. On a multiprocessor machine, returns the speed of the first CPU. On error returns zero. */ float get_cpu_clock_speed () { FILE* fp; char buffer[1024]; size_t bytes_read; char* match; float clock_speed; /* Read the entire contents of /proc/cpuinfo into the buffer. fp = fopen (“/proc/cpuinfo”, “r”); bytes_read = fread (buffer, 1, sizeof (buffer), fp); fclose (fp); /* Bail if read failed or if buffer isn’t big enough. */ if (bytes_read == 0 || bytes_read == sizeof (buffer)) return 0; /* NUL-terminate the text. */ buffer[bytes_read] = ‘\0’; /* Locate the line that starts with “cpu MHz”. */ match = strstr (buffer, “cpu MHz”); if (match == NULL) return 0; /* Parse the line to extract the clock speed. */ sscanf (match, “cpu MHz : %f”, &clock_speed); return clock_speed; */ } int main () { printf (“CPU clock speed: %4.0f MHz\n”, get_cpu_clock_speed ()); return 0; }