如何理解Linux中的 /proc ?


什么是 /proc ?

官方文檔是這么解釋的:

The proc filesystem is a pseudo-filesystem which provides an interface to kernel data structures. It is commonly mounted at /proc. Typically, it is mounted automatically by the system, but it can also be mounted manually using a command such as:

$ mount -t proc proc /proc (it means --> mount -t type device dir, This tells the kernel to attach the filesystem found on device (which is of type type) at the directory dir. )

Most of the files in the proc filesystem are read-only, but some files are writable, allowing kernel variables to be changed.

簡單的說, proc 是一個偽文件系統,它提供了內核數據結構的接口。它通常掛載在 /proc 目錄下。一般是由系統自動掛載的,不過也可以通過 mount 命令進行手動掛載。proc 文件系統只包含系統運行時的信息(如系統內存、mount 設備信息等),它只存在於內存中而不占用外存空間。它以文件系統的形式,為訪問內核數據的操作提供接口。

所謂的進程,是只有機器在運行的時候才有的數據,也就是都在內存當中的。而內存中的數據都寫入到 /proc/* 這個目錄下。我們可以直接查看這個目錄下的文件。如下:

# ll /proc
total 0
dr-xr-xr-x.  9 root           root                         0 Dec  1 18:37 1
dr-xr-xr-x.  9 root           root                         0 Dec  1 18:37 10
dr-xr-xr-x.  9 root           root                         0 Dec  1 18:37 101
dr-xr-xr-x.  9 root           root                         0 Dec  1 18:37 104
dr-xr-xr-x.  9 root           root                         0 Dec  1 18:37 105
...

各個進程的 PID 都是以目錄的類型存在於 /proc 當中。比如,系統開機執行的第一個進程 init (其PID為1),該進程的所有相關信息都在 /proc/1/ 中。而在 /proc/{pid}/ 目錄下的內容是這個樣子的:

# ls -l /proc/self/
total 0
-r--r--r--. 1 root root 0 Dec  2 17:04 arch_status
dr-xr-xr-x. 2 root root 0 Dec  2 17:04 attr
-rw-r--r--. 1 root root 0 Dec  2 17:04 autogroup
-r--------. 1 root root 0 Dec  2 17:04 auxv
-r--r--r--. 1 root root 0 Dec  2 17:04 cgroup
--w-------. 1 root root 0 Dec  2 17:04 clear_refs
-r--r--r--. 1 root root 0 Dec  2 17:04 cmdline
-rw-r--r--. 1 root root 0 Dec  2 17:04 comm
-rw-r--r--. 1 root root 0 Dec  2 17:04 coredump_filter
-r--r--r--. 1 root root 0 Dec  2 17:04 cpuset
lrwxrwxrwx. 1 root root 0 Dec  2 17:04 cwd -> /proc #當前執行"ls -l /proc/self/"時的目錄
-r--------. 1 root root 0 Dec  2 17:04 environ
lrwxrwxrwx. 1 root root 0 Dec  2 17:04 exe -> /usr/bin/ls 
...

什么是 /proc/self ?

項目中涉及到了 /proc/self/exeproc/self/mountinfo 等相關的知識,所以在此記錄一下 /proc/self/ 的含義。/proc/self/ 到底是一個什么目錄呢?它又有什么作用呢?

【/proc/self】

官方文檔是這么解釋的:

/proc/self:This directory refers to the process accessing the /proc filesystem, and is identical to the /proc directory named by the process ID of the same process.

When a process accesses this magic symbolic link, it resolves to the process's own /proc/[pid] directory.

簡單的說,不同的進程訪問 /proc 時獲得的信息是不同的,當一個進程訪問 /proc/self 時,會解析成該進程相應的目錄(也就是 /proc/[本進程的pid]/)。/proc/self 指的是當前運行進程自己的環境,進程可以通過訪問 /proc/self/ 目錄來獲取自己的系統信息。(還可以在這個回答中看大家的討論,也許可以幫助你理解)

/proc/{pid}/exe 的作用如下:

Under Linux 2.2 and later, this file is a symbolic link containing the actual pathname of the executed command. This symbolic link can be dereferenced normally; attempting to open it will open the executable. You can even type /proc/[pid]/exe to run another copy of the same executable as is being run by process [pid]. In a multithreaded process, the contents of this symbolic link are not available if the main thread has already terminated (typically by calling pthread_exit(3)).

可以看到,我們可以通過執行 /proc/[pid]/exe 來運行一個被[pid]進程運行的可執行文件的副本。

【/proc/[pid]/mountinfo】

This file contains information about mount points in the process's mount namespace.

該文件包含了與進程[pid]相關的掛載信息。

我們看一下這些掛載信息是什么呢?

[root@hadoop1 ~]# cat /proc/self/mountinfo
21 43 0:21 / /sys rw,nosuid,nodev,noexec,relatime shared:6 - sysfs sysfs rw,seclabel
22 43 0:5  / /proc rw,nosuid,nodev,noexec,relatime shared:5 - proc proc rw
...
33 28 0:30 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:12 - cgroup cgroup rw,seclabel,memory
34 28 0:31 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:13 - cgroup cgroup rw,seclabel,cpuset

官方手冊中給出了這些信息的解釋,如下:

36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
(1)(2)(3)   (4)   (5)      (6)      (7)   (8) (9)   (10)         (11)

這里只說我覺得比較重要的幾點:

(4) root: the pathname of the directory in the filesystem which forms the root of this mount.(根)

(5) mount point: the pathname of the mount point relative to the process's root directory.(掛載點)

(9) filesystem type: the filesystem type in the form "type[.subtype]". 內核支持的文件系統類型可以通過 cat /proc/filesystems 查看,比如( "ext4", vfat", "tmpfs", "cgroup", "proc"等,詳見這里

(10) mount source: filesystem-specific information or "none". (自己掛載的時候就常常用到"none")


參考:

  1. 可以直接通過 man proc 查看man手冊 (官方手冊是最一手的資料)
  2. https://unix.stackexchange.com/questions/333225/which-process-is-proc-self-for
  3. Linux 在線查詢手冊 http://man7.org/linux/man-pages/index.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM