Understanding proc file system

Source: Internet
Author: User
Understanding proc file system
From: http://linux.chinaunix.net/doc/2004-10-05/16.shtml
 

Directory:

  • /Proc --- A Virtual File System
  • Load the proc file system
  • View/proc files
  • Obtain useful system/kernel information
  • Information about running processes
  • Interaction with the kernel through/proc
  • Conclusion
  • References


 

Summary:

 

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 Linux/proc file systems 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 /proc/mounts/proc /proc 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 is consistent with other common file systems and registers itself to the virtual file system layer (VFS. 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--r--r-- 1 root root 0 Dec 25 11:01 /proc/cpuinfo$ file /proc/cpuinfo/proc/cpuinfo: empty$ cat /proc/cpuinfoprocessor       : 0vendor_id       : GenuineIntelcpu family      : 6model           : 8model name      : Pentium III (Coppermine)stepping        : 6cpu MHz         : 1000.119cache size      : 256 KBfdiv_bug        : nohlt_bug         : nosep_bug         : nof00f_bug        : nocoma_bug        : nofpu             : yesfpu_exception   : yescpuid level     : 2wp              : yesflags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mcacmov pat pse36 mmx fxsr xmmbogomips        : 1998.85processor       : 3vendor_id       : GenuineIntelcpu family      : 6model           : 8model name      : Pentium III (Coppermine)stepping        : 6cpu MHz         : 1000.119cache size      : 256 KBfdiv_bug        : nohlt_bug         : nosep_bug         : nof00f_bug        : nocoma_bug        : nofpu             : yesfpu_exception   : yescpuid level     : 2wp              : yesflags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mcacmov pat pse36 mmx fxsr xmmbogomips        : 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 mozillaroot 32558 32425 8  22:53 pts/1  00:01:23  /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/32558total 0-r--r--r--    1 root  root            0 Dec 25 22:59 cmdline-r--r--r--    1 root  root            0 Dec 25 22:59 cpulrwxrwxrwx    1 root  root            0 Dec 25 22:59 cwd -> /proc/-r--------    1 root  root            0 Dec 25 22:59 environlrwxrwxrwx    1 root  root            0 Dec 25 22:59 exe -> /usr/bin/mozilla*dr-x------    2 root  root            0 Dec 25 22:59 fd/-r--r--r--    1 root  root            0 Dec 25 22:59 maps-rw-------    1 root  root            0 Dec 25 22:59 mem-r--r--r--    1 root  root            0 Dec 25 22:59 mountslrwxrwxrwx    1 root  root            0 Dec 25 22:59 root -> //-r--r--r--    1 root  root            0 Dec 25 22:59 stat-r--r--r--    1 root  root            0 Dec 25 22:59 statm-r--r--r--    1 root  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/selfIs 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.

$ hostnamemachinename.domainname.com$ cat /proc/sys/kernel/domainnamedomainname.com$ cat /proc/sys/kernel/hostnamemachinename$ echo "new-machinename"  > /proc/sys/kernel/hostname$ hostnamenew-machinename.domainname.com

In this way, you can modify the host name by modifying the files in the/proc file system. Many of its 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 its host.

$ ping machinename.domainname.comno 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.