Linux /proc/pid 文件目录解析

Linux 内核提供了一种通过 proc 文件系统,在运行时访问内核内部数据结构、改变内核设置的机制。proc 文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为访问系统内核数据的操作提供接口。

用户和应用程序可以通过 proc 得到系统的信息,并可以改变内核的某些参数。由于系统的信息,如进程,是动态改变的,所以用户或应用程序读取 proc 文件时,proc 文件系统是动态从系统内核读出所需信息并提交的。

 

/proc/[pid]/auxv 包含传递给进程的 ELF 解释器信息,格式是每一项都是一个 unsigned long长度的 ID 加上一个 unsigned long 长度的值。最后一项以连续的两个 0x00 开头。

hexdump -x /proc/2406/auxv

/proc/[pid]/environ 显示进程的环境变量 

/proc/[pid]/fd 是一个目录,包含进程打开文件的情况。举例如下:

address                                    perms  offset      dev       inode                                         pathname

permissions(perms)

                  r = read
                  w = write
                  x = execute
                  s = shared
                  p = private (copy on write)

 

 
The pathname field will usually be the file that is
              backing the mapping.

 

ELF 全称 “Executable and Linkable Format”,即可执行可链接文件格式,目前常见的Linux、 Android可执行文件、共享库(.so)、目标文件( .o)以及Core 文件(吐核)均为此格式。

 

/proc/[pid]/stack 示当前进程的内核调用栈信息,只有内核编译时打开了 CONFIG_STACKTRACE 编译选项,才会生成这个文件。

/proc/[pid]/statm 显示进程所占用内存大小的统计信息。包含七个值,度量单位是 page(page大小可通过 getconf PAGESIZE 得到)。

  1. 进程占用的总的内存
  2. 进程当前时刻占用的物理内存
  3. 同其它进程共享的内存
  4. 进程的代码段
  5. 共享库(从2.6版本起,这个值为0)
  6. 进程的堆栈
  7. dirty pages(从2.6版本起,这个值为0)

 

/proc/[pid]/status 包含进程的状态信息。其很多内容与 /proc/[pid]/stat 和 /proc/[pid]/statm 相同,但是却是以一种更清晰地方式展现出来。举例如下:

               文献很详细
               Name   Command run by this process.  Strings longer than
                     TASK_COMM_LEN (16) characters (including the
                     terminating null byte) are silently truncated.

              Umask  Process umask, expressed in octal with a leading
                     zero; see umask(2).  (Since Linux 4.7.)

              State  Current state of the process.  One of "R
                     (running)", "S (sleeping)", "D (disk sleep)", "T
                     (stopped)", "t (tracing stop)", "Z (zombie)", or "X
                     (dead)".

              Tgid   Thread group ID (i.e., Process ID).

              Ngid   NUMA group ID (0 if none; since Linux 3.13).

              Pid    Thread ID (see gettid(2)).

              PPid   PID of parent process.

              TracerPid
                     PID of process tracing this process (0 if not being
                     traced).

              Uid, Gid
                     Real, effective, saved set, and filesystem UIDs
                     (GIDs).

              FDSize Number of file descriptor slots currently
                     allocated.

              Groups Supplementary group list.

              NStgid Thread group ID (i.e., PID) in each of the PID
                     namespaces of which [pid] is a member.  The
                     leftmost entry shows the value with respect to the
                     PID namespace of the process that mounted this
                     procfs (or the root namespace if mounted by the
                     kernel), followed by the value in successively
                     nested inner namespaces.  (Since Linux 4.1.)

              NSpid  Thread ID in each of the PID namespaces of which
                     [pid] is a member.  The fields are ordered as for
                     NStgid.  (Since Linux 4.1.)

              NSpgid Process group ID in each of the PID namespaces of
                     which [pid] is a member.  The fields are ordered as
                     for NStgid.  (Since Linux 4.1.)

              NSsid  descendant namespace session ID hierarchy Session
                     ID in each of the PID namespaces of which [pid] is
                     a member.  The fields are ordered as for NStgid.
                     (Since Linux 4.1.)

              VmPeak Peak virtual memory size.

              VmSize Virtual memory size.

              VmLck  Locked memory size (see mlock(2)).

              VmPin  Pinned memory size (since Linux 3.2).  These are
                     pages that can't be moved because something needs
                     to directly access physical memory.

              VmHWM  Peak resident set size ("high water mark").  This
                     value is inaccurate; see /proc/[pid]/statm above.

              VmRSS  Resident set size.  Note that the value here is the
                     sum of RssAnon, RssFile, and RssShmem.  This value
                     is inaccurate; see /proc/[pid]/statm above.

              RssAnon
                     Size of resident anonymous memory.  (since Linux
                     4.5).  This value is inaccurate; see
                     /proc/[pid]/statm above.

              RssFile
                     Size of resident file mappings.  (since Linux 4.5).
                     This value is inaccurate; see /proc/[pid]/statm
                     above.

              RssShmem
                     Size of resident shared memory (includes System V
                     shared memory, mappings from tmpfs(5), and shared
                     anonymous mappings).  (since Linux 4.5).

              VmData, VmStk, VmExe
                     Size of data, stack, and text segments.  This value
                     is inaccurate; see /proc/[pid]/statm above.

              VmLib  Shared library code size.

              VmPTE  Page table entries size (since Linux 2.6.10).

              VmPMD  Size of second-level page tables (added in Linux
                     4.0; removed in Linux 4.15).

              VmSwap Swapped-out virtual memory size by anonymous
                     private pages; shmem swap usage is not included
                     (since Linux 2.6.34).  This value is inaccurate;
                     see /proc/[pid]/statm above.

              HugetlbPages
                     Size of hugetlb memory portions (since Linux 4.4).

              CoreDumping
                     Contains the value 1 if the process is currently
                     dumping core, and 0 if it is not (since Linux
                     4.15).  This information can be used by a
                     monitoring process to avoid killing a process that
                     is currently dumping core, which could result in a
                     corrupted core dump file.

              Threads
                     Number of threads in process containing this
                     thread.

              SigQ   This field contains two slash-separated numbers
                     that relate to queued signals for the real user ID
                     of this process.  The first of these is the number
                     of currently queued signals for this real user ID,
                     and the second is the resource limit on the number
                     of queued signals for this process (see the
                     description of RLIMIT_SIGPENDING in getrlimit(2)).

              SigPnd, ShdPnd
                     Mask (expressed in hexadecimal) of signals pending
                     for thread and for process as a whole (see
                     pthreads(7) and signal(7)).

              SigBlk, SigIgn, SigCgt
                     Masks (expressed in hexadecimal) indicating signals
                     being blocked, ignored, and caught (see signal(7)).

              CapInh, CapPrm, CapEff
                     Masks (expressed in hexadecimal) of capabilities
                     enabled in inheritable, permitted, and effective
                     sets (see capabilities(7)).

              CapBnd Capability bounding set, expressed in hexadecimal
                     (since Linux 2.6.26, see capabilities(7)).

              CapAmb Ambient capability set, expressed in hexadecimal
                     (since Linux 4.3, see capabilities(7)).

              NoNewPrivs
                     Value of the no_new_privs bit (since Linux 4.10,
                     see prctl(2)).

              Seccomp
                     Seccomp mode of the process (since Linux 3.8, see
                     seccomp(2)).  0 means SECCOMP_MODE_DISABLED; 1
                     means SECCOMP_MODE_STRICT; 2 means
                     SECCOMP_MODE_FILTER.  This field is provided only
                     if the kernel was built with the CONFIG_SECCOMP
                     kernel configuration option enabled.

              Speculation_Store_Bypass
                     Speculation flaw mitigation state (since Linux
                     4.17, see prctl(2)).

              Cpus_allowed
                     Hexadecimal mask of CPUs on which this process may
                     run (since Linux 2.6.24, see cpuset(7)).

              Cpus_allowed_list
                     Same as previous, but in "list format" (since Linux
                     2.6.26, see cpuset(7)).

              Mems_allowed
                     Mask of memory nodes allowed to this process (since
                     Linux 2.6.24, see cpuset(7)).

              Mems_allowed_list
                     Same as previous, but in "list format" (since Linux
                     2.6.26, see cpuset(7)).

              voluntary_ctxt_switches, nonvoluntary_ctxt_switches
                     Number of voluntary and involuntary context
                     switches (since Linux 2.6.23).

参考文献

https://www.hi-linux.com/posts/64295.html

https://man7.org/linux/man-pages/man5/proc.5.html

https://www.networkworld.com/article/3211296/unix-dealing-with-signals.html

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Linux系统中,/proc目录下的文件或目录包含了许多与系统运行状态相关的信息,下面是一些常见的或目录及其含义: 1. `/proc/cpuinfo`:显示CPU的信息,如CPU型号、频率、缓存大小等。 2. `/proc/meminfo`:显示系统内存的信息,如总内存大小、可用内存大小等。 3. `/proc/loadavg`:显示系统的平均负载,包括1分钟、5分钟、15分钟的负载。 4. `/proc/filesystems`:显示系统支持的文件系统类型。 5. `/proc/net/dev`:显示网络设备的统计信息,如接收和发送的数据包数量、错误数量等。 6. `/proc/mounts`:显示系统当前挂载的文件系统信息。 7. `/proc/sys/kernel`:包含了一些与内核相关的参数,如系统的主机名、时区等。 8. `/proc/sys/net`:包含了一些与网络相关的参数,如TCP/IP协议栈的参数、路由表等。 9. `/proc/sys/fs`:包含了一些与文件系统相关的参数,如文件系统缓存、文件句柄数量等。 注意:在/proc目录下的文件或目录实际上并不是真正的文件或目录,而是内核提供的接口。读取这些文件或目录实际上是在读取内核中的数据结构,因此它们的内容可能会随着系统运行状态而动态变化。 ### 回答2: `/proc`目录是Linux操作系统中的一个特殊目录,提供了即时系统信息和进程信息的虚拟文件系统。以下是`/proc`目录中一些重要文件和目录的含义: - `/proc/cpuinfo`: 该文件提供了有关系统CPU的详细信息,如制造商、型号、核心数量、缓存等。可以通过查看此文件来了解系统的处理能力和配置信息。 - `/proc/meminfo`: 该文件提供了有关系统内存的信息,如总内存量、可用内存、已使用的内存、缓存和交换空间等。可以通过查看此文件来监控系统的内存使用情况。 - `/proc/loadavg`: 该文件提供了系统的平均负载信息。负载表示系统中正在使用和等待CPU的进程数量。通过查看此文件,可以了解系统的负载情况,以便决定何时采取相应的措施。 - `/proc/net`: 该目录提供了有关网络协议、连接和统计信息的文件。其中,`/proc/net/tcp`和`/proc/net/udp`文件分别显示了系统上的TCP和UDP连接信息,可以通过查看这些文件来监控系统的网络活动。 - `/proc/<PID>`: 在`/proc`目录下以进程ID命名的子目录包含了有关每个运行中进程的详细信息。每个子目录中的文件可用于查看进程的状态、内存使用、打开的文件和文件系统信息等。例如,可以使用`/proc/1/status`文件来查看init进程的状态信息。 总之,`/proc`目录下的文件和目录提供了丰富的系统和进程信息,可以为系统管理和性能监控提供重要参考。 ### 回答3: `/proc`目录是Linux系统中的一个特殊目录,提供了对内核和正在运行的进程信息的访问。在该目录下,包含了一系列的文件和目录,每个文件和目录代表了系统中的一个进程或一些系统信息。 首先,`/proc`目录中的文件和目录都是虚拟的文件系统,它们并不真实存在于磁盘上,而是在运行时由内核动态生成。 其中,`/proc/[PID]`目录下的文件和目录代表了系统中各个进程的信息。`[PID]`指代进程的ID号,通过访问这些文件和目录,可以了解进程的诸多详细信息,如进程的状态、命令行参数、内存映射、文件描述符等等。例如,可以通过`/proc/1/status`文件查看init进程的状态信息。 `/proc/[PID]/fd`目录下则包含了进程打开的文件描述符的符号链接,通过这些符号链接可以找到相应的文件。 除了进程相关的信息之外,`/proc`目录下还包含了一些系统信息的文件。例如,`/proc/meminfo`文件提供了系统内存的相关信息,`/proc/cpuinfo`文件则提供了CPU的相关信息。 需要强调的是,`/proc`目录下的文件都以文本形式存在,可以通过读取和解析这些文件来获得所需的信息。尽管目录和文件的命名规则是明确的,但要注意文件的格式和内容在不同版本和配置的Linux上可能会有所不同。 总之,`/proc`目录是一个非常重要的系统目录,通过访问它下面的文件和目录,我们可以了解系统内核和正在运行的进程的详细信息,有助于系统调试和性能优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值