Linux Operation control
The concept of job control
Interactive shell The basic purpose of is to read commands from the user terminal , And create processes that execute the programs specified by these commands .
A single command may run only one process , But a command usually uses multiple processes . If in shell Use in command “ |” Operator , You will explicitly request several processes in your own process . however , Even if only one program is running , It can also use multiple processes internally . for example , Single compile command （ for example ：“cc -c foo.c”） There are usually four processes . If you run make Its job is to run other programs in a separate process .
Processes that belong to a single command are called process groups or jobs (job), thus , You can operate on all processes at once . for example , Input
Control-C Will send
SIGINT Terminate all processes in the foreground process group .
session) It's a bigger set of processes . Usually , All processes generated by a single login belong to the same session .
Each process belongs to a process group . When a process creates , It becomes the same process group and session as its parent process (session) Members of . Can pass
setsid Function to put it in another process group , If the process group belongs to the same session .
Usually , The new session is created by the system login program , conversation leader(session leader) Is to run the user login shell The process of .
Support job control shell Jobs that can use the terminal at any time must be arranged . Otherwise, there may be multiple jobs trying to read from the terminal at one time , And for which process should receive user input . To avoid that ,shell A protocol must be used with the terminal driver .
shell Only one process can be given unlimited access to the control terminal at a time . This is called the foreground job of the control terminal (foreground job). Executed without access to the terminal , from shell Other process groups managed by the program are called background jobs （background jobs）.
If a background job needs to read from its control terminal , The terminal driver stops ; If set
TOSTOP Pattern , Also used to write . Users can type
SUSP（control-z） Character to stop the foreground job , The program can send
SIGSTOP Signal to stop any operation .shell My duty is to inform when the operation will stop , Inform the user , It also provides a mechanism that allows users to continue to stop jobs interactively and switch jobs before the foreground and background .
Process control terminal
One of the properties of a process is its control terminal . use
fork The created child process controls the terminal from its parent process . such , All processes in the session will be removed from the session leader（session leader） Inherit control terminal . A session with terminal control leader(session leader) Called the control process of the terminal .
When a single process calls
setsid When you become the leader of a new conversation , It will be disconnected from its control terminal .
Access control terminal
The process in the foreground job of the control terminal can have unlimited access to the terminal . Background processes don't .
When a process in a background job attempts to read from its control terminal , Usually... Is sent to the process group SIGTTIN The signal . He usually causes all processes in the group to stop （ Unless they process the signal and don't stop on their own ）. however , If the read process ignores or blocks this signal , The read will fail , And display EIO error .
Again , When a process in a background job attempts to write to its control terminal , The default behavior is to send SIGTTOU The signal . however , The behavior can be marked by the local mode of TOSTOP Bit to modify （ See local mode ）. If this bit is not set （ default setting ）, Then it is always allowed to write to the control terminal without sending a signal . If SIGTTOU The signal is ignored or blocked by the write process , Write is also allowed .
Orphan progress group (Orphaned Process Groups)
When the control process terminates , Its terminal will become idle , And you can set up a new session on it .
To prevent problems , The session will be leader Process groups that continue to run after termination are marked as orphan process groups .
When a process is organized into an isolated group , Will be sent to the child processes under it SIGHUP The signal . Usually , This leads to the termination of the process . however , If the program ignores this signal or creates a handler for it , Even if its control process is terminated , It can also continue to run as if it were in an isolated process group . But it still can't access the terminal .
Job control command
Through the job control, you can put the job on the front desk , backstage , Stop or start the job
|jobs||List all the commands|
|bg %n||Put the current task or the specified task in the background ,n Is a task ID|
|fg %||Put the current or specified job to the foreground ,n It's homework ID|
|Control-Z||Stop the foreground job and put it as a stopped job in the background|