Linux /proc/pid目錄下各文件含義


/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
 
 


免責聲明!

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



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