Job Control

Now that command backgrounding and multitasking have been discussed, some more definitions can be mentioned. A process is a command which has been submitted to the shell for execution. gsh contains a set of special commands which make dealing with processes much easier. gsh treats each command entered at the command-line as a job, where a single job may contain multiple processes. For example:

% ls           one command, one process, one job
% ls ; ps      two commands, two processes, two jobs
% ls & ps  two commands, two processes, two jobs
% ls | more    two processes, one job

When a job is run from the shell, it can be in several modes of operation. Jobs can be in any of three states: "running", "stopped", or "done". A job can be executing in either the foreground or the background.

Commands exist to place a job in any mode of operation. When a job is run directly from a command-line it is running and it is in the foreground. Since the command-line cannot be accessed, two special keys have been defined: ^C kills the job and ^Z will stop the job. When the job is killed, it is gone forever, but a stopped job can be restarted. When a job is stopped, the kernel suspends each of the processes in the job.

Jobs that are running in the background or have been stopped can be accessed using several built-in commands. The bg command will place a job in the background, placing it in the running state if necessary. The fg command will similarly place a job in the foreground, and the stop command will stop a backgrounded job. The kill command will terminate a job.

Each time job control is accessed, a special job status line is displayed following the command. The first item on the left in brackets is the job number. Next is a single character, either a '+', '-', or a blank. The '+' designates the currently accessed job, the '-' is the previously accessed job, and all other jobs are not specified. The jobs command will display a list of all jobs.

Have another look at the example in the Section called Background Execution of Commands; now more of the notation will be understandable.

Each of the special commands, bg, fg, stop, and kill, take an argument which specifies the job to perform the operation on. The argument is either a number specifying the process id, or a '%' followed by one of the following: '+' or '-' for the current job, a '-' for the previous job, or a number to specify any specific job. If nothing follows the '%' or the argument is missing, then the current job is the default.

There is one additional way that a job may be stopped. If the job is placed in the background and it attempts to read from the console, the job will be stopped, and the status line says "(tty input)" as the reason for the job being stopped. The job should be foregrounded so that the user may enter input to the program. It can then be placed back in the background as necessary (with ^Z and bg).