Job control (Unix)

From Wikipedia, the free encyclopedia

On operating systems that support executing multiple processes in parallel or in series (batch processing), job control refers to the orchestration of multiple batch jobs.

Contents

[edit] Unix shell

When using Unix or related operating systems via a terminal, a user will initially only have a single process running, their login shell. Most tasks (directory listing, editing files, etc.) can easily be accomplished by letting the program take control of the terminal and returning control to the shell when the program exits; however, sometimes the user will wish to carry out a task in the background while using the terminal for another purpose. Job control is a facility developed to make this possible, by allowing the user to start programs in the background, send programs into the background, bring background programs into the foreground, and start and stop running programs. Processes under the influence of a job control facility are referred to as jobs.

[edit] History

Job control was first implemented in the C shell, making use of features of the 4.1BSD kernel. The facility was then incorporated into the Bourne shell, and exists in most modern Unix shells.

[edit] Implementation

  • Typically, the shell keeps a list of jobs in a job table. A job consists of all the members of a pipeline; thus all the processes constituting the job will be in the same process group.
  • A program can be started as a background task by appending &[1] to the command line; its output is directed to the terminal (potentially interleaved with other programs' output) but it cannot read from the terminal input.
  • A task running in the foreground can be stopped by typing the suspend character (Ctrl+Z); this sends SIGTSTP to the process group and returns control to the shell.
  • A stopped job can be resumed as a background job with the bg builtin or as the foreground job with fg; in either case the shell redirects I/O appropriately and sends SIGCONT to the process.
  • jobs will list the background jobs existing in the job table, along with their job number and job state (stopped or running).
  • The kill builtin (not /bin/kill) can signal jobs by job ID as well as by process ID: jobs specified by a job ID should be killed by prefixing "%"[2]. Kill can send any signal to a job, however if the intent is to rid the system of the processes the signals SIGKILL and SIGTERM (the default), are probably the most applicable.
  • disown can be used to remove jobs from the job table, converting them from jobs into daemons so that they continue executing when the user logs out.

[edit] Job ID

A job ID is a token used to identify jobs to shell builtins. Job IDs begin with the % character; %n identifies job n, while %% identifies the current job. Other job IDs are specified by POSIX.[3]

[edit] References

  1. ^ This section uses Bash syntax; other shells offer similar functionality under other names.
  2. ^ Code applicable for bash, and bash-compatible shells
  3. ^ IEEE Std 1003.1-2001, Section 3.203, Job Control Job ID

[edit] See also

[edit] Further reading

[edit] External links