Job control (Unix)
[toc]
In Unix and Unix-like operating systems, job control refers to control of jobs by a shell, especially interactively, where a "job" is a shell's representation for a process group.
lmz:
- 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 is delivered to each process that is a member of the group.
- job指的是进程组(一个命令通常就是一个进程组,因为每一个命令往往包含好几个进程)。job control指的是对进程组发送信号,以控制进程组的状态。常见的状态包括暂停、继续、终止。
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 to the job. Job control is of particular interest in Unix due to its multiprocessing, and should be distinguished from job control generally, which is frequently applied to sequential execution (batch processing).
Overview
When using Unix or Unix-like operating systems via a terminal (or terminal emulator), a user will initially only have a single process running, their login shell. Most tasks[a] (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 Control+C.
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 processes 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 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, viz.,
grep title somefile.txt | sort | less
creates at least three processes: one for grep, one for sort, and one for less. Job control allows the shell to control these related processes as one entity, and when a user issues the appropriate key combination (usually Control+Z), the entire group of processes gets suspended. Jobs are managed by the operating system as a single process group, and the job is the shell's internal representation of such a group. This is defined in POSIX as:[1]
A set of processes, comprising a shell pipeline, and any processes descended from it, that are all in the same process group.
A job can be referred to by a handle[b] called the job control job ID or simply job ID, which is used by shell builtins 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.[2] 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.[3]
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.
lmz: 在shell中执行命令,命令的进程会控制shell,命令结束后就会把控制权还给shell。但是如果想在终端中执行多个命令怎么办?我们可以对命令也就是job发送信号,从而在前台后台分别启动进程。
Commands
The POSIX standard specifies two commands for resuming suspended jobs in the background and foreground, respectively bg and fg.
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 and their descendents. The
jobscommand will list the background jobs existing in the job table, along with their job number and job state (stopped or running). - A job running in the foreground can be stopped by typing the suspend character (Ctrl-Z). This sends the "terminal stop" signal (SIGTSTP) to the process group. By default, SIGTSTP causes processes receiving it to stop, and control is returned to the shell.
- A job running in the foreground can be interrupted by typing the interruption character (Ctrl-C). This sends the "interrupt" signal (SIGINT), which defaults to terminating the process, though it can be overridden.
- A stopped job can be resumed as a background job with the
bgbuiltin, or as the foreground job withfg. - In Bash-compatible shells, the
killbuiltin (not /bin/kill) can signal jobs by job ID as well as by process group ID.
tags:wekipedia