In
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
and
Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
operating system
An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ...
s, job control refers to control of jobs by a
shell
Shell may refer to:
Architecture and design
* Shell (structure), a thin structure
** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses
Science Biology
* Seashell, a hard outer layer of a marine ani ...
, especially interactively, where a "job" is a shell's representation for a
process group
In a POSIX-conformant operating system, a process group denotes a collection of one or more processes.
Among other things, a process group is used to control the distribution of a signal;
when a signal is directed to a process group, the signal ...
. Basic job control features are the suspending, resuming, or terminating of all processes in the job/process group; more advanced features can be performed by sending
signals
A signal is both the process and the result of Signal transmission, transmission of data over some transmission media, media accomplished by embedding some variation. Signals are important in multiple subject fields including signal processin ...
to the job. Job control is of particular interest in Unix due to its
multiprocessing
Multiprocessing (MP) is the use of two or more central processing units (CPUs) within a single computer system. The term also refers to the ability of a system to support more than one processor or the ability to allocate tasks between them. The ...
, and should be distinguished from
job control generally, which is frequently applied to sequential execution (
batch processing
Computerized batch processing is a method of running software programs called jobs in batches automatically. While users are required to submit the jobs, no other interaction by the user is required to process the batch. Batches may automatically ...
).
Overview
When using
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
or
Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
operating systems via a
terminal (or
terminal emulator
A terminal emulator, or terminal application, is a computer program that emulates a video terminal within some other display architecture. Though typically synonymous with a shell or text terminal, the term ''terminal'' covers all remote term ...
), a user will initially only have a single process running, their interactive
shell
Shell may refer to:
Architecture and design
* Shell (structure), a thin structure
** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses
Science Biology
* Seashell, a hard outer layer of a marine ani ...
(it may or may not be a
login
In computer security, logging in (or logging on, signing in, or signing on) is the process by which an individual gains access to a computer system or program by identifying and authenticating themselves.
Typically, user credential ...
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 – formally, by attaching to
standard input and standard output to the shell, which reads or writes from the terminal, and catching signals sent from the keyboard, like the termination signal resulting from pressing .
However, sometimes the user will wish to carry out a task while using the terminal for another purpose. A task that is running but is not receiving input from the terminal is said to be running "in the background", while the single task that is receiving input from the terminal is "in the foreground". Job control is a facility developed to make this possible, by allowing the user to start processes in the background, send already running processes into the background, bring
background process
A background process is a computer process that runs ''behind the scenes'' (i.e., in the background) and without user intervention. Typical tasks for these processes include logging, system monitoring, scheduling, and user notification.
On a Wind ...
es into the foreground, and suspend or terminate processes.
The concept of a ''job'' maps the (shell) concept of a single shell command to the (operating system) concept of the possibly many processes that the command entails. Multi-process tasks come about because processes may create additional child processes, and a single shell command may consist of a
pipeline
A pipeline is a system of Pipe (fluid conveyance), pipes for long-distance transportation of a liquid or gas, typically to a market area for consumption. The latest data from 2014 gives a total of slightly less than of pipeline in 120 countries ...
of multiple communicating processes. For example, a command to select lines containing the text "title", sort these alphabetically, and display the result in a
pager
A pager, also known as a beeper or bleeper, is a Wireless communication, wireless telecommunications device that receives and displays Alphanumericals, alphanumeric or voice messages. One-way pagers can only receive messages, while response p ...
.
grep title somefile.txt , sort , less
This creates at least three processes: one for , one for , and one for
. Job control allows the shell to control these related processes as one entity, and when a user issues the appropriate key combination (usually ), the entire group of processes gets suspended.
Jobs are managed by the operating system as a single
process group
In a POSIX-conformant operating system, a process group denotes a collection of one or more processes.
Among other things, a process group is used to control the distribution of a signal;
when a signal is directed to a process group, the signal ...
, and the job is the shell's internal representation of such a group. This is defined in
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
as:
A job can be referred to by a
handle
A handle is a part of, or an attachment to, an object that allows it to be grasped and object manipulation, manipulated by hand. The design of each type of handle involves substantial ergonomics, ergonomic issues, even where these are dealt wi ...
called the ''job control job ID'' or simply ', which is used by
shell builtin
In computing, a shell builtin is a Command (computing), command or a Subroutine, function, exposed by a Shell (computing), shell, that is implemented in the shell itself, instead of an external computer program, program which the shell would load a ...
s to refer to the job. Job IDs begin with the
%
character;
%n
identifies job ''n'', while
%%
identifies the current job. Other job IDs are specified by
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
. In informal usage the number may be referred to as the "job number" or "job ID", and Bash documentation refers to the (%-prefixed) job ID as the ''jobspec.''
Job control and job IDs are typically only used in interactive use, where they simplify referring to process groups; in scripting PGIDs are used instead, as they are more precise and robust, and indeed job control is disabled by default in bash scripts.
History
Job control was first implemented in the
C shell
The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the ...
by Jim Kulp, then at
IIASA
The International Institute for Applied Systems Analysis (IIASA) is an independent International research institute located in Laxenburg, near Vienna in Austria, founded as an East-West scientific cooperation initiative during the Cold War. Th ...
in Austria, making use of features of the 4.1
BSD
The Berkeley Software Distribution (BSD), also known as Berkeley Unix or BSD Unix, is a discontinued Unix operating system developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berkeley, beginni ...
kernel.
The
KornShell
KornShell (ksh) is a Unix shell which was developed by David Korn (computer scientist), David Korn at Bell Labs in the early 1980s and announced at USENIX Annual Technical Conference, USENIX on July 14, 1983. The initial development was base ...
, developed at
Bell Labs
Nokia Bell Labs, commonly referred to as ''Bell Labs'', is an American industrial research and development company owned by Finnish technology company Nokia. With headquarters located in Murray Hill, New Jersey, Murray Hill, New Jersey, the compa ...
, adopted it and it was later incorporated into the SVR4 version of the
Bourne shell
The Bourne shell (sh) is a shell command-line interpreter for computer operating systems. It first appeared on Version 7 Unix, as its default shell. Unix-like systems continue to have /bin/sh—which will be the Bourne shell, or a symbolic lin ...
, and exists in most modern Unix shells.
Commands
The POSIX standard specifies two commands for resuming suspended jobs in the background and foreground, respectively and . These were modeled after the Korn shell job control commands.
The command pauses until the background tasks in the list have completed. All tasks must be running in the current shell. If the jobid list is empty then the pause continues until all outstanding background tasks are completed.
[ ]
Implementation
Typically, the shell keeps a list of jobs in a job table. Recall that a job corresponds to a process group, which consists of all the members of a
pipeline
A pipeline is a system of Pipe (fluid conveyance), pipes for long-distance transportation of a liquid or gas, typically to a market area for consumption. The latest data from 2014 gives a total of slightly less than of pipeline in 120 countries ...
and their descendants. The
jobs
command will list the background jobs existing in the job table, along with their job number and job state (stopped or running). When a
session ends when the user
logs out (exits the shell, which terminates the ''session leader'' process), the shell process sends
SIGHUP to all jobs, and waits for the process groups to end before terminating itself.
The
disown
command can be used to remove jobs from the job table, so that when the session ends the
child process
A child process (CP) in computing is a process created by another process (the parent process). This technique pertains to multitasking operating systems, and is sometimes called a subprocess or traditionally a subtask.
There are two major proce ...
groups are not sent SIGHUP, nor does the shell wait for them to terminate. They thus become
orphan processes, and may be terminated by the operating system, though more often this is used so the processes are adopted by
init
In Unix-based computer operating systems, init (short for ''initialization'') is the first process started during booting of the operating system. Init is a daemon process that continues running until the system is shut down. It is the direc ...
(the kernel sets their
parent process
In computing, a parent process is a process that has created one or more child processes.
Unix-like systems
In Unix-like operating systems, every process except (the swapper) is created when another process executes the fork() system call. T ...
to init) and continue executing as
daemon
A demon is a malevolent supernatural being, evil spirit or fiend in religion, occultism, literature, fiction, mythology and folklore.
Demon, daemon or dæmon may also refer to:
Entertainment Fictional entities
* Daemon (G.I. Joe), a character ...
s. Alternatives to prevent jobs from being terminated include
nohup and using a
terminal multiplexer.
A job running in the foreground can be stopped by typing the suspend character (
Ctrl-Z
In computer data, a substitute character (␚) is a control character that is used to pad transmitted data in order to send it in blocks of fixed size, or to stand in place of a character that is recognized to be invalid, erroneous or unrepres ...
). This sends the "terminal stop"
signal
A signal is both the process and the result of transmission of data over some media accomplished by embedding some variation. Signals are important in multiple subject fields including signal processing, information theory and biology.
In ...
(SIGTSTP) to the process group. By default, SIGTSTP causes processes receiving it to stop, and control is returned to the shell. However, a process can register a signal handler for or ignore SIGTSTP. A process can also be paused with the "stop" signal (SIGSTOP), which cannot be caught or ignored.
A job running in the foreground can be interrupted by typing the interruption character (
Ctrl-C
Control-C is a common computer command. It is generated by holding down the key and typing the key.
In graphical user interface environments, control+C is often used to copy highlighted text to the clipboard. Macintosh computers use for t ...
). This sends the "interrupt" signal (
SIGINT
Signals intelligence (SIGINT) is the act and field of intelligence-gathering by interception of ''signals'', whether communications between people (communications intelligence—abbreviated to COMINT) or from electronic signals not directly u ...
), which defaults to terminating the process, though it can be overridden.
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 the SIGCONT signal to the process, which causes the operating system to resume its execution. In
Bash, a program can be started as a background job by appending an ampersand (
&
) 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 background process that attempts to read from or write to its
controlling terminal is sent a SIGTTIN (for input) or SIGTTOU (for output) signal. These signals stop the process by default, but they may also be handled in other ways. Shells often override the default stop action of SIGTTOU so that background processes deliver their output to the controlling terminal by default.
In Bash-compatible shells, the
kill
builtin (not
/bin/kill
) can signal jobs by job ID as well as by process group ID – sending a signal to a job sends it to the whole process group, and jobs specified by a job ID should be killed by prefixing
%
.
kill
can send any signal to a job; however, if the intent is to rid the system of the processes the signals
SIGKILL
Signals are standardized messages sent to a running program to trigger specific behavior, such as quitting or error handling. They are a limited form of inter-process communication (IPC), typically used in Unix, Unix-like, and other POSIX-comp ...
and
SIGTERM
Signals are standardized messages sent to a running program to trigger specific behavior, such as quitting or error handling. They are a limited form of inter-process communication (IPC), typically used in Unix, Unix-like, and other POSIX-comp ...
(the default) are probably the most applicable.
See also
*
C shell
The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the ...
Notes
References
Further reading
* {{cite book,
url=http://www.informit.com/title/0201702452,
title=The Design and Implementation of the FreeBSD Operating System,
author=
Marshall Kirk McKusick
Marshall Kirk McKusick (born January 19, 1954) is an American computer scientist, known for his extensive work on BSD UNIX, from the 1980s to FreeBSD in the present day. He served on the board of the USENIX Association from 1986 to 1992 and aga ...
and George V. Neville-Neil,
date=2004-08-02,
publisher=Addison Wesley,
chapter-url=http://www.informit.com/articles/article.aspx?p=366888&seqNum=8,
chapter=FreeBSD Process Management: Process Groups and Sessions,
isbn=0-201-70245-2
External links
* "Job Control"
''Bash Reference Manual''
Unix