Linux/unix Process Relationship

Source: Internet
Author: User
Tags session id

Process Relationship Processes Group

In addition to a process, each process ID is also. Also belongs to a process group. Over base is a collection of one or more procedures.

Usually they are associated with the same job, which accepts 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 function GETPGRP returns the process group ID of the calling process.

The Getpid function also has this function.

The process can add an existing group or create a new process group by calling Setpgid.

Session

A session is a collection of one or more process groups.

Typically, a shell's pipeline will weave several processes into a group. For example, see the following:

Proc1 | PROC2 &

PROC3 | proc4 | Proc5

Its session schedule is as follows:

The process calls the SETSID function to establish a new session.

#include <unistd.h>

pid_t setsid (void);

You can also call GetSID to return the process group ID of the session's first process

#include <unistd.h>

pid_t getsid (pid_t pid);

Control Terminal

When you log in, you are actively creating a control terminal yourself.

When the program interacts with the control terminal, the way to ensure that the program can read and write the control terminal is to open the file/dev/tty. In the kernel, the word special file is synonymous with the control terminal. Assuming the program does not control the terminal, opening this device will fail.

1. A session can have a control terminal

2. Establishing a session-receiving process that is connected to the control terminal is called the control process

3. Several process groups in a session can be divided into one foreground process group and one or several background process groups

4. Suppose a session has a control terminal. Then it has a foreground process. Other process groups in the session are background process groups

5. Whenever you type the interrupt key for a terminal, the interrupt signal is sent to all processes in the foreground process group

6. Whenever you type the terminal's exit key, the exit signal is sent to all processes in the foreground process group

7. Assume that the modem detected by the terminal interface is disconnected from the network. Sends the hang-off signal 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 process group ID that invokes the foreground process, which is associated with the terminal opened on FD.

Assuming that the process has a control terminal, the process can call TCSETPGRP to set the foreground process group ID to PGRP. FD must refer to the control terminal of the session.

#include <termios.h>

Pid_ttcgetsid (int fd);

This function identifies the session ID of the session first process that controls the terminal.

Job Control

Job control agrees to start multiple jobs (process groups) on one terminal, he controls which job can access the terminal, and those jobs are also executed in the background.

[Email protected]:~$ cat > Temp.foo & Boot in background, but will read from standard input

[1] 3894

[Email protected]:~$

Type Enter

[1]+ Stopped cat > Temp.foo

[Email protected]:~$FG%1 makes job No. 1th a foreground job

Cat >temp.foo Shell tells us which homework is at the front desk now.

hello,world input 1 lines followed by file Terminator (Ctrl+d)

[Email protected]:~$cat Temp.foo Check that the line has been fed into the file

Hello,world

[Email protected]:~$

The shell starts the cat process in the background, but when the cat view reads its standard input, the terminal driver knows that he is a background job. The sigttin signal is then sent to the background job.

The shell detects a change in the state of its child process and notifies us that the job has been stopped.

And then. We use the shell's FG to send this stopped job to the foreground for execution.

Doing so enables the shell to place this job in the foreground process group. And will continue to send the signal to the process group.

Because the job is now in the foreground process group. So it can read the control terminal.

[Email protected]:~$ cat Temp.foo & running in the background

[1] 4125

[Email protected]:~ $hello, world

[1]+ Done Cat Temp.foo

[Email protected]:~$stty tostop

[Email protected]:~$cat Temp.foo &

[1] 4129

[Email protected]:~$

Type Enter to find the job has stopped

[1]+ Stopped Cat Temp.foo

[Email protected]:~$ FG%1 resuming stopped jobs in foreground

Cat Temp.foo Shell tells us which homework is at the front desk now.

Hello,world This is the output of the job change.

Summary: Add & after the shell command, indicating that the command will start in the background. The FG command is able to send the backend to the foreground.

Stty Tostop can disable the output of background jobs to the terminal.

Shell Run Program

How the shell runs the program, and how it relates to the concepts of process groups, control terminals, and sessions.

The shell places the foreground process process in its own process group. The shell is left in his own process group.
Ps-o Pid,ppid,pgrp,session,tpgid,comm

PID PPID pgrp sess tpgid COMMAND

4302 4293 4302 4302 4359 Bash

4359 4302 4359 4302 4359 PS

The shell puts the foreground job (PS) into its own process group (4359). The PS command is the leader process and is the only process in the process group.

Further, this process group has a control terminal. So it's the foreground process group. Our login shell is a background process when we run the PS command. But it's important to note that. Both process groups 4302 and 43509 are members of the same session.

Run this process in the background:

Ps-o Pid,ppid,pgrp,session,tpgid,comm &

PID PPID pgrp sess tpgid COMMAND

4302 4293 4302 4302 4302 Bash

4390 4302 4390 4302 4302 PS

Once again. The PS command is placed in its own process group, but at this point the process group (4302) is no longer a foreground process group, but a background process group.

The shell that is logged on is the foreground process group.

Run two processes in a pipeline as follows:

Ps-opid,ppid,pgrp,session,tpgid,comm | Cat

PID PPID pgrp sess tpgid COMMAND

4302 4293 4302 4302 4411 Bash

4411 4302 4411 4302 4411 PS

4412 4302 4411 4302 4411 Cat

Two processes PS and CAT1 are in a new process group (4411), which is a foreground process. Shell creation will run the process in the pipeline, which means that both PS and Cat's parent process are shell.

When you run this pipe line in the background. The results are similar.

Ps-opid,ppid,pgrp,session,tpgid,comm | Cat &

PID PPID pgrp sess tpgid COMMAND

4302 4293 4302 4302 4302 Bash

4440 4302 4440 4302 4302 PS

4441 4302 4440 4302 4302 Cat

You need to pay attention here. With a different shell, the sequence of processes it creates is not the same. The above discussion is based on the example of a shell with job control on Linux.

Assuming there is no shell on Linux, the last process in the pipeline is the shell's child process. The process that runs the other commands in the pipeline is the child process of the last process.

Orphan Process Group

A process whose parent process has been terminated is called an orphan process, and such a process is adopted by the INIT process. The entire process group can also be called an orphan.

Orphan process Group definition: A process group is not an orphan process group if the parent process of each member of the group is either a member of the group or a member of the session to which the reorganization belongs.

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Linux/unix Process Relationship

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.