📜  Linux中的proc文件系统

📅  最后修改于: 2022-05-13 01:57:34.739000             🧑  作者: Mango

Linux中的proc文件系统

Proc 文件系统 (procfs) 是在系统启动时即时创建并在系统关闭时解散的虚拟文件系统。

它包含有关当前正在运行的进程的有用信息,它被视为内核的控制和信息中心。

proc 文件系统还提供内核空间和用户空间之间的通信媒介。

下面是我电脑上 /proc 的快照。

ls -l /proc

total 0
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 10
dr-xr-xr-x  9 avahi        avahi          0 Mar 31 21:34 1034
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1036
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1039
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1041
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1043
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1044
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1048
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 105
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1078
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 11
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1121
dr-xr-xr-x  9 lp           lp             0 Mar 31 21:34 1146
dr-xr-xr-x  9 postgres     postgres       0 Mar 31 21:34 1149
dr-xr-xr-x  9 mysql        mysql          0 Mar 31 21:34 1169
dr-xr-xr-x  9 postgres     postgres       0 Mar 31 21:34 1180
dr-xr-xr-x  9 postgres     postgres       0 Mar 31 21:34 1181
dr-xr-xr-x  9 postgres     postgres       0 Mar 31 21:34 1182
dr-xr-xr-x  9 postgres     postgres       0 Mar 31 21:34 1183
dr-xr-xr-x  9 postgres     postgres       0 Mar 31 21:34 1184
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 1186
dr-xr-xr-x  9 root         root           0 Mar 31 21:34 12

...

如果列出目录,您会发现进程的每个 PID 都有一个专用目录。
您只能使用终端在终端上检查目录



ls -l /proc | grep '^d'

现在让我们检查分配PID的特定进程,您可以从ps命令获取任何正在运行的进程的PID

ps -aux

输出:

ps -aux 命令输出

现在检查 PID=7494 突出显示的进程,您可以检查 /proc 文件系统中是否有该进程的条目。

ls -ltr /proc/7494

输出:

total 0
-rw-r--r--  1 mandeep mandeep 0 Apr  1 01:14 oom_score_adj
dr-xr-xr-x 13 mandeep mandeep 0 Apr  1 01:14 task
-r--r--r--  1 mandeep mandeep 0 Apr  1 01:16 status
-r--r--r--  1 mandeep mandeep 0 Apr  1 01:16 stat
-r--r--r--  1 mandeep mandeep 0 Apr  1 01:16 cmdline
-r--r--r--  1 mandeep mandeep 0 Apr  1 01:17 wchan
-rw-r--r--  1 mandeep mandeep 0 Apr  1 01:17 uid_map
-rw-rw-rw-  1 mandeep mandeep 0 Apr  1 01:17 timerslack_ns
-r--r--r--  1 mandeep mandeep 0 Apr  1 01:17 timers
-r--------  1 mandeep mandeep 0 Apr  1 01:17 syscall
-r--r--r--  1 mandeep mandeep 0 Apr  1 01:17 statm
-r--------  1 mandeep mandeep 0 Apr  1 01:17 stack
-r--r--r--  1 mandeep mandeep 0 Apr  1 01:17 smaps
-rw-r--r--  1 mandeep mandeep 0 Apr  1 01:17 setgroups
-r--r--r--  1 mandeep mandeep 0 Apr  1 01:17 sessionid
-r--r--r--  1 mandeep mandeep 0 Apr  1 01:17 schedstat
-rw-r--r--  1 mandeep mandeep 0 Apr  1 01:17 sched
lrwxrwxrwx  1 mandeep mandeep 0 Apr  1 01:17 root -> /proc/2341/fdinfo
-rw-r--r--  1 mandeep mandeep 0 Apr  1 01:17 projid_map
-r--------  1 mandeep mandeep 0 Apr  1 01:17 personality

...

在 linux 中,/proc 为每个正在运行的进程包括一个目录,包括内核进程,在名为 /proc/PID 的目录中,这些是存在的目录:

directorydescription
/proc/PID/cmdlineCommand line arguments.
/proc/PID/cpuCurrent and last cpu in which it was executed.
/proc/PID/cwdLink to the current working directory.
/proc/PID/environValues of environment variables.
/proc/PID/exeLink to the executable of this process.
/proc/PID/fdDirectory, which contains all file descriptors.
/proc/PID/mapsMemory maps to executables and library files.
/proc/PID/memMemory held by this process.
/proc/PID/rootLink to the root directory of this process.
/proc/PID/statProcess status.
/proc/PID/statmProcess memory status information.
/proc/PID/statusProcess status in human readable form.

/proc 文件系统中的其他一些文件是:

filedescription
/proc/cryptolist of available cryptographic modules
/proc/diskstatsnformation (including device numbers) for each of the logical disk devices
/proc/filesystemslist of the file systems supported by the kernel at the time of listing
/proc/kmsgholding messages output by the kernel
/proc/meminfosummary of how the kernel is managing its memory.
/proc/scsiinformation about any devices connected via a SCSI or RAID controller
/proc/ttyinformation about the current terminals
/proc/versioncontaining the Linux kernel version, distribution number, gcc version number (used to build the kernel) and any other pertinent information relating to the version of the kernel currently running

比如/proc/crypto的内容是

less /proc/crypto

name         : ccm(aes)
driver       : ccm_base(ctr(aes-aesni), cbcmac(aes-aesni))
module       : ccm
priority     : 300
refcnt       : 2
selftest     : passed
internal     : no
type         : aead
async        : no
blocksize    : 1
ivsize       : 16
maxauthsize  : 16
geniv        : 

name         : ctr(aes)
driver       : ctr(aes-aesni)
module       : kernel
priority     : 300
refcnt       : 3
selftest     : passed
internal     : no
type         : blkcipher
blocksize    : 1
min keysize  : 16
max keysize  : 32
ivsize       : 16
geniv        : chainiv

...

参考 :
1) /wiki/Procfs
2) /过程