Running Linux, 4th Ed.Running Linux, 4th Ed.Search this book

5.6. The /proc Filesystem

Unix systems have come a long way with respect to providing uniform interfaces to different parts of the system; as you will learn in the next chapter, hardware is represented in Linux in the form of a special type of file. There is, however, a special filesystem called the /proc filesystem that goes even one step further: it unifies files and processes.

From the user's or the system administrator's point of view, the /proc filesystem looks just like any other filesystem; you can navigate around it with the cd command, list directory contents with the ls command, and view file contents with the cat command. However, none of these files and directories occupies any space on your hard disk. The kernel traps accesses to the /proc filesystem and generates directory and file contents on the fly. In other words, whenever you list a directory or view file contents in the /proc filesystem, the kernel dynamically generates the contents you want to see.

To make this less abstract, let's see some examples. The following example displays the list of files in the top-level directory of the /proc filesystem:

owl # ls /proc
1      1618  17613  27191  27317  2859   8929         kcore_elf   rtc
11120  1621  1795   27192  27320  2860   9            kmsg        scsi
11121  1649  1796   27204  27324  28746  bus          ksyms       self
11153  1657  1798   27205  27326  28747  cmdline      loadavg     slabinfo
15039  1664  1799   27221  27374  28754  config.gz    locks       stat
1512   1681  1800   27229  27377  29877  cpuinfo      lvm         swaps
1530   1689  2      27287  27379  29878  devices      mdstat      sys
1534   1703  20007  27289  27380  29944  dma          meminfo     tty
1560   1708  21391  27292  27381  3      fb           misc        uptime
1570   1709  21394  27297  27397  4      filesystems  modules     version
1578   1710  2302   27308  27515  5      fs           mounts
1585   1711  2309   27310  27518  5841   ide          mtrr
1586   1712  2356   27312  27521  5842   interrupts   net
1587   1713  27182  27314  2786   5860   ioports      partitions
1588   1731  27183  27315  28536  6100   kcore        pci

The numbers will be different on your system, but the general organization will be the same. All those numbers are directories that represent each of the processes running on your system. For example, let's look at the information about the process with the ID 1534:

tigger # ls /proc/1534
cmdline  environ  fd       mem      stat     status
cwd      exe      maps     root     statm

You see a number of files that each contain information about this process. For example, the cmdline file shows the command line with which this process was started. status gives information about the internal state of the process and cwd links to the current working directory of this process.

Probably you'll find the hardware information even more interesting than the process information. All the information that the kernel has gathered about your hardware is collected in the /proc filesystem, even though it can be difficult to find the information you are looking for.

Let's start by checking your machine's memory. This is represented by the file /proc/meminfo:

owl # cat /proc/meminfo
        total:    used:    free:  shared: buffers:  cached:
Mem:  267919360 255311872 12607488        0 40587264 77791232
Swap: 123371520  5861376 117510144
MemTotal:    261640 kB
MemFree:      12312 kB
MemShared:        0 kB
Buffers:      39636 kB
Cached:       75968 kB
BigTotal:         0 kB
BigFree:          0 kB
SwapTotal:   120480 kB
SwapFree:    114756 kB

If you then try the command free, you can see that you get exactly the same information, only the numbers are reformatted a bit. free does nothing more than read /proc/meminfo and rearrange the output a bit.

Most tools on your system that report information about your hardware do it this way. The /proc filesystem is a portable and easy way to get at this information. The information is especially useful if you want to add new hardware to your system. For example, most hardware boards need a few I/O addresses to communicate with the CPU and the operating system. If you configured two boards to use the same I/O addresses, disaster is about to happen. You can avoid this by checking which I/O addresses the kernel has already detected as being in use:

tigger # more /proc/ioports
0000-001f : dma1
0020-003f : pic1
0040-005f : timer
0060-006f : keyboard
0080-009f : dma page reg
00a0-00bf : pic2
00c0-00df : dma2
00f0-00ff : npu
01f0-01f7 : ide0
0220-022f : soundblaster
02e8-02ef : serial(auto)
0388-038b : OPL3/OPL2
03c0-03df : vga+
03f0-03f5 : floppy
03f6-03f6 : ide0
03f7-03f7 : floppy DIR
03f8-03ff : serial(auto)
0530-0533 : WSS config
0534-0537 : MSS audio codec
e000-e0be : aic7xxx
e400-e41f : eth0

Now you can look for I/O addresses that are free. Of course, the kernel can show I/O addresses only for boards that it has detected and recognized, but in a correctly configured system, this should be the case for all boards.

You can use the /proc filesystem for the other information you might need when configuring new hardware as well: /proc/interrupts lists the occupied interrupt lines (IRQs) and /proc/dma lists the DMA channels in use.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.