Classic Shell Scripting - Arnold Robbins [188]
The three already-open files suffice for many programs, which can use them without the burden of having to deal with file opening and closing, and without having to know anything about filename syntax, or filesystems.
Wildcard expansion by the shell removes a significant burden from programs and provides uniform handling of command lines.
The environment space provides another way to supply information to processes, beyond their command lines and input files.
Process Listing
The most important command for listing processes is the process status command, ps. For historical reasons, there are two main flavors of ps: a System V style and a BSD style. Many systems provide both, although sometimes one of them is part of an optional package. On our Sun Solaris systems, we have:
$ /bin/ps
System V-style process status
PID TTY TIME CMD
2659 pts/60 0:00 ps
5026 pts/60 0:02 ksh
12369 pts/92 0:02 bash
$ /usr/ucb/ps
BSD-style process status
PID TT S TIME COMMAND
2660 pts/60 O 0:00 /usr/ucb/ps
5026 pts/60 S 0:01 /bin/ksh
12369 pts/92 S 0:02 /usr/local/bin/bash
Without command-line options, their output is quite similar, with the BSD style supplying a few more details. Output is limited to just those processes with the same user ID and same controlling terminal as those of the invoker.
Like the file-listing command, ls, the ps command has many options, and both have considerable variation across Unix platforms. With ls, the -l option requesting the long output form is used frequently. To get verbose ps output, we need quite different sets of options. In the System V style, we use:
$ ps -efl
System V style
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
19 T root 0 0 0 0 SY ? 0 Dec 27 ? 0:00 sched
8 S root 1 0 0 41 20 ? 106 ? Dec 27 ? 9:53 /etc/init -
19 S root 2 0 0 0 SY ? 0 ? Dec 27 ? 0:18 pageout
19 S root 3 0 0 0 SY ? 0 ? Dec 27 ? 2852:26 fsflush
...
whereas in the BSD style, we use:
$ ps aux
BSD style
USER PID %CPU %MEM SZ RSS TT S START TIME COMMAND
root 3 0.4 0.0 0 0 ? S Dec 27 2852:28 fsflush
smith 13680 0.1 0.2 1664 1320 pts/25 O 15:03:45 0:00 ps aux
jones 25268 0.1 2.02093619376 pts/24 S Mar 22 29:56 emacs -bg ivory
brown 26519 0.0 0.3 5424 2944 ? S Apr 19 2:05 xterm -name thesis
...
Both styles allow option letters to be run together, and the BSD style allows the option hyphen to be dropped. In both examples, we removed excess whitespace to make the lines fit on the page.
There are some design infelicities in both styles, occasioned by the need to display a lot of information in too little space: process start dates may be abbreviated differently, commands in the last field are truncated, and column values can run together. The latter misfeature makes it hard to filter ps output reliably.
The USER and UID fields identify the owner of a process: that can be critical information if a process is hogging the system.
The PID value is the process ID, a number that uniquely identifies the process. In the shell, that number is available as $$: we use it in other chapters to form unique names of temporary files. Process ID assignments start out at zero, and increment for each new process throughout the run life of the system. When the maximum representable integer is reached, process numbering starts again at zero, but avoids values that are still in use for other processes. A typical single-user system might have a few dozen active processes, whereas a large multiuser system might have several thousand.
The PPID value is the parent process ID: the number of the process that created this one. Every process, except the first, has a parent, and each process may have zero or more child processes, so processes form a tree. Process number 0 is usually called something like kernel, sched, or swapper, and is not shown in ps output on some systems. Process number 1 is rather special; it is called init, and is described in the init(8) manual pages. A child process whose parent dies prematurely is assigned init as its new parent. When a system is shut down properly, processes