Terminal process relationships

Source: Internet
Author: User
Tags session id terminates

1. Terminal Login

Login process: Kernel--->init[process id=1]---> (fork) init---> (EXEC) Getty---> (EXEC)--->login

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

2. Network Login

Such as

3. Process Group

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

The function GETPGRP returns the process group ID of the calling process, and the result is represented by pid_t:

#include <unistd.h>pid_t getpgrp (void);//returns the process group ID of the calling process.

2) SUS defines the Getpgid function as a xsi extension to emulate the behavior of the function in 1)

#include <unistd.h>pid_t getpgid (pid_t pid);//Successful return of process group ID, error return-1

If the PID is 0, the process group ID of the calling process is returned. Thus Getpgid (0) is equivalent to GETPGRP ();

3). Each process group can have a process leader. This leader and its process group ID are equal to the process ID to identify (leader's ProcessID = Process group ID). It may be that a process leader creates a process group, creates a process for the group, and then terminates. As long as there is at least one process in this group, the process group still exists, regardless of whether the process leader terminates. A process joins an existing process group or creates a new process group by calling Setpgid:

#include <unistd.h>int setpgid (pid_t pid, pid_t pgid);//Successful return 0, error return-1

The Setpgid function sets the process group ID of the PID process to Pgid. If two parameters are equal, the process specified by the PID becomes the process leader. If the PID is 0, then the caller's process ID is used. Similarly, if Pgid is 0, the process ID specified by the PID is used as the process group ID.

PS: Typically enables a process to set the process group ID of its child process, and causes the child process to set the process group ID

4. Session

A session is a collection of one or more process groups, with three process groups in the following session

The process calls the SETSID function to establish a new session

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

If the calling process is not a process leader, then this function creates a new session. There are three things that happen:

1). The process becomes the session first process of a new conversation. (The session first process is the process that created the session.) The process is the only process in this new session.

2). This process becomes the process leader for a new process group. The new Process group ID is the process ID of the calling process.

3). The process does not control the terminal. If the process has a control terminal before calling Setsid, the association is interrupted.

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

The GETSID function returns the session leader's process group ID for a process

#include <unistd.h>pid_t getsid (pid_t pid);//return session leader's process group ID, error return-1

If the PID is 0,GETSID returns the process group ID of the session leader that called the process. For security reasons, some implementations may have the following limitations: If the PID is not part of the session where the caller resides, the calling process cannot get the process group ID of the session leader.

5. Control Terminal

There are several other features of the session and process group:

1). The session can have a single control terminal. This is usually a terminal device (in the case of a terminal login) or a pseudo-terminal device (in the case of a network logon) that is logged on to it.

2). Establish a session with the control terminal connection the first process is called the control process.

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

4). If a session has a control terminal, then it has a foreground process group, and all other process groups in this session are background process groups.

5). Whenever we enter the terminal's interrupt key (often delete or control-c), the interrupt signal is sent to all processes in the foreground process group.

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

7). If the terminal interface detects a modem or a network connection is disconnected, the pending signal is sent to the control process (the session first process).

6.tcgetpgrp, TCSETPGRP, and TCGETSID functions

You need a way to tell the kernel which process group is the foreground process group, so that the end device driver card can understand where the terminal input and the signal from the terminal are.

#include <unistd.h>pid_t tcgetpgrp (int filedes);//returns the process group ID of the foreground process group; error return -1int tcsetpgrp (int filedes, pid_t pgrpid) ;//returns 0, error returned-1

The TCGETPGRP function returns the process group ID of the foreground process group that is associated with the terminal that is opened on Filedes. If the process has a control terminal, the process can call TCSETPGRP to set the foreground process group ID to pgrpid.

SUS defines a xsi extension, called Tcgetsid, to allow an application to get the session leader's process group ID, given a file descriptor that controls the TTY.

#include <termios.h>pid_t tcgetsid (int filedes);//Returns the session leader's process group ID. Error returned-1

An application that needs to manage the control terminal can use TCGETSID to identify the session ID that controls the first process of the terminal session (it is equivalent to the process group ID of the session's first process).

7. Operation Control

Job control requires three forms of support:

(1) Shell that supports job control.

(2) The terminal driver in the kernel must support job control.

(3) Support for certain operating control signals must be provided

8.shell Execution Program

Here are just a few examples to see Apue

Shell command:

Ps-o Pid,ppid,pgid,sid,comm | CAT1 | Cat2

Its 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 pipeline is the child process of the shell, and the process that executes the other commands in the pipeline is the child of the last process

9. Orphan Process Group

A process that has been terminated by a parent process is called an orphan process (orphan), which is adopted by the INIT process.

Terminal process relationships

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.