Understanding Proc file system

Source: Internet
Author: User

 
 
Abstract:
 
 
The Linux Kernel provides a mechanism to access the internal data structure of the kernel and change the kernel settings through the/proc file system during runtime. Although the basic concepts of the/proc file system for Linux systems on various hardware platforms are the same, this article only discusses the Linux/proc file system based on the intel x86 architecture.
 
 
 
___________________________________________________
 
 
/Proc --- A Virtual File System
The/proc file system is a mechanism used by the kernel and kernel module to send information to the process (so it is called/proc ). This pseudo file system allows you to interact with the internal data structure of the kernel to obtain useful information about the process. on the fly, you can change the settings (by changing the kernel parameters ). Unlike other file systems,/proc exists in the memory rather than on the hard disk. If you view the file/proc/mounts (like the mount command to list all mounted file systems), you will see a line like this:
 
 
 
Grep proc/mounts
/Proc rw 0 0
 
/Proc is controlled by the kernel and there is no device carrying/proc. Because/proc stores state information controlled by the kernel, the logical location of most of this information is in the memory controlled by the kernel. If you perform 'LS-l' on/proc, you can see that most files are 0 bytes large. However, you can see some information when you view these files. How is this possible? This is because the/proc file system registers itself to the virtual file system layer (VFS) like other conventional file systems. However, the/proc file system will not establish the corresponding files and directories based on the information in the kernel until VFS calls it to request files and directories of I-nodes.
 
 
Load the proc file system
If the proc file system is not loaded yet, run the following command to load the proc file system:
 
 
Mount-t proc/proc
The above command will successfully load your proc file system. For more details, see the man page of the mount command.
 
 
View/proc files
/Proc files can be used to access information about the Kernel Status, computer attributes, and the status of running processes. Most files and directories in/proc provide the latest information about the physical environment of the system. Although files in/proc are virtual, they can still be viewed using any file editor or programs such as 'more', 'less ', or 'cat. When the editing program tries to open a virtual file, the file is created out of thin air by the information in the kernel. Here are some interesting results I got from my system:
 
$ Ls-l/proc/cpuinfo
-R -- 1 root 0 Dec 25 :01/proc/cpuinfo
 
$ File/proc/cpuinfo
/Proc/cpuinfo: empty
 
$ Cat/proc/cpuinfo
 
Processor: 0
Vendor_id: GenuineIntel
Cpu family: 6
Model: 8
Model name: Pentium III (Coppermine)
Stepping: 6
Cpu MHz: 1000.119
Cache size: 256 KB
Fdiv_bug: no
Hlt_bug: no
Sep_bug: no
F00f_bug: no
Coma_bug: no
Fpu: yes
Fpu_exception: yes
Cpuid level: 2
Wp: yes
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca
Cmov pat limit 36 mmx fxsr xmm
Bogomips: 1998.85
 
Processor: 3
Vendor_id: GenuineIntel
Cpu family: 6
Model: 8
Model name: Pentium III (Coppermine)
Stepping: 6
Cpu MHz: 1000.119
Cache size: 256 KB
Fdiv_bug: no
Hlt_bug: no
Sep_bug: no
F00f_bug: no
Coma_bug: no
Fpu: yes
Fpu_exception: yes
Cpuid level: 2
Wp: yes
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca
Cmov pat limit 36 mmx fxsr xmm
Bogomips: 1992.29
 
 
This is a result obtained from a dual-CPU system. Most of the above information clearly shows the useful hardware information of this system. Some/proc files are encoded. Different tools can be used to interpret the encoded information and output it into a readable form. Such tools include 'top', 'ps', and 'apm.
 
 
 
Obtain useful system/kernel information
 
The proc file system can be used to collect useful information about the system and the running kernel. The following are some important documents:
 
/Proc/cpuinfo-CPU information (model, family, cache size, etc)
/Proc/meminfo-physical memory, swap space, and other information
/Proc/mounts-List of mounted file systems
/Proc/devices-list of available devices
/Proc/filesystems-Supported file systems
/Proc/modules-loaded modules
/Proc/version-kernel version
/Proc/cmdline-kernel command line parameters entered at system startup
In proc, there are more than the files listed above. Readers who want to learn more can get more information about every file in/proc or read references [1. I recommend that you use 'more' instead of 'cat' unless you know that this file is small, because some files (such as kcore) may be very long.
 
 
Information about running processes
The/proc file system can be used to obtain information about running processes. There are some numbered subdirectories in/proc. Each numbered directory corresponds to a process id (PID ). In this way, each running process/proc has a directory named after its PID. These subdirectories contain files that provide important details about the process status and environment. Let's try to find a running process.
 
$ Ps-aef | grep mozilla
Root 32558 32425 8 00:01:23 pts/1/usr/bin/mozilla
 
The preceding command shows that the PID of a running mozilla process is 32558. Correspondingly,/proc should have a directory named 32558
 
 
$ Ls-l/proc/32558
Total 0
-R -- 1 root 0 Dec 25 offline line
-R -- 1 root 0 Dec 25 22:59 cpu
Lrwxrwxrwx 1 root 0 Dec 25 22:59 cwd->/proc/
-R -------- 1 root 0 Dec 25 22: 59 environ
Lrwxrwxrwx 1 root 0 Dec 25 exe->/usr/bin/mozilla *
Dr-x ------ 2 root 0 Dec 25 22:59 fd/
-R -- 1 root 0 Dec 25 22:59 maps
-Rw ------- 1 root 0 Dec 25 22:59 mem
-R -- 1 root 0 Dec 25 22:59 mounts
Lrwxrwxrwx 1 root 0 Dec 25 root-> //
-R -- 1 root 0 Dec 25 22:59 stat
-R -- 1 root 0 Dec 25 22:59 statm
-R -- 1 root 0 Dec 25 22:59 status
 
The "cmdline" file contains the command line called when the process is started. The environment of the "envir" process is changed to two. "Status" indicates the status of a process, including the user ID (UID), group ID (GID), parent process ID (PPID), and current status of the process, for example, "Sleelping" and "Running ". Each process directory has several symbolic links. "cwd" is a symbolic link pointing to the current working directory of the process. "exe" points to the executable program of the running process, "root" points to the directory (usually "/") of the root directory "/"). The directory "fd" contains a link to the file descriptor used by the process. "Cpu" only appears when running the SMP kernel, which is the process time by CPU.
 
/Proc/self is an interesting sub-Directory, which allows the program to conveniently use/proc to find information about the local process. /Proc/self is a symbolic link to the PID directory of the process accessing/proc in/proc.
 
 
 
Interaction with the kernel through/proc
 
Most of the/proc files discussed above are read-only. In fact, the/proc file system provides an interaction mechanism for the kernel through/proc files that can be read and written. Writing these files can change the state of the kernel. Therefore, you must modify these files with caution. The/proc/sys directory stores all the readable and writable files and can be used to change kernel behavior.
 
/Proc/sys/kernel-this directory contains information about General kernel behavior. /Proc/sys/kernel/{domainname, hostname} stores the domain name and Host Name of the machine/network. These files can be used to modify these names.
 
 
 
$ Hostname
Machinename.domainname.com
 
$ Cat/proc/sys/kernel/domainname
Domainname.com
 
$ Cat/proc/sys/kernel/hostname
Machinename
 
$ Echo "new-machinename">/proc/sys/kernel/hostname
 
$ Hostname
New-machinename.domainname.com
 
 
In this way, you can modify the host name by modifying the files in the/proc file system. Many other configurable files exist in/proc/sys/kernel /. It is not possible to list all these files here. Readers can view them in this directory to obtain more details.
Another configurable directory is/proc/sys/net. Files in this directory can be used to modify Network Properties of machines/networks. For example, simply modifying a file allows you to become addicted to computers hiding in the Internet.
 
 
$ Echo 1>/proc/sys/net/ipv4/icmp_echo_ignore_all
 
This will hide your machine in the Network addiction because it does not respond to icmp_echo. The host will not respond to ping queries from other hosts.
 
 
$ Ping machinename.domainname.com
No answer from machinename.domainname.com
 
To change back to the default setting, as long
 
$ Echo 0>/proc/sys/net/ipv4/icmp_echo_ignore_all
 
There are many others in/proc/sys that can be used to change the kernel attributes. For more information, see references [1] and [2.
 
 
Conclusion
The/proc file system provides a file-based internal Linux interface. It can be used to determine the status of various devices and processes of the system. Configure them. Therefore, understanding and application knowledge about this file system is the key to understanding your Linux system.
 
 
 
 
References
 
 
 
[1] The Linux proc file system Documentation is located at:/usr/src/linux/Documentation/filesystems/proc.txt
 
[2] RedHat Guide: The/proc File System: http://www.redhat.com/docs/manuals/linux/RHL-7.3-Manual/ref-guide/ch-proc.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.