sections in this module | City
College of San Francisco - CS260A Linux System Administration Module: Processes |
module list |
/proc
Preview question: On linux, list the directory /proc. Note the subdirectory for each running process, named for the process id. Can you easily list the /proc subdirectory for your login shell? |
Gathering statistics for the running system or about individual processes can be difficult. The information, however, is available in the kernel - the data is contained in standard kernel tables: it is just the interface that is cumbersome.
There are tasks that require easy access to process and system information. We have been learning about it in the context of system monitoring and process control, but some tasks, such as writing an interactive program debugger, have much greater need for easy access.
One brilliant idea for streamlining this facility was developed at Sun and has been adopted and enhanced by linux. This presents access to much kernel information in an interface that looks like a filesystem. This 'filesystem' is called /proc.
'files' that appear on /proc are not files at all. The same operations that would examine (or, in some cases, change) files are converted to requests to the kernel. The result is presented in a text format that makes it seem like the data is really in a file.
/proc has two parts: per-process information, which is contained in subdirectories of /proc named for the process-id, and system-wide information, which is contained in the many 'files' found in the directory /proc itself, as well as in subdirectories named for topics. We will examine the per-process information first.
The per-process information is presented in a standard set of 'files' organized beneath a directory named for the process-id.
[gboyd@nelson proc]$ ls -F
/proc/$$
attr/
cpuset
fd/
maps
oom_adj smaps task/
auxv
cwd@
limits
mem
oom_score stat wchan
cmdline
environ limits
mounts
root@ statm
coredump_filter exe@ loginuid
mountstats schedstat status
[gboyd@nelson proc]$
Some interesting (and useful) pieces of data here are:
name | contents |
cmdline | the command-line of the process, in a set of null-terminated strings |
environ | the process' environment variables, again as null-terminated strings |
cwd | a symlink to the process' current working directory |
exe | a symlink to the process' executable file |
status | a list of statistics about the current process, including everything needed by ps(1) |
fd | a set of symlinks to each of the files opened for the process. The symlinks are named the same as the file number (0=stdin,1=stdout,...) |
The system-wide information is just as interesting. A sampling of the files and directories you will find in /proc and their contents is below:
name | contents |
cmdline | the kernel command-line at boot. (this shows options the kernel was invoked with) |
cpuinfo | the type(s) of cpu(s) on the current system and their
features. One important flag here is the "virtualization
supported" flag, which is vmx on Intel Machines and svm
on AMD machines. Note that virtualization must be enabled in
the BIOS for Intel machines as well. (Look for a way to
enable hardware-assisted virtualization.) The KVM
virtualization system only runs on 64-bit installations with
hardware-assisted virtualization enabled. |
devices | the major number of the different devices known by your system |
filesystems | the types of filesystems supported by your kernel. those indicated as nodev are unused on the current system (there are no instances of those types of filesystems.) |
meminfo | RAM usage |
modules | kernel modules loaded |
partitions | information on disk partitions, including the major and minor number of the device the partition is mounted on. |
stat | system statistics since boot |
version | kernel version and build information |
There are also system directories beneath /proc. The sys subdirectory contains useful parameters that can be modified by the administrator simply by writing to the appropriate file. We will discuss one of these facilities in the next section, the sys request feature.
For more details on the /proc contents, see proc(5)The sys request feature
The sys request feature provides the administrator with a way to debug a running system that is having problems. Normally, if the system halts or hangs due to a software problem, the only thing you can do is restart it. The information about what the kernel was doing is lost, as may be the most recent log information or even some data. The sys request feature enables a hot key that can be used from any virtual console (CNTL-ALT-F1...5). This hot key enables you to do emergency system operations when you cannot get a command to run. Here is how it works:
You enable the sysrq feature by having the administrator set /proc/sys/kernel/sysrq to 1:
echo 1 > /proc/sys/kernel/sysrq
If there is a system hang you can simply switch to a virtual console and type alt+sysrq+X simultaneously, where X is one of the following (this is only a partial list - much more dangerous things can be done with sysrq. They are not listed):
X | meaning |
s | attempt to sync discs (flush all pending writes) |
u | attempts to unmount all filesystems and remount them read-only |
p | output all flags and registers to the console |
m | output memory statistics |
t | output a list of the processes |
The sysrq feature should be disabled (by setting /proc/sys/kernel/sysrq to
0, which is the
default) unless you have reason to believe it is needed, as the
sysrq feature can be used by anyone with access to a console, and
it can be used to crash the system, losing files and other
information. Note that sys requests can be made remotely as well,
by writing to /proc/sysrq-trigger
from a remote login as root.
(Note: sysrq does not seem to be enabled on newer versions of linux. Perhaps it was deemed too dangerous and was disabled.)
the /sys filesystem
Besides /proc, which is created and managed once the system is up and running, basic information about the system such as that provided to the kernel from the boot program is encapsulated in the /sys filesystem. As an example, try this command:
cat /sys/block/sda/size
to get the number of 512-byte blocks on the disk 'sda'. Check out the other 'files' in /sys/block/sda
Prev | This page was made entirely
with free software on linux: Kompozer, the Mozilla Project and Openoffice.org |
Next |