Chapter 7: The /proc File System

Try invoking the mount command without arguments -- this displays the file systems currently mounted on your GNU/Linux computer. You'll see one line that looks like this:

none on /proc type proc (rw)

This is the special /proc file system. Notice that the first field, none, indicates that this file system isn't associated with a hardware device such as a disk drive. Instead, /proc is a window into the running Linux kernel. Files in the /proc file system don't correspond to actual files on a physical device. Instead, they are magic objects that behave like files but provide access to parameters, data structures, and statistics in the kernel. The "contents" of these files are not always fixed blocks of data, as ordinary file contents are. Instead, they are generated on the fly by the Linux kernel when you read from the file. You can also change the configuration of the running kernel by writing to certain files in the /proc file system. Let's look at an example:

% ls -l /proc/version
-r--r--r-- 1 root root 0 Jan 17 18:09 /proc/version

Note that the file size is zero; because the file's contents are generated by the kernel, the concept of file size is not applicable. Also, if you try this command yourself, you'll notice that the modification time on the file is the current time.

What's in this file? The contents of /proc/version consist of a string describing the Linux kernel version number. It contains the version information that would be obtained by the uname system call, described in Chapter 8: Linux System Calls, in Section 8.15, "uname", plus additional information such as the version of the compiler that was used to compile the kernel. You can read from /proc/version like you would any other file. For instance, an easy way to display its contents is with the cat command.

% cat /proc/version
Linux version 2.2.14-5.0 ( (gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)) #1 Tue Mar 7 21:07:39 EST 2000

The various entries in the /proc file system are described extensively in the proc man page (Section 5). To view it, invoke this command:

% man 5 proc

In this chapter, we'll describe some of the features of the /proc file system that are most likely to be useful to application programmers, and we'll give examples of using them. Some of the features of /proc are handy for debugging, too.

If you're interested in exactly how /proc works, take a look at the source code in the Linux kernel sources, under /usr/src/linux/fs/proc/.


Download PDF.