Chapter 9 process relationship

Source: Internet
Author: User

 

1. Terminal Login

Logon Process: Kernel ---> init [process ID = 1] ---> (fork) init ---> (exec) getty ---> (exec) ---> login

Logon End Process: Process 1 = init-> logon shell <-> terminal device driver <-> end user

 

 

2. network logon

For example

 

 

 

3. Process Group

1). Each process belongs to a process group, which obtains signals from the same terminal.

The getpgrp function returns the ID of the Process Group of the calling process. The result is also indicated by pid_t:

# Include <unistd. h> pid_t getpgrp (void); // return the ID of the process group that calls the process.

2) SUS defines the getpgid function as an XSI extension to follow suit 1) the behavior of the Function

# Include <unistd. h> pid_t getpgid (pid_t pid); // The Process Group ID is returned successfully, and-1 is returned with an error.

If the pid is 0, the ID of the process group that calls the process is returned. Therefore, getpgid (0) is equivalent to getpgrp ();

 

3) Each process group can have a process leader. The Process ID equivalent to the Process Group ID of the Leader is identified (ProcessID = Process Group ID of the Leader ). A process leader may create a process group, create a process in the group, and terminate the process. As long as there is at least one process in this group, this process group still exists, regardless of whether the process leader is terminated or not. Add a process to an existing process group or create a new process group by calling setpgid:

# Include <unistd. h> int setpgid (pid_t pid, pid_t pgid); // success returns 0, error returns-1

The setpgid function sets the pid Process Group ID to pgid. If the two parameters are the same, the process specified by the pid becomes the process leader. If the pid is 0, the caller's process ID is used. Similarly, if the pgid is 0, the process ID specified by the pid is used as the process group ID.

PS: Generally, a process is used to set the process group ID of its sub-processes, and the sub-process is used to set the process group ID in the sub-process.

 

4. Session

A session is a collection of one or more process groups. The following session contains three Process Groups.

The process calls the setsid function to create a new session.

# Include <unistd. h> pid_t setsid (void); // return the process group ID; error return-1

If the called process is not a process leader, this function creates a new session. Three things happen:

1). the process becomes the first process of the new session. (The first process of a session is the process that creates the session .) This process is the only process in the new session.

2). The process becomes the process leader of a new process group. The new process group ID is the process ID of the called process.

3) The process has no control terminal. If a process has a control terminal before calling setsid, the Association is interrupted.

PS: If the caller is already a process leader, this function returns an error. To ensure that this is not the case, the common practice is to call fork and terminate the parent process so that the child process can continue.

 

The getsid function returns the ID of the Process Group led by the session of a process.

# Include <unistd. h> pid_t getsid (pid_t pid); // return the process group ID of the session leader. The error "-1" is returned.

If the pid is 0, getsid returns the ID of the process group that calls the process. For security reasons, some implementations may have the following restrictions: If the pid does not belong to the caller's session, the calling process cannot obtain the session-led process group ID.

 

5. control terminal

Sessions and Process Groups have several other features ::

1) sessions can have a single control terminal. This usually occurs when you log on to the terminal device (when you log on to the terminal) or Pseudo Terminal Device (when you log on to the network ).

2) the first process of a session established to connect to 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 group, and all other Process Groups in this session are background process groups.

5). Whenever we enter the terminal interrupt key (usually DELETE or Control-C), the interrupt signal is sent to all processes in the foreground process group.

6). Whenever we enter the terminal's return key (via Control + \), the exit signal will be sent to all processes in the foreground process group.

7) if the terminal interface detects that the modem or network connection is disconnected, the suspended signal is sent to the control process (the first process of the session ).

 

 

6. tcgetpgrp, tcsetpgrp, and tcgetsid Functions

A method is required to tell the kernel which process group is the foreground process group, so that the terminal device driver card can understand the terminal input and terminal signal.

# Include <unistd. h> pid_t tcgetpgrp (int filedes); // Process Group ID of the foreground Process Group;-1int tcsetpgrp (int filedes, pid_t pgrpid) is returned if an error occurs. // 0 is returned, error returned-1

The tcgetpgrp function returns the ID of the foreground process group associated with the terminal opened on filedes. If a process has a control terminal, the process can call tcsetpgrp to set the foreground Process Group ID to pgrpid.

 

SUS defines an XSI extension, called tcgetsid, to allow an application to obtain the session-led process group ID and give a file descriptor that controls TTY.

# Include <termios. h> pid_t tcgetsid (int filedes); // return the process group ID of the session leader. Error returned-1

Applications that need to manage the control terminal can use tcgetsid to identify the session ID (equivalent to the Process Group ID of the first process) that controls the terminal session ).

 

7. Job Control

Job control requires support in three forms:

(1) shell that supports job control.

(2) The Terminal Driver in the kernel must support job control.

(3) Support for some operation control signals must be provided

 

8. shell execution Program

For more information, see APUE.

Shell command:

ps -o pid,ppid,pgid,sid,comm | cat1 | cat2

Output:

PID PPID PGID SID COMMAND
949 947 949 949 sh
1888 949 949 949 cat2
1889 1823 949 949 ps
1890 1988 949 949 cat1

The last process in the MPs queue is the shell sub-process, while the process that executes other commands in the MPs queue is the sub-process of the final process.

 

9. Orphan Process Group

A parent process terminated is called an orphan process, which is adopted by the init process.

 

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.