代码改变世界

Linux /proc/pid目录下各文件含义

2015-11-20 11:00  youxin  阅读(6633)  评论(0编辑  收藏  举报
/proc 是一个伪文件系统, 被用作内核数据结构的接口, 而不仅仅是解释说明/dev/kmem.
/proc 里的大多数文件都是只读的, 但也可以通过写一些文件来改变内核变量.
(

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

用户和应用程序可以通过proc得到系统的信息,并可以改变内核的某些参数。由于系统的信息,如进程,是动态改变的,所以用户或应用程序读取proc文件时,proc文件系统是动态从系统内核读出所需信息并提交的。下面列出的这些文件或子文件夹,并不是都是在你的系统中存在,这取决于你的内核配置和装载的模块。另外,在/proc下还有三个很重要的目录:net,scsi和sys。 Sys目录是可写的,可以通过它来访问或修改内核的参数,而net和scsi则依赖于内核配置。例如,如果系统不支持scsi,则scsi 目录不存在。

除了以上介绍的这些,还有的是一些以数字命名的目录,它们是进程目录。系统中当前运行的每一个进程都有对应的一个目录在/proc下,以进程的 PID号为目录名,它们是读取进程信息的接口。而self目录则是读取进程本身的信息接口,是一个link。

)



下面对整个 /proc 目录作一个大略的介绍.

[number]
在 /proc 目录里, 每个正在运行的进程都有一个以该进程 ID 命名的子目录, 其下包括如下的目录和伪文件:
[number] /cmdline
该文件保存了进程的完整命令行. 如果该进程已经被交换出内存, 或者该进程已经僵死, 那么就没有任何东西在该文件里, 这时候对该文件的读操作将返回零个字符. 该文件以空字符 null 而不是换行符作为结束标志.
[number] /cwd
一个符号连接, 指向进程当前的工作目录. 例如, 要找出进程 20 的 cwd, 你可以:
cd /proc/20/cwd; /bin/pwd
请注意 pwd 命令通常是 shell 内置的, 在这样的情况下可能工作得不是很好(casper 注: pwd 只能显示 /proc/20/cwd, 要是想知道它的工作目录,直接ls -al /proc/20不就好了).
[number] /environ
该文件保存进程的环境变量, 各项之间以空字符分隔, 结尾也可能是一个空字符. 因此, 如果要输出进程 1 的环境变量, 你应该:
(cat /proc/1/environ; echo) | tr ";\000"; ";\n";
(至于为什么想要这么做, 请参阅 lilo(8).)
[number] /exe
也是一个符号连接, 指向被执行的二进制代码. 在 Linux 2.0 或者更早的版本下, 对 exe 特殊文件的 readlink(2) 返回一个如下格式的字符串: [设备号]:节点号
举个例子, [0301]:1502 就是某设备的 1502 节点, 该设备的主设备号为 03 (如 IDE, MFM 等驱动器), 从设备号为 01 (第一个驱动器的第一分区). 而在 Linux 2.2 下, readlink(2) 则给出命令的实际路径名. 另外, 该符号连接也可以正常析引用(试图打开 exe 文件实际上将打开一个可执行文件). 你甚至可以键入 /proc/[number]/exe 来运行 [number] 进程的副本. 带 -inum 选项的 find(1) 命令可以定位该文件.
[number] /fd
进 程所打开的每个文件都有一个符号连接在该子目录里, 以文件描述符命名, 这个名字实际上是指向真正的文件的符号连接,(和 exe 记录一样).例如, 0 是标准输入, 1 是标准输出, 2 是标准错误, 等等. 程序有时可能想要读取一个文件却不想要标准输入,或者想写到一个文件却不想将输出送到标准输出去,那么就可以很有效地用如下的办法骗过(假定 -i 是输入文件的标志, 而 -o 是输出文件的标志):
foobar -i /proc/self/fd/0 -o /proc/self/fd/1 ...
这 样就是一个能运转的过滤器. 请注意该方法不能用来在文件里搜索, 这是因为 fd 目录里的文件是不可搜索的. 在 UNIX 类的系统下, /proc/self/fd/N 基本上就与 /dev/fd/N 相同. 实际上, 大多数的 Linux MAKEDEV 脚本都将 /dev/fd 符号连接到 [..]/proc/self/fd 上.
]
 

 

attr:

进程的属性

 

cmdline:

启动进程时执行的命令

 

cwd:

指向进程当前工作目录的软

 ll cwd可知是个软连接。

 

environ:

进程执行时使用的环境变量,文件内容使用null字节('\0')分隔,然后以null字节结束。因此获取进程使用的环境变量使用如下:

(cat /proc/pid/environ; echo) | tr '\000' '\n'

 

fd:

 此目录包含进程打开的所有文件,文件名为文件描述符,目录中每个软连接都会指向进程打开的实际文件。

 比如:nginx下:

root@iZ23onhpqvwZ:/proc/22210/fd# ll
total 0
dr-x------ 2 root root 0 Oct 20 17:39 ./
dr-xr-xr-x 9 root root 0 Oct 20 17:22 ../
lrwx------ 1 root root 64 Oct 20 17:39 0 -> /dev/null
lrwx------ 1 root root 64 Oct 20 17:39 1 -> /dev/null
lrwx------ 1 root root 64 Oct 20 17:39 10 -> socket:[2917559817]
l-wx------ 1 root root 64 Oct 20 17:39 2 -> /opt/nginx/logs/error.log
l-wx------ 1 root root 64 Oct 20 17:39 3 -> /opt/nginx/logs/access.log
lrwx------ 1 root root 64 Oct 20 17:39 6 -> socket:[2917557592]
lrwx------ 1 root root 64 Oct 20 17:39 7 -> socket:[2917557593]
l-wx------ 1 root root 64 Oct 20 17:39 8 -> /opt/nginx/logs/error.log
lrwx------ 1 root root 64 Oct 20 17:39 9 -> socket:[2917559816]

 

limits:

该文件存储了进程的软限制,硬限制等信息。

 可以查看该进程允许打开的最大描述字个数。

 

 

maps:

 

 

      address         perms offset  dev   inode   pathname

      08048000-08056000 r-xp 00000000 03:0c 64593   /usr/sbin/gpm

      4001f000-40135000 r-xp 00000000 03:0c 45494   /lib/libc-2.2.4.so

         * address:进程占用的地址空间。

         * perms:权限集

                 r = read

                 w = write

                 x = execute

                 s = shared

                 p = private (copy on write)

        * offset:文件偏移量。

        * dev:为设备(major:minor)

        * inode:设备上的inode。0为没有inode关联互内存区域,通常为:BSS(uninitialized data)

 

root:

指向进程更目录的软链

 

smaps:

 

      This file shows memory consumption for each of the process's mappings.  For each of mappings there is a series of lines such as the following:

 

  08048000-080bc000 r-xp 00000000 03:02 13130    /bin/bash

  Size:       464 kB

  Rss:       424 kB

  Shared_Clean:       424 kB

  Shared_Dirty: 0 kB

  Private_Clean: 0 kB

  Private_Dirty: 0 kB

 

      The first of these lines shows the same information as is displayed for the mapping in /proc/[pid]/maps. The remaining lines show the size of

      the  mapping,  the amount of the mapping that is currently resident in RAM, the number of clean and dirty shared pages in the mapping, and the

      number of clean and dirty private pages in the mapping.

 

      This file is only present if the CONFIG_MMU kernel configuration option is enabled.

 
status:
  Name:   bash
  State:  S (sleeping)
  Tgid:   3515
  Pid:   3515
  PPid:   3452
  TracerPid:   0
  Uid:   1000   1000   1000   1000
  Gid:   100   100   100   100
  FDSize: 256
  Groups: 16 33 100
  VmPeak:     9136 kB
  VmSize:     7896 kB
  VmLck:  0 kB
  VmHWM:      7572 kB
  VmRSS:      6316 kB
  VmData:     5224 kB
  VmStk: 88 kB
  VmExe:       572 kB
  VmLib:      1708 kB
  VmPTE: 20 kB
  Threads:   1
  SigQ:   0/3067
  SigPnd: 0000000000000000
  ShdPnd: 0000000000000000
  SigBlk: 0000000000010000
  SigIgn: 0000000000384004
  SigCgt: 000000004b813efb
  CapInh: 0000000000000000
  CapPrm: 0000000000000000
  CapEff: 0000000000000000
  CapBnd: ffffffffffffffff
  Cpus_allowed:   00000001
  Cpus_allowed_list:   0
  Mems_allowed:   1
  Mems_allowed_list:   0
  voluntary_ctxt_switches:   150
  nonvoluntary_ctxt_switches:   545
 
      The fields are as follows:
 
      * Name: Command run by this process.
 
      * State: Current state of the process.  One of "R (running)", "S (sleeping)", "D (disk sleep)", "T (stopped)", "T (tracing  stop)",  "Z  (zom-
bie)", or "X (dead)".
 
      * Tgid: Thread group ID (i.e., Process ID).
 
      * Pid: Thread ID (see gettid(2)).
 
      * TracerPid: PID of process tracing this process (0 if not being traced).
 
      * Uid, Gid: Real, effective, saved set, and file system UIDs (GIDs).
 
      * FDSize: Number of file descriptor slots currently allocated.
 
      * Groups: Supplementary group list.
 
      * VmPeak: Peak virtual memory size.
 
      * VmSize: Virtual memory size.
 
      * VmLck: Locked memory size.
 
      * VmHWM: Peak resident set size ("high water mark").
 
      * VmRSS: Resident set size.
 
      * VmData, VmStk, VmExe: Size of data, stack, and text segments.
 
      * VmLib: Shared library code size.
 
      * VmPTE: Page table entries size (since Linux 2.6.10).
 
      * Threads: Number of threads in process containing this thread.
 
      * SigPnd, ShdPnd: Number of signals pending for thread and for process as a whole (see pthreads(7) and signal(7)).
 
      * SigBlk, SigIgn, SigCgt: Masks indicating signals being blocked, ignored, and caught (see signal(7)).
 
      * CapInh, CapPrm, CapEff: Masks of capabilities enabled in inheritable, permitted, and effective sets (see capabilities(7)).
 
      * CapBnd: Capability Bounding set (since kernel 2.6.26, see capabilities(7)).
 
      * Cpus_allowed: 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_context_switches, nonvoluntary_context_switches: Number of voluntary and involuntary context switches (since Linux 2.6.23).
 
task:
进程包含的线程,子目录名是线程的ID