/proc目录及proc文件系统

参考文档

使用 /proc 文件系统来访问 Linux 内核的内容

Linux下/proc目录简介


/proc目录简介


# cat /etc/fstab 
proc            /proc                   proc    defaults                0       0
tmpfs           /tmp                    tmpfs   defaults                0       0
sysfs           /sys                    sysfs   defaults                0       0
tmpfs           /dev                    tmpfs   defaults                0       0
var             /dev                    tmpfs   defaults                0       0
ramfs           /dev                    ramfs   defaults                0       0
debugfs         /sys/kernel/debug       debugfs defaults                0       0

1/PROC目录及proc文件系统

/PROC里面的文件是proc文件系统对应的文件.

Linux内核提供了一种通过 proc 文件系统,在运行时访问内核内部数据结构、改变内核设置的机制.

proc文件系统是一个内存文件系统,它只存在内存当中,而不占用外存空间.

在开机前,只存在/proc/目录,目录中无文件

在开机后,proc文件系统将有关进程的信息写入/proc目录中,即创建目录文件和普通文件来标识进程的相关信息.

然后我们可以通过目录中的文件来读取进程相关信息,但部分可以通过写来改变内核环境.?

最初开发 /proc 文件系统是为了提供有关系统中进程的信息。但是由于这个文件系统非常有用,因此内核中的很多元素也开始使用它来报告信息,或启用动态运行时配置。


2/PROC目录中的文件

linux@ubuntu:/proc$ ls
1      14    2189  2289  24    2493  2631  451   663   7481  8041  912  998          fb             meminfo       sys
10     15    22    2290  2407  25    2638  4603  670   7482  8043  915  999          filesystems    misc          sysrq-trigger
1000   1562  2200  2298  2411  2501  2639  4729  7     757   8044  916  acpi         fs             modules       sysvipc
1005   16    221   23    2416  2505  2713  5     7145  759   8093  917  asound       interrupts     mounts        timer_list
1019   17    2229  2303  2417  2506  2718  522   717   7618  8107  921  buddyinfo    iomem          mpt           timer_stats
11     1760  2237  2304  2419  2508  2726  524   7225  7619  8219  923  bus          ioports        mtrr          tty
1148   1786  2240  2305  2424  2511  3     540   7226  766   8221  933  cgroups      irq            net           uptime
12     18    2241  2315  2426  2512  333   545   733   7675  8346  940  cmdline      kallsyms       pagetypeinfo  version
12827  1834  2250  2333  2429  2516  337   591   7385  7679  8351  968  consoles     kcore          partitions    version_signature
12918  1969  2261  2341  2432  2541  35    6     7387  769   860   988  cpuinfo      key-users      sched_debug   vmallocinfo
12938  2     2263  2350  2435  2542  38    62    7390  790   866   991  crypto       kmsg           schedstat     vmstat
12975  204   2269  2353  2436  2544  39    63    7392  792   867   992  devices      kpagecount     scsi          zoneinfo
12994  2062  2279  2358  2442  2548  40    631   7394  793   880   993  diskstats    kpageflags     self
13     207   2283  2360  246   2592  42    632   7396  7941  882   994  dma          latency_stats  slabinfo
13016  209   2285  2365  247   26    441   64    7398  7949  887   995  dri          loadavg        softirqs
13059  21    2286  2386  2476  2612  442   646   7400  8     8989  996  driver       locks          stat
1333   210   2288  2392  2492  2630  4484  6609  7402  8039  9     997  execdomains  mdstat         swaps

3/PROC文件分类

3.1/以数字为名的文件夹

最小的数字为1,最大的数字为四位数,例如8989

linux@ubuntu:/proc$ l /proc/8989/ 
attr/      clear_refs       cpuset   fd/      limits      mem         net/       oom_score_adj  sched      stack   syscall
autogroup  cmdline          cwd@     fdinfo/  loginuid    mountinfo   ns/        pagemap        schedstat  stat    task/
auxv       comm             environ  io       map_files/  mounts      oom_adj    personality    sessionid  statm   wchan
cgroup     coredump_filter  exe@     latency  maps        mountstats  oom_score  root@          smaps      status

其中

attr		提供安全相关的属性
cgroup 进程所属的控制组

cmdline 	程序启动时的命令行
environ 环境变量值
fd 一个包含所有文件描述符的目录
mem 进程的内存被录用情况
status 		当前进程的状态
cwd 当前你工作目录的链接
exe 指向 该进程的执行命令文件
maps 内存映射信息
statm 进程内存使用信息
root 链接此进程的root 目录
oom_adj oom_score oom_score_adj 用于oom killer

3.2/普通文件

linux@ubuntu:/proc$ ls -al | grep '^-'
-r--r--r--   1 root       root                0  2月 26 10:45 buddyinfo
-r--r--r--   1 root       root                0  2月 26 10:45 cgroups
-r--r--r--   1 root       root                0  2月 26 10:45 cmdline
-r--r--r--   1 root       root                0  2月 26 10:45 consoles
-r--r--r--   1 root       root                0  2月 26 10:45 cpuinfo
-r--r--r--   1 root       root                0  2月 26 10:45 crypto
-r--r--r--   1 root       root                0  2月 26 10:45 devices
-r--r--r--   1 root       root                0  2月 26 10:45 diskstats
-r--r--r--   1 root       root                0  2月 26 10:45 dma
-r--r--r--   1 root       root                0  2月 26 10:45 execdomains
-r--r--r--   1 root       root                0  2月 26 10:45 fb
-r--r--r--   1 root       root                0  2月 26 10:45 filesystems
-r--r--r--   1 root       root                0  2月 26 10:45 interrupts
-r--r--r--   1 root       root                0  2月 26 10:45 iomem
-r--r--r--   1 root       root                0  2月 26 10:45 ioports
-r--r--r--   1 root       root                0  2月 26 10:45 kallsyms
-r--------   1 root       root       1069543424  2月 26 10:45 kcore
-r--r--r--   1 root       root                0  2月 26 10:45 key-users
-r--------   1 root       root                0  2月 23 20:50 kmsg
-r--------   1 root       root                0  2月 26 10:45 kpagecount
-r--------   1 root       root                0  2月 26 10:45 kpageflags
-rw-r--r--   1 root       root                0  2月 26 10:45 latency_stats
-r--r--r--   1 root       root                0  2月 26 10:45 loadavg
-r--r--r--   1 root       root                0  2月 26 10:45 locks
-r--r--r--   1 root       root                0  2月 23 20:51 mdstat
-r--r--r--   1 root       root                0  2月 23 20:51 meminfo
-r--r--r--   1 root       root                0  2月 26 10:45 misc
-r--r--r--   1 root       root                0  2月 26 10:45 modules
-rw-r--r--   1 root       root                0  2月 23 20:50 mtrr
-r--r--r--   1 root       root                0  2月 26 10:45 pagetypeinfo
-r--r--r--   1 root       root                0  2月 26 10:45 partitions
-r--r--r--   1 root       root                0  2月 26 10:45 sched_debug
-r--r--r--   1 root       root                0  2月 26 10:45 schedstat
-r--------   1 root       root                0  2月 26 10:45 slabinfo
-r--r--r--   1 root       root                0  2月 26 10:45 softirqs
-r--r--r--   1 root       root                0  2月 23 20:51 stat
-r--r--r--   1 root       root                0  2月 26 10:45 swaps
--w-------   1 root       root                0  2月 26 10:45 sysrq-trigger
-r--r--r--   1 root       root                0  2月 26 10:45 timer_list
-rw-r--r--   1 root       root                0  2月 26 10:45 timer_stats
-r--r--r--   1 root       root                0  2月 26 10:45 uptime
-r--r--r--   1 root       root                0  2月 26 10:45 version
-r--r--r--   1 root       root                0  2月 26 10:45 version_signature
-r--------   1 root       root                0  2月 26 10:45 vmallocinfo
-r--r--r--   1 root       root                0  2月 23 20:51 vmstat
-r--r--r--   1 root       root                0  2月 26 10:45 zoneinfo

档名	                                     文件内容
/proc/cmdline		加载 kernel 时所下达的相关参数!查阅此文件,可了解系统是如何启动的!
/proc/cpuinfo		本机的 CPU 的相关资讯,包含时脉、类型与运算功能等
/proc/devices		这个文件记录了系统各个主要装置的主要装置代号,与 mknod 有关呢!
/proc/filesystems	目前系统已经加载的文件系统罗!
/proc/interrupts	目前系统上面的 IRQ 分配状态。
/proc/ioports		目前系统上面各个装置所配置的 I/O 位址。
/proc/kcore			这个就是内存的大小啦!好大对吧!但是不要读他啦!
/proc/loadavg		还记得 top 以及 uptime 吧?没错!上头的三个平均数值就是记录在此!
/proc/meminfo		使用 free 列出的内存资讯,嘿嘿!在这里也能够查阅到!
/proc/modules		目前我们的 Linux 已经加载的模块列表,也可以想成是驱动程序啦!
/proc/mounts		系统已经挂载的数据,就是用 mount 这个命令呼叫出来的数据啦!
/proc/swaps			到底系统挂加载的内存在哪里?呵呵!使用掉的 partition 就记录在此啦!
/proc/partitions	使用 fdisk -l 会出现目前所有的 partition 吧?在这个文件当中也有纪录喔!
/proc/pci			在 PCI 汇流排上面,每个装置的详细情况!可用 lspci 来查阅!
/proc/uptime		就是用 uptime 的时候,会出现的资讯啦!
/proc/version		核心的版本,就是用 uname -a 显示的内容啦!
/proc/bus/*			一些汇流排的装置,还有 U盘 的装置也记录在此喔!

3.3/其他文件夹

dr-xr-xr-x  2 root       root       0  2月 23 20:50 acpi/
dr-xr-xr-x  5 root       root       0  2月 26 10:38 asound/
dr-xr-xr-x  4 root       root       0  2月 26 10:38 bus/
dr-xr-xr-x  3 root       root       0  2月 26 10:38 dri/
dr-xr-xr-x  2 root       root       0  2月 26 10:38 driver/
dr-xr-xr-x  8 root       root       0  2月 23 20:50 fs/
dr-xr-xr-x 56 root       root       0  2月 26 10:38 irq/
dr-xr-xr-x  3 root       root       0  2月 26 10:38 mpt/
dr-xr-xr-x  6 linux      linux      0  2月 26 10:38 net/
dr-xr-xr-x  5 root       root       0  2月 26 10:38 scsi/
dr-xr-xr-x  1 root       root       0  2月 23 20:50 sys/
dr-xr-xr-x  2 root       root       0  2月 26 10:38 sysvipc/
dr-xr-xr-x  4 root       root       0  2月 26 10:38 tty/

3.4链接文件

linux@ubuntu:/proc$ ls -al | grep '^l'
lrwxrwxrwx   1 root       root               11  2月 26 10:46 mounts -> self/mounts
lrwxrwxrwx   1 root       root                8  2月 26 10:46 net -> self/net
lrwxrwxrwx   1 root       root               64  2月 23 20:50 self -> 13087

self文件是个连接,但又好像是个目录
在我键入ls的时候,self文件的内容就是ls进程的信息
cat /proc/self/status 的时候,打印的信息是 cat /proc/self/status" 进程的信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值