Linux/UNIX process relationship

Source: Internet
Author: User

Process link 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 <unistd. h>

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 <unistd. h>

Pid_t setsid (void );

You can also call getsid to return the process group ID of the first process in the session.

# Include <unistd. h>

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 <unistd. h>

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 <termios. h>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.