Thursday, March 14, 2013

Linux -- study of Procfs (pseudo-filesystem)


Procfs is a pseudo-filesystem (like sysfs and several others), which means that files in /proc do not exist in your hard drive, but the information they have is calculated on demand. Like the rest of filesystems used in Linux, procfs is supported by the Virtual File System (VFS). VFS is a kernel layer that provides abstraction when working with file systems, so that it handles the differences between file systems and shows a common interface to work with them. Although other Unix-like systems provide a procfs (FreeBSD, for example), the format varies between systems. Linux uses a plain text format and FreeBSD uses a binary format in some places. The first approach is better when working with shell commands like cat, grep, etc, but the second one is better when programming. Under /proc we can find general system information and specific process information and statistics. Linux distinguishes different types of information with the inode number. An inode number in Linux is represented as a 32 bit number and a PID (Process Identifier) is represented as a 16 bit number. With this schema, Linux splits the inode number in two halves of 16 bit. The left half is interpreted as a PID number and the right one is interpreted as a class of information. Since a PID=0 is not valid, Linux uses this value to indicate that inode contains global information. What kernel does when we type for example cat /proc/cpuinfo is showed in Illustration below. First of all, the process created by the shell requests data by reading the file. VFS catches the request and establishes the kind of file to read is a procfs file (actually, the file is a pseudo-file). The procfs subsytem queries the kernel tables to find the information required by the process. The kernel structures asked depend on the type of information the process wants (global, specific, about cpu, a process, etc). After the data have been collected, the process̢۪ buffer is filled. The most important aspect is that this process of information gathering is completely transparent from an external point of view.

No comments:

Post a Comment