BKJIA exclusive Article] This article describes how to use the PROC file system in Linux to obtain process information through a simple and easy-to-understand example. The information obtained through the PROC file system is mainly the virtual memory used by the process, as well as the actual memory, signal mechanism information, and other monitoring tools in Linux, be able to have a comprehensive understanding of the system running situation. First, we provide a brief introduction to the PROC file system:
The PROC file system is a virtual file system that is implemented through the interface of the file system and used to output the running status of the system. It provides an interface for communication between the operating system and application processes in the form of a file system, this allows applications to securely and conveniently obtain the current operating status of the system and internal data information of the kernel, and modify the configuration information of some systems. In addition, because PROC is implemented using the interface of the file system, you can access it just like accessing a common file, but it only exists in the memory, it does not exist in a real physical disk. Therefore, when the system is restarted and powered off, all data and information in the system will disappear.
Table 1 describes some important files and directories in the file system.
Table 1 important PROC file system files and directories
| File or directory |
Description |
| /Proc/1 |
The information directory of process 1. Each process/ProcThere is a directory named its process number |
| /Proc/cpuinfo |
Processor information, such as type, manufacturer, model, and performance |
| /Proc/devices |
List of device drivers with core configurations currently running |
| /Proc/dma |
Display the currently used DMA Channel |
| /Proc/filesystems |
Core Configuration File System |
| /Proc/interrupts |
Display interruption of use |
| /Proc/ioports |
Current I/O port |
| /Proc/kcore |
System physical memory image |
| /Proc/kmsg |
Core output messages are also sentSyslog |
| /Proc/ksyms |
Core symbol table |
| /Proc/loadavg |
Average system load |
| /Proc/meminfo |
Memory usage information, including physical memory and swap |
| /Proc/modules |
Core modules currently loaded |
| /Proc/net |
Network Protocol status information |
| /Proc/stat |
Different States of the system |
| /Proc/version |
Core version |
| /Proc/uptime |
System Startup duration |
It is worth noting that all of the above files provide readable text files, sometimes in a format that is not easy to read. Many commands have been formatted to make it easier to read. For example, the free program reads/proc/meminfo and converts the number of bytes to kilobytes and adds some information ).
The following example shows how to use the PROC file system to obtain process information.
First, use the vi editor to create a c source program file and compile it to form the target file. The main function of this file is to compute it, save it in the/root directory, and run it below:
# Cd/root // switch directory #./calculate // run this program, a process named after this program is generated
Using the ps command, you can run a process like calculate in the system:
#psroot 2108 61.2 0.1 1344 224 pts/0 R 21:20 0:11 ./calculate……
The basic information of the process is stored in the/proc file system. The specific location is in the/proc directory. Run the following command to view information about processes running in the system:
# Ls/proc // view the content in the/proc directory // The following shows the directory where information about running processes in the system is stored. Each process corresponds to a directory, add blue 2108 directory 1 1790 1922 2049 2083 8 fs meminfo swaps10 1799 1923 2056 2108 9 ide misc sys11 1809 1924 2063 2111 apm interrupts modules sysvipc1491 1818 1925 2065 2138 bus iomem mounts tty1550 1829 1968 2067 2162 running line ioports mtrr uptime1554 1893 1969 2069 cpuinfo irq net version1572 19 2163 1978 3 devices kcore partitions1591 2071 2 1902 4 dma kmsg pci1670 2073 1911 2032 2074 5 driver ksyms scsi1720 1919 2043 2079 6 execdomains loadavg self1757 1920 2045 2081 1921 7 fb locks slabinfo1771 2047 2082 2108 77 filesystems mdstat # cd 2108 // switch to the directory, to view the process information in detail # ls // list the detailed State information file of the process using line cwd environ exe fd maps mem mounts root stat statm status
Among these files, the status file is very important and contains a lot of useful information about the process. You can obtain information from this file, perform the following operations to list the file content:
# Cat status // use the cat command to list the status File Content Name: calculate // process Name State: R (running) // process running status Tgid: 2108 // process group IDPid: 2108 // process IDPPid: 2083 // parent process IDTracerPid: 0 // trace the debugging process IDUid: 0 0 0 0 // UIDGid of the program corresponding to the process: 0 0 0 0 // The GIDFDSize of the program corresponding to the process: 256 // the size of the file handle used by the Process Groups: 0 1 2 3 4 10 10 // group information // virtual memory used by the process, as well as actual memory and signal mechanism information VmSize: 1344 kBVmLck: 0 kBVmRSS: 224 kBVmData: 12 kBVmStk: 16 kBVmExe: 4 kBVmLib: 1292 kBSigPnd: 0000000000000000 SigBlk: 0000000000000000 SigIgn: 8000000000000000 SigCgt: 0000000000000000 CapInh: 0000000000000000 CapPrm: 00000000 fffffeffCapEff: 00000000 fffffeff
In this way, we can know the virtual memory used by the process, as well as the actual memory and signal mechanism information.