We can use the /proc file system to collect information about processes
in the system. A file in /proc or one of its subdirectories is actually
a program that reads kernel variables and reports them as ASCII strings.
In Linux, each file in /proc reads one or more kernel variables, for
example, /proc/meminfo provides memory statistics; each subdirectory
with numeric names contains pseudo files to read information about the
process whose PID is the same as the directory name, for example,
/proc/1/status gives a lot of general information about the init
process, including its name (init), current status (sleeping), PID (1),
UID (0), and other information.
Files in /proc are read just like ordinary ASCII files. You can read
them by typing cat /proc/cpuinfo to the shell, or you can write
a C/C++ program and then use standard library routines such as fgets()
or fscanf() to read them.
In lab exercise: write a small program to read /proc/PID/stat based
on the example here. In the example, PID = 1, i.e.,
we get the status information about the init process, while in
your program, you should be able to read the stat file for the current
running process, i.e., PID = getpid().
|