Next: 10.5 Portable conditional compilation Up: 10.4 Process information and Previous: 10.4.2 ptrace and the

## 10.4.3 Process control under Linux

Under Linux, the ptrace system call is supported for process control, and it works as in 4.3BSD. To obtain process and system information, Linux also provides a /proc filesystem, but with very different semantics. Under Linux, /proc consists of a number of files providing general system information, such as memory usage, load average, loaded module statistics, and network statistics. These files are generally accessed using read and write and their contents can be parsed using scanf. The /proc filesystem under Linux also provides a directory entry for each running process, named by process ID, which contains file entries for information such as the command line, links to the current working directory and executable file, open file descriptors, and so forth. The kernel provides all of this information on the fly in response to read requests. This implementation is not unlike the /proc filesystem found in Plan 9, but it does have its drawbacks--for example, for a tool such as ps to list a table of information on all running processes, many directories must be traversed and many files opened and read. By comparison, the kvm routines used on other UNIX systems read kernel data structures directly with only a few system calls.

Obviously, each implementation is so vastly different that porting applications which use them can prove to be a real task. It should be pointed out that the SVR4 /proc filesystem is a very different beast than that found in Linux, and they may not be used in the same context. Arguably, any program which uses the kvm routines or SVR4 /proc filesystem is not really portable, and those sections of code should be rewritten for each operating system.

The Linux ptrace call is nearly identical to that found in BSD, but there are a few differences:

- The requests `PTRACE_PEEKUSER` and `PTRACE_POKEUSER` under BSD are named `PTRACE_PEEKUSR` and `PTRACE_POKEUSR`, respectively, under Linux.
- Process registers can be set using the `PTRACE_POKEUSR` request with offsets found in `/usr/include/linux/ptrace.h`.
- The SunOS requests `PTRACE_{READ,WRITE}{TEXT,DATA}` are not supported, nor are `PTRACE_SETACBKPT`, `PTRACE_SETWRBKPT`, `PTRACE_CLRBKPT`, or `PTRACE_DUMPCORE`. These missing requests should only affect a small number of existing programs.

Linux does not provide the kvm routines for reading the kernel address space from a user program, but some programs (most notably kmem_ps) implement their own versions of these routines. In general, these are not portable, and any code which uses the kvm routines is probably depending upon the availability of certain symbols or data structures in the kernel--not a safe assumption to make. Use of kvm routines should be considered architecture-specific.

Next: 10.5 Portable conditional compilation Up: 10.4 Process information and Previous: 10.4.2 ptrace and the