The proc file system is a virtual file system generated on the fly when the system starts. In this article, we learn about the files and directories making up this file system.
Table of contents.
- The file structure.
The proc file system is a virtual file system that is generated on the fly when the system boots up and destroyed when the system shuts down.
Files in this file system are not actual files, rather are magic objects behaving like files that provide access to parameters, data structures, and other kernel information and statistics.
$ ll /proc/version
This is one of the files, notice the size is zero, this is because it is generated by the kernel, also notice the modification date, the time stamp is similar to the time the system was booted up.
The above file holds contents involving the version number of the kernel, the same information obtained by executing the uname command:
$ uname -a
To read the file we concatenate it:
$ cat /proc/version
The file structure.
Let's now list the contents of the /proc directory:
$ ls /proc
From the output, we can see three distinct colors, blue, white, and cyan/light blue.
- Blue represents subdirectories. Some are numbered which is a representation of a specific process.
- White represents normal files containing data e.g, memory statistics, cpu information etc.
- Cyan files are symbolic links.
As we can also see some of the blue subdirectories are numbered. These numbers represent process IDs PIDS. Each directory will store information related to the process whose ID corresponds to the file name.
That is, a process will PID ** will have its data in subdirectory **.
Also notice that most of these subdirectories have similar files, such as mem for the memory the process uses, status - the current status of a process, CWD - the current working directory, syscall - the process system calls, limits - the resource limits of the process, etc. Thes files hold information about the process. For example, to get the status of a particular process we concatenate the contents of the status file in the process' subdirectory.
$ cat /proc/1800/status
In this case, the output will be the status of a process with a PID of 1800 in our example it is a gnome-session process.
We can confirm this by executing the command:
$ ps -p 1800
To view the memory details of a process we write:
$ cat /proc/[process ID]/mem
As mentioned earlier the white colored files have data about the kernel.
$ ls -p /proc/ | grep -v /
For example to view the system's memory data we write:
$ cat /proc/meminfo
This information is also obtainable using the free command.
This goes to say that when we execute Linux commands, they fetch their data from this file system and print it out as output.
For example, the uptime command displays information about the system's uptime in seconds. This command uses data in the /proc/uptime file to calculate its command output:
$ cat /proc/uptime
And now to execute the command itself:
To print data regarding the CPU we concatenate the cpuinfo file:
$ cat /proc/cpuinfo
Such information is useful in cases where we have a new system and are curious about the processor. Information such as its model number and name, vendor id, cache size, speed, etc will be displayed.
Another important file is the cmdline file. This file stores the parameters passed to the kernel when it is booted.
$ cat /proc/cmdline
Other files include:
- console - stores information about the current kernel consoles.
- devices - has information about currently configured device drivers.
- filesystems - file systems supported by the kernel.
- iomem - information about current system memory map for devices.
- locks - files locked by the kernel.
- mounts - data about mounts used by the kernel.
- pci - data about PCI devices.
- partitions - data about partitions in the kernel.
- swap - data regarding swap space.
- fb - data about frame buffer devices.
- Just to name a few.
Proc files are very useful in cases where we don't want to execute long complex text filtering commands. In such cases, we just read the appropriate file to get the raw information we need.
Also keep in mind that, some of the data in this file system is not as reliable for use in system analysis and so we should keep to commands just in case.
It is also worth noting that, we can change the configuration of the kernel by writing to some of these files.
Advanced Linux Programming - Mark Mitchell. Chapter 7.