Linux Proc Pseudo Filesystem

The proc directory

Proc stands for process in linux. As we know, that “Everything is a file” in linux. Keeping true to this analogy, all the processes are organized and processed inside the proc directory.

/proc/ is the process information and pseudo filesystem. All the running processes have their directories inside the proc folder. The general syntax of directories is /proc/[pid] , You can easily find the pid of a running process by a simple ps -A | grep <process_name>

File descriptors

Each running process can have multiple file descriptors. All of the active file descriptors can be found at /proc/[pid]/fd folder.

This subdirectory contains one entry for each file which the process has open, named by its file descriptor, and which is a sym link to the actual file.

If you do a ls -l in the directory you will receive something similar -

Screenshot

In linux, all the file descriptors are numbered. 0, 1, 2 are the reserved file descriptors for STDIN, STDOUT, STDERR recpectively.

In the above screenshot, we can observe that file descriptor 14 points to the current music file that is being played. \<the proces is the vlc media player>

There is one more interesting directory related to file descriptors of the process it is -

/proc/[pid]/fdinfo/ - This subdirectory contains one entry for each of the file descriptors of the process. The files contains the position of the file descriptor and the flags with which the file has been opened

Other interesting findings -

/proc/[pid]/exe - This symlinks to the executable file of the process.

/proc/[pid]/environ - This file contains all the environment variables that are available to the process. Best way to see it is using the strings program. strings environ

/proc/[pid]/cwd - It points to the current working directory of the process. Symlink to the current working directory.

/proc/[pid]/io - Contains all the I/O stats for the process. Like how many bytes the process has read, written, number of syscalls etc.

/proc/[pid]/oom_score - Linux kernel has a thing called “Out Of Memory” killer. OOM killer kills a process in case the system is running out of memory. Higher the OOM score of a process, higher is the likelihood that it will be killed by the kernel.

The OOM score of a process is calculated on basis of what percentage of memory the process is using. The OOM score can vary between -16 to +15. Score of -17 means that the process cannot be killed by OOM killer.

That is all, although far from complete. /proc/ contains tons of information about the system, which I did not found as relevant.

Comments