Job control

from Wikipedia, the free encyclopedia

The term Job control refers in Unix and Unix-like operating systems to the control of jobs of the shell , especially for jobs as a representation of a process group of Shell . The job control terminates the processes by means of signals , continues them or transfers them to standby .

overview

A user who runs Unix or a Unix-like system via a terminal (or a terminal emulation ) only creates a single process at the beginning: the shell itself. Most tasks (listing directories, editing files, etc.) can be carried out quite easily by letting the program control the terminal and the shell regaining control after the program has ended. Formally, this is done by integrating standard data streams into the shell, which write to or from the terminal and intercept inputs from the keyboard (e.g. the abort signal of the key combination Strg+ C).

Sometimes, however, a user would like to continue using the terminal while a process is running. One speaks of a background process if the process does not receive any input from the terminal, in the opposite case it is a process in the foreground. Job control enables the user to start processes in the background, bring processes that are already running from the background to the foreground, switch processes to standby mode or end processes.

A job combines the typical shell behavior of a single shell command with the ability of the operating system to process several processes that a command can contain. Multi-process tasks occur because processes can create child processes themselves, and a single shell command can contain a pipeline that connects multiple processes. The following command serves as an example, which selects lines with text content "title", sorts them alphabetically and then displays them with the pager less :

grep title somefile.txt | sort | less

The above command creates at least three processes: one for grep , one for sort, and one for less . The job control enables the shell to view these processes as a single unit and should a user press the appropriate key combination (usually Strg+ Z), the entire process group is put into standby mode .

Jobs are managed by the operating system as a single process group. A job is the internal shell representation of such a group. The definition can be found in POSIX :

A set of processes comprising a shell pipeline, and any processes descended from it, that are all in the same process group.

A set of processes that contain a shell pipeline and any descendant processes of the same process group.

A job ID is an abstract reference of the shell to a resource (process group) managed externally by the operating system and therefore corresponds to the definition of a handle . The shell built-in uses this job ID to refer to the corresponding job. Job IDs are marked with an %initial character, denoting %nthe nth job. The term %%refers to the current job. Further job IDs are described in POSIX . A more informal designation of job ID is job number and the Bash documentation also contains the expression “jobspec”.

Job IDs are usually only used in interactive applications, where they only refer to process groups. For scripts , PGIDs are used instead, which are more precise and more robust. In fact, job control is switched off by default in Bash scripts.

History

Job control was first used in the C shell. The IIASA then used the features of the 4.1 BSD kernel to introduce job control there as well. It was later integrated in the KornShell developed by Bell Labs and in the SVR4 version of the BourneShell . The job control is now included in most modern Unix shells.

Commands

The POSIX standard names two commands to continue jobs in standby mode in the foreground or background (see fg and bg in background process ). These commands were modeled on the job control commands of the Korn shell.

execution

Usually the shell keeps a Job Table list of all active jobs. It should be noted that a job relates to a process group, which consists of all elements of a pipeline and their derived processes. With the jobscommand, all background processes of this Job Table list, including their job numbers and their current status (stopped or active) can be displayed. If the user logs out (user exits the shell), the session leader process is ended. The shell process sends the SIGHUP abort signal to all jobs and terminates itself as the last process.

The disowncommand can be used to remove jobs from the job table. As a result, the child process groups do not receive a SIGHUP signal when a login session is terminated and the shell does not wait for them to be terminated. The processes become orphan processes and can be terminated by the operating system. In most cases, however, they are adopted by the init process (this is set as their parent process by the kernel) and continue to work as daemon processes. Another possibility to prevent the termination of the job is the nohup command in connection with a terminal multiplexer.

A job in the foreground can be stopped by the standby symbol ( Ctrl-Z ), which sends the terminal stop SIGTSTP to the process group. By default, the SIGTSTP command forces a process to stop and control is returned to the shell. However, a process can allow a signal handler to ignore SIGTSTP . A process can also be paused with the stop signal SIGSTOP , which cannot be intercepted or ignored.

Foreground jobs can be interrupted using the Ctrl-C cancel character. The SIGINT signal is sent, which terminates the process by default, but can be overwritten.

A stopped job can be continued as a background process with the command bgor in the foreground with the command fg. In both cases the shell redirects the input and output and sends the SIGCONT signal to the process, which leads the operating system to continue the process. In bash , a program can be started as a background job by appending a &to the command line. Its output is nested with other process outputs and passed on to the terminal, but cannot read any input from the terminal.

The terminal sends the SIGTTIN signal and the SIGTTOU signal for input and output instructions from background processes . Even if these signals are preset, they can still be modified. Some shells overwrite the default setting of the stop command SIGTTOU , which means that outputs from the background processes are automatically displayed in the terminal .

In Bash-compatible shells , the killcommand ( /bin/killis only a single use case) can send signals to jobs or process groups with a specific ID. The signal is sent to the entire process group, which is why a job with a specific ID %should be terminated with the prefix . killcan send any signal to a job; however, the signals SIGKILL and SIGTERM should be used to remove processes from the system .

Individual evidence

  1. ^ Open Group : Job Control Job. Retrieved September 19, 2019 (3,203 job control).
  2. Open Group : Job Control Job ID. Retrieved September 19, 2019 (3,204 Job control ID).
  3. gnu.org: Job Control Basics. Retrieved September 19, 2019 .
  4. ^ Foreword by Bill Joy . In: Gail Anderson and Paul Anderson: The UNIX C Shell Field Guide. Prentice Hall 1986, ISBN 0-13-937468-X , p. XVII.
  5. Cilinder: BSD Unix job control. Retrieved September 19, 2019 .
  6. bg  -  Open Group Base Specification
  7. fg  -  Open Group Base Specification

further reading

Web links