如何理解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
原文地址:https://www.cnblogs.com/kkbill/p/13403625.html