/proc目錄及proc文件系統

參考文檔

使用 /proc 文件系統來訪問 Linux 內核的內容

Linux下/proc目錄簡介


/proc目錄簡介


# cat /etc/fstab 
proc            /proc                   proc    defaults                0       0
tmpfs           /tmp                    tmpfs   defaults                0       0
sysfs           /sys                    sysfs   defaults                0       0
tmpfs           /dev                    tmpfs   defaults                0       0
var             /dev                    tmpfs   defaults                0       0
ramfs           /dev                    ramfs   defaults                0       0
debugfs         /sys/kernel/debug       debugfs defaults                0       0

1/PROC目錄及proc文件系統

/PROC裏面的文件是proc文件系統對應的文件.

Linux內核提供了一種通過 proc 文件系統,在運行時訪問內核內部數據結構、改變內核設置的機制.

proc文件系統是一個內存文件系統,它只存在內存當中,而不佔用外存空間.

在開機前,只存在/proc/目錄,目錄中無文件

在開機後,proc文件系統將有關進程的信息寫入/proc目錄中,即創建目錄文件和普通文件來標識進程的相關信息.

然後我們可以通過目錄中的文件來讀取進程相關信息,但部分可以通過寫來改變內核環境.?

最初開發 /proc 文件系統是爲了提供有關係統中進程的信息。但是由於這個文件系統非常有用,因此內核中的很多元素也開始使用它來報告信息,或啓用動態運行時配置。


2/PROC目錄中的文件

linux@ubuntu:/proc$ ls
1      14    2189  2289  24    2493  2631  451   663   7481  8041  912  998          fb             meminfo       sys
10     15    22    2290  2407  25    2638  4603  670   7482  8043  915  999          filesystems    misc          sysrq-trigger
1000   1562  2200  2298  2411  2501  2639  4729  7     757   8044  916  acpi         fs             modules       sysvipc
1005   16    221   23    2416  2505  2713  5     7145  759   8093  917  asound       interrupts     mounts        timer_list
1019   17    2229  2303  2417  2506  2718  522   717   7618  8107  921  buddyinfo    iomem          mpt           timer_stats
11     1760  2237  2304  2419  2508  2726  524   7225  7619  8219  923  bus          ioports        mtrr          tty
1148   1786  2240  2305  2424  2511  3     540   7226  766   8221  933  cgroups      irq            net           uptime
12     18    2241  2315  2426  2512  333   545   733   7675  8346  940  cmdline      kallsyms       pagetypeinfo  version
12827  1834  2250  2333  2429  2516  337   591   7385  7679  8351  968  consoles     kcore          partitions    version_signature
12918  1969  2261  2341  2432  2541  35    6     7387  769   860   988  cpuinfo      key-users      sched_debug   vmallocinfo
12938  2     2263  2350  2435  2542  38    62    7390  790   866   991  crypto       kmsg           schedstat     vmstat
12975  204   2269  2353  2436  2544  39    63    7392  792   867   992  devices      kpagecount     scsi          zoneinfo
12994  2062  2279  2358  2442  2548  40    631   7394  793   880   993  diskstats    kpageflags     self
13     207   2283  2360  246   2592  42    632   7396  7941  882   994  dma          latency_stats  slabinfo
13016  209   2285  2365  247   26    441   64    7398  7949  887   995  dri          loadavg        softirqs
13059  21    2286  2386  2476  2612  442   646   7400  8     8989  996  driver       locks          stat
1333   210   2288  2392  2492  2630  4484  6609  7402  8039  9     997  execdomains  mdstat         swaps

3/PROC文件分類

3.1/以數字爲名的文件夾

最小的數字爲1,最大的數字爲四位數,例如8989

linux@ubuntu:/proc$ l /proc/8989/ 
attr/      clear_refs       cpuset   fd/      limits      mem         net/       oom_score_adj  sched      stack   syscall
autogroup  cmdline          cwd@     fdinfo/  loginuid    mountinfo   ns/        pagemap        schedstat  stat    task/
auxv       comm             environ  io       map_files/  mounts      oom_adj    personality    sessionid  statm   wchan
cgroup     coredump_filter  exe@     latency  maps        mountstats  oom_score  root@          smaps      status

其中

attr		提供安全相關的屬性
cgroup 進程所屬的控制組

cmdline 	程序啓動時的命令行
environ 環境變量值
fd 一個包含所有文件描述符的目錄
mem 進程的內存被錄用情況
status 		當前進程的狀態
cwd 當前你工作目錄的鏈接
exe 指向 該進程的執行命令文件
maps 內存映射信息
statm 進程內存使用信息
root 鏈接此進程的root 目錄
oom_adj oom_score oom_score_adj 用於oom killer

3.2/普通文件

linux@ubuntu:/proc$ ls -al | grep '^-'
-r--r--r--   1 root       root                0  2月 26 10:45 buddyinfo
-r--r--r--   1 root       root                0  2月 26 10:45 cgroups
-r--r--r--   1 root       root                0  2月 26 10:45 cmdline
-r--r--r--   1 root       root                0  2月 26 10:45 consoles
-r--r--r--   1 root       root                0  2月 26 10:45 cpuinfo
-r--r--r--   1 root       root                0  2月 26 10:45 crypto
-r--r--r--   1 root       root                0  2月 26 10:45 devices
-r--r--r--   1 root       root                0  2月 26 10:45 diskstats
-r--r--r--   1 root       root                0  2月 26 10:45 dma
-r--r--r--   1 root       root                0  2月 26 10:45 execdomains
-r--r--r--   1 root       root                0  2月 26 10:45 fb
-r--r--r--   1 root       root                0  2月 26 10:45 filesystems
-r--r--r--   1 root       root                0  2月 26 10:45 interrupts
-r--r--r--   1 root       root                0  2月 26 10:45 iomem
-r--r--r--   1 root       root                0  2月 26 10:45 ioports
-r--r--r--   1 root       root                0  2月 26 10:45 kallsyms
-r--------   1 root       root       1069543424  2月 26 10:45 kcore
-r--r--r--   1 root       root                0  2月 26 10:45 key-users
-r--------   1 root       root                0  2月 23 20:50 kmsg
-r--------   1 root       root                0  2月 26 10:45 kpagecount
-r--------   1 root       root                0  2月 26 10:45 kpageflags
-rw-r--r--   1 root       root                0  2月 26 10:45 latency_stats
-r--r--r--   1 root       root                0  2月 26 10:45 loadavg
-r--r--r--   1 root       root                0  2月 26 10:45 locks
-r--r--r--   1 root       root                0  2月 23 20:51 mdstat
-r--r--r--   1 root       root                0  2月 23 20:51 meminfo
-r--r--r--   1 root       root                0  2月 26 10:45 misc
-r--r--r--   1 root       root                0  2月 26 10:45 modules
-rw-r--r--   1 root       root                0  2月 23 20:50 mtrr
-r--r--r--   1 root       root                0  2月 26 10:45 pagetypeinfo
-r--r--r--   1 root       root                0  2月 26 10:45 partitions
-r--r--r--   1 root       root                0  2月 26 10:45 sched_debug
-r--r--r--   1 root       root                0  2月 26 10:45 schedstat
-r--------   1 root       root                0  2月 26 10:45 slabinfo
-r--r--r--   1 root       root                0  2月 26 10:45 softirqs
-r--r--r--   1 root       root                0  2月 23 20:51 stat
-r--r--r--   1 root       root                0  2月 26 10:45 swaps
--w-------   1 root       root                0  2月 26 10:45 sysrq-trigger
-r--r--r--   1 root       root                0  2月 26 10:45 timer_list
-rw-r--r--   1 root       root                0  2月 26 10:45 timer_stats
-r--r--r--   1 root       root                0  2月 26 10:45 uptime
-r--r--r--   1 root       root                0  2月 26 10:45 version
-r--r--r--   1 root       root                0  2月 26 10:45 version_signature
-r--------   1 root       root                0  2月 26 10:45 vmallocinfo
-r--r--r--   1 root       root                0  2月 23 20:51 vmstat
-r--r--r--   1 root       root                0  2月 26 10:45 zoneinfo

檔名	                                     文件內容
/proc/cmdline		加載 kernel 時所下達的相關參數!查閱此文件,可瞭解系統是如何啓動的!
/proc/cpuinfo		本機的 CPU 的相關資訊,包含時脈、類型與運算功能等
/proc/devices		這個文件記錄了系統各個主要裝置的主要裝置代號,與 mknod 有關呢!
/proc/filesystems	目前系統已經加載的文件系統羅!
/proc/interrupts	目前系統上面的 IRQ 分配狀態。
/proc/ioports		目前系統上面各個裝置所配置的 I/O 位址。
/proc/kcore			這個就是內存的大小啦!好大對吧!但是不要讀他啦!
/proc/loadavg		還記得 top 以及 uptime 吧?沒錯!上頭的三個平均數值就是記錄在此!
/proc/meminfo		使用 free 列出的內存資訊,嘿嘿!在這裏也能夠查閱到!
/proc/modules		目前我們的 Linux 已經加載的模塊列表,也可以想成是驅動程序啦!
/proc/mounts		系統已經掛載的數據,就是用 mount 這個命令呼叫出來的數據啦!
/proc/swaps			到底系統掛加載的內存在哪裏?呵呵!使用掉的 partition 就記錄在此啦!
/proc/partitions	使用 fdisk -l 會出現目前所有的 partition 吧?在這個文件當中也有紀錄喔!
/proc/pci			在 PCI 匯流排上面,每個裝置的詳細情況!可用 lspci 來查閱!
/proc/uptime		就是用 uptime 的時候,會出現的資訊啦!
/proc/version		核心的版本,就是用 uname -a 顯示的內容啦!
/proc/bus/*			一些匯流排的裝置,還有 U盤 的裝置也記錄在此喔!

3.3/其他文件夾

dr-xr-xr-x  2 root       root       0  2月 23 20:50 acpi/
dr-xr-xr-x  5 root       root       0  2月 26 10:38 asound/
dr-xr-xr-x  4 root       root       0  2月 26 10:38 bus/
dr-xr-xr-x  3 root       root       0  2月 26 10:38 dri/
dr-xr-xr-x  2 root       root       0  2月 26 10:38 driver/
dr-xr-xr-x  8 root       root       0  2月 23 20:50 fs/
dr-xr-xr-x 56 root       root       0  2月 26 10:38 irq/
dr-xr-xr-x  3 root       root       0  2月 26 10:38 mpt/
dr-xr-xr-x  6 linux      linux      0  2月 26 10:38 net/
dr-xr-xr-x  5 root       root       0  2月 26 10:38 scsi/
dr-xr-xr-x  1 root       root       0  2月 23 20:50 sys/
dr-xr-xr-x  2 root       root       0  2月 26 10:38 sysvipc/
dr-xr-xr-x  4 root       root       0  2月 26 10:38 tty/

3.4鏈接文件

linux@ubuntu:/proc$ ls -al | grep '^l'
lrwxrwxrwx   1 root       root               11  2月 26 10:46 mounts -> self/mounts
lrwxrwxrwx   1 root       root                8  2月 26 10:46 net -> self/net
lrwxrwxrwx   1 root       root               64  2月 23 20:50 self -> 13087

self文件是個連接,但又好像是個目錄
在我鍵入ls的時候,self文件的內容就是ls進程的信息
cat /proc/self/status 的時候,打印的信息是 cat /proc/self/status" 進程的信息
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章