Departments & Programs


Processes and jobs; foreground and background

Last revision August 9, 2004

Table of Contents:

  1. Shell interpretation
  2. Editing command lines
  3. Stuck in a Unix login session?
  4. Program execution
  5. Simple commands
  6. Unix command syntax
  7. Controlling processes
  8. Your login environment

A process is "an instance of a program, with an associated environment, executing in the computer." New processes are always created by having an existing process fork or spawn a child process. The parent process, much like a real parent, can either keep close tabs on the child, waiting for him to finish or controlling his I/O, or disown the child to become a separate detached process.

When you login, the master init process spawns and detaches a child process that becomes the parent process for your login session. This process is an instance of the shell program.

The shell reads the input you type at the terminal to decide when and how to spawn new child processes to run the programs you desire. In the simplest case, your input command to the shell gives the name of a single program and its arguments. The shell spawns a single child process to execute that program (for example, ls) and then waits for that process to finish before processing more input from you. You can keep typing more input which will be saved in a type-ahead buffer, but the shell will not begin processing it until the child process has finished. This type of child process is said to be a foreground process.

You can also create background processes. When the shell starts a background process, that child process is allowed to run by itself while the shell immediately resumes processing more terminal input. The shell does not wait for the background child process to complete; it prompts for more input immediately. You tell the shell to start a process in the background by ending the command line with the ampersand metacharacter (&).

Background processes have several characteristics that differ from foreground processes:

  • A process running in the background can continue to send messages or output to the terminal (sometimes interrupting other work you are doing), but if it tries to read from the terminal, it is blocked (suspended). You get a message when this happens. To continue the command, you must bring it back into the foreground and provide the input it needs.
  • Generally, programs need input data or create output of some kind. By default, the terminal is used to prompt for input or display output. If you want to run a program in the background, particularly if you want it to keep running after you logout, you need to redirect its input and output to use files rather than the terminal.
  • When a background process is finished, the shell prints a done message just before your next prompt. That is, the message does not interrupt your current foreground process, but waits until it finishes.
  • A background process will continue to run even if the parent shell is logged off. In this case, it is said to be detached. You cannot re-connect to this process from a future login.
  • If you try to logout while there is a suspended (not actually running) background process, then the logout will fail with a message that there are stopped jobs. You need to either kill or restart these jobs in the background or foreground, using the techniques described below. Once you have disposed of all these suspended background jobs, then you can re-issue the logout command.
<--Previous Overview Next-->

Comments or Questions?