Processes
A process is the execution of a command by Mac OS X. The shell that starts when you log in is a command, or a process, like any other. When you give the name of a Mac OS X utility on the command line, you initiate a process. When you run a shell script, another shell process is started and additional processes are created for each command in the script. Depending on how you invoke the shell script, the script is run either by the current shell or, more typically, by a subshell (child) of the current shell. A process is not started when you run a shell builtin, such as cd.
Process Structure
fork system call
Like the file structure, the process structure is hierarchical, with parents, children, and even a root. A parent process forks a child process, which in turn can fork other processes. (The term fork indicates that, as with a fork in the road, one process turns into two. Initially the two forks are identical except that one is identified as the parent and one as the child. You can also use the term spawn; the words are interchangeable.) The operating system routine, or system call, that creates a new process is named fork.
When Mac OS X begins execution when a system is started, it starts launchd, a single process called a spontaneous process, with PID number 1. This process holds the same position in the process structure as the root directory does in the file structure: It is the ancestor of all processes that the system and users work with. When the system is in multiuser mode, launchd runs additional processes that display the login window and may also display login: prompts on terminals or accept network logins. When a user responds to one of these prompts, the process that authenticates the user then starts a user environment such as the Finder for a graphical login, or the user's shell process, for a textual login.
Process Identification
PID number
Mac OS X assigns a unique PID (process identification) number at the inception of each process. As long as a process exists, it keeps the same PID number. During one session the same process is always executing the login shell. When you fork a new process—for example, when you use an editor—the PID number of the new (child) process is different from that of its parent process. When you return to the login shell, it is still being executed by the same process and has the same PID number as when you logged in.
The following example shows that the process running the shell forked (is the parent of) the process running ps (page 133). When you call it with the –1 option, ps displays a full listing of information about each process. The line of the ps display with bash in the COMMAND column refers to the process running the shell. The column headed by PID identifies the PID number. The column headed PPID identifies the PID number of the parent of the process. From the PID and PPID columns you can see that the process running the shell (PID 6476) is the parent of the process running sleep (PID 11040). The parent PID number of sleep is the same as the PID number of the shell (6476).
$ sleep 10 & [1] 11040 $ ps -l UID PID PPID CPU PRI NI VSZ RSS WCHAN STAT TT TIME COMMAND 502 6476 6472 0 31 0 27800 864 - Ss p5 0:00.13 -bash 502 11040 6476 0 31 0 27232 672 - S p5 0:00.01 sleep 10
Refer to page 819 for more information on ps and the columns it displays with the –1 option. A second pair of sleep and ps–1 commands shows that the shell is still being run by the same process but that it forked another process to run sleep:
$ sleep 10 & [1] 11046 $ ps -l UID PID PPID CPU PRI NI VSZ RSS WCHAN STAT TT TIME COMMAND 502 6476 6472 0 31 0 27800 864 - Ss p5 0:00.14 -bash 502 11046 6476 0 31 0 27232 364 - S p5 0:00.00 sleep 10
Executing a Command
fork and sleep
When you give the shell a command, it usually forks (spawns) a child process to execute the command. While the child process is executing the command, the parent process sleeps. While a process is sleeping, it does not use any computer time but remains inactive, waiting to wake up. When the child process finishes executing the command, it tells its parent of its success or failure via its exit status and then dies. The parent process (which is running the shell) wakes up and prompts for another command.
Background process
When you run a process in the background by ending a command with an ampersand (&), the shell forks a child process without going to sleep and without waiting for the child process to run to completion. The parent process, which is executing the shell, reports the job number and PID number of the child and prompts for another command. The child process runs in the background, independent of its parent.
Builtins
Although the shell forks a process to run most of the commands you give it, some commands are built into the shell. The shell does not need to fork a process to run builtins. For more information refer to "Builtins" on page 138.
Variables
Within a given process, such as your login shell or a subshell, you can declare, initialize, read, and change variables. By default however, a variable is local to a process. When a process forks a child process, the parent does not pass the value of a variable to the child. You can make the value of a variable available to child processes (global) by using the export builtin under bash (page 560) or the setenv builtin under tcsh (page 353).