In addition to a process ID, each process in the process Link Group also belongs to a process Group. A process Group is a collection of one or more processes. Usually they are associated with the same job, process-related process Group
In addition to a process ID, each process also belongs to a process Group. A process Group is a collection of one or more processes. Usually they are associated with the same job and can receive various signals from the same terminal.
# Include
Pid_tgetpgrp (void);/* POSIX.1version */
Pid_t getpgid (pid_tpid );
Intsetpgid (pid_t pid, pid_t pgid );
The getpgrp function returns the ID of the process group that calls the process.
The getpid function also provides this function.
A process can join an existing group or create a new process Group by calling setpgid.
Session
A session is a collection of one or more process groups.
Usually several processes are grouped into a group by the shell pipeline. As follows:
Proc1 | proc2 &
Proc3 | proc4 | proc5
The session schedule is as follows:
The process calls the setsid function to create a new session.
# Include
Pid_t setsid (void );
You can also call getsid to return the process Group ID of the first process in the session.
# Include
Pid_t getsid (pid_t pid );
Control Terminal
When you log on, a control terminal is automatically created.
When the program interacts with the control terminal, to ensure that the program can read and write the control terminal, open the file/dev/tty. In the kernel, the word special file is the definition of the control terminal. If the program does not have a control terminal, opening this device will fail.
1. a session can have a control terminal
2. the session receiving process that establishes a connection with the control terminal is called a control process.
3. Several process groups in a session can be divided into one foreground process Group and one or more background process groups.
4. if a session has a control terminal, it has a foreground process, and other process groups in the session are background process groups.
5. whenever you type the terminal interrupt key, the interrupt signal is sent to all processes in the foreground process Group.
6. whenever you type the terminal's return key, the exit signal is sent to all processes in the foreground process Group.
7. if the modem detected by the terminal interface is disconnected from the network, the end signal is sent to the control process.
Tcgetpgrp, tcsetpgrp, and tcgetsid functions
# Include
Pid_t tcgetpgrp (int fd );
Int tcsetpgrp (int fd, pid_t pgrp );
The tcgetpgrp function returns the ID of the process group that calls the foreground process. The foreground process group is associated with the terminal opened on fd.
If a process has a control terminal, the process can call tcsetpgrp to set the foreground process Group ID to pgrp. fd must reference the control terminal of the session.
# Include
Pid_ttcgetsid (int fd );
This function identifies the session ID of the first session process of the control terminal.
Job Control
Job Control allows you to start multiple jobs (process groups) on a terminal. it controls which jobs can access the terminal and which jobs are also running in the background.
Catchen123 @ ubuntu :~ $Cat> temp. foo &Started in the background, but will read from standard input
[1] 3894
Chen123 @ ubuntu :~ $
Enter
[1] + Stopped cat> temp. foo
Chen123 @ ubuntu :~ $Fg % 1Make Job 1 a foreground job
Cat> temp. foo shell tells us which job is in the foreground
Hello, worldEnter one line followed by the file terminator (ctrl + D)
Chen123 @ ubuntu :~ $Cat temp. fooCheck that this row has been sent to a file
Hello, world
Chen123 @ ubuntu :~ $
Shell starts the cat process in the background. but when the cat view reads its standard input, the terminal driver knows that it is a background job and sends the SIGTTIN signal to the background job. Shell detects the status change of its sub-processes and notifies us that the job has been stopped. Then, we use shell fg to send the stopped job to the foreground for running. In this way, shell can place the job in the foreground process group and send the signal to the process Group. Because the job is now in the foreground process group, it can read control terminals.
Chen123 @ ubuntu:~ $ Cat temp. foo &Run in the background
[1] 4125
Chen123 @ ubuntu :~ $ Hello, world
[1] + Done cat temp. foo
Chen123 @ ubuntu :~ $Stty tostop
Chen123 @ ubuntu :~ $Cat temp. foo &
[1] 4129
Chen123 @ ubuntu :~ $
Press enter to find that the job has been stopped
[1] + Stopped cat temp. foo
Chen123 @ ubuntu :~ $Fg % 1Resume a stopped job on the foreground
Cat temp. foo shell tells us which job is in the foreground
Hello, world. this is the output of the change job.
Conclusion: Add & after the shell command, indicating that the command will be started in the background. The fg command can send the background to the foreground. Stty tostop can disable the output of background jobs to terminals.
Shell execution program
How shell executes programs, and its relationship with process groups, control terminals, sessions, and other concepts.
Shell places the foreground process in its own process Group, and shell stays in its own process Group.
Ps-o pid, ppid, pgrp, session, tpgid, comm
PID PPID PGRP SESS TPGID COMMAND
4302 4293 4302 4302 bash
4359 4302 4359 4302 4359 ps
Shell puts the foreground job (ps) into its own process Group (4359. The ps command is the leader process and the only process in the process Group. Further, this process Group has a control terminal, so it is a front-end process Group. The logon shell is a background process when executing the ps command. Note that both process groups 4302 and 43509 are members of the same session.
Execute this process in the background:
Ps-o pid, ppid, pgrp, session, tpgid, comm &
PID PPID PGRP SESS TPGID COMMAND
4302 4293 4302 4302 bash
4390 4302 4390 4302 4302 ps
Again, the ps command is placed in its own process group, but the process Group (4302) is no longer a foreground process group, but a background process Group. The shell to log on to is the foreground process Group.
Execute two processes in one pipeline as follows:
Ps-opid, ppid, pgrp, session, tpgid, comm | cat
PID PPID PGRP SESS TPGID COMMAND
4302 4293 4302 4302 bash
4411 4302 4411 4302 4411 ps
4412 4302 4411 4302 4411 cat
The two processes ps and cat1 are both in a new process Group (4411), which is a foreground process. Shell creation will execute the processes in the MPs queue, that is, the parent processes of ps and cat are both shell.
When this MPs queue line is executed in the background, the result is similar.
Ps-opid, ppid, pgrp, session, tpgid, comm | cat &
PID PPID PGRP SESS TPGID COMMAND
4302 4293 4302 4302 bash
4440 4302 4440 4302 4302 ps
4441 4302 4440 4302 4302 cat
Note that different shells are used to create different processes in different order. The above discussion is based on the shell with job control on Linux.
If there is no shell for linux, the last process in the pipeline is a sub-process of shell, and the process that executes other commands in the pipeline is the sub-process of the last process.
Orphan process Group
A parent process terminated is called an orphan process, which is adopted by the init process. The whole process group can also be called orphan.
Orphan process group definition: If a process Group is not an orphan process Group, the parent process of each member in the group is either a member of the group, or it is not a member of the reorganized session.