home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeRunning LinuxSearch 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 occupy 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 or 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:

tigger # ls /proc
1          184        25472      8          8525         kmsg
130        185        25475      82         8526         ksyms
134        186        25497      8484       8593         loadavg
136        187        25498      8485       963          locks
139        2          25499      8488       965          meminfo
143        24924      25500      8489       9654         modules
144        25441      25515      8492       968          mounts
145        25442      25549      8496       97           net
146        25445      25550      8507       99           pci
147        25446      26019      8508       cmdline      scsi
148        25449      26662      8510       cpuinfo      self
151        25451      26663      8511       devices      stat
163        25462      270        8512       dma          sys
168        25463      3          8520       filesystems  uptime
172        25464      4484       8522       interrupts   version
180        25465      4639       8523       ioports
182        25466      55         8524       kcore

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

tigger # ls /proc/172
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:

tigger # cat /proc/meminfo
        total:    used:    free:  shared: buffers:  cached:
Mem:  130957312 128684032  2273280 37888000  3198976 20615168
Swap: 133885952 64434176 69451776
MemTotal:    127888 kB
MemFree:       2220 kB
MemShared:    37000 kB
Buffers:       3124 kB
Cached:       20132 kB
SwapTotal:   130748 kB
SwapFree:     67824 kB

If you then try the command free, you can see that that is 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 IO addresses that are free. Of course, the kernel can show IO 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 that you might need for configuring new hardware as well: /proc/interrupts lists the occupied interrupt lines (IRQs) and /proc/dma lists the DMA (direct memory access) channels in use.

Library Navigation Links

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