Reading notes-apue Third Edition-(9) Process relationship

Source: Internet
Author: User
Tags joins

Logon process
Terminal Login

Init reads the terminal information from the/etc/ttys, and for each terminal that is allowed to log in, fork the child process and execute Getty. Getty open End Device and execute login:

Execle ("/bin/login," "Login", "-P", username, (char *) 0, ENVP)

Login username and password authentication, but also switch to the user home directory/modify the host terminal & read/write permissions/create process group/setuid and other actions, and then run the login shell.

Execl ("/bin/sh", "-sh", (char*) 0)

The login Shell's standard input, output, and error outputs are set as terminal devices, and the login shell also reads. Bash_profile Initialization files

Network Login

The maximum difference between a network logon is that the connection between the terminal and the host is not peer-to-peer. As with network services such as FTP and SMTP, network logons are nothing more than a service.

, the system processes the network connection through the inetd (xinetd) daemon. Assume that the user is running on another host

telnet hostname
The inetd runs the corresponding telnetd program according to the network connection type fork out child process. TELNETD will turn on the pseudo terminal and fork. The parent process handles network traffic, the child process runs the login program, and the next process is consistent with the terminal login described in the previous section. (The communication details between the pseudo-terminal and the actual user terminal are described in subsequent chapters)
Process Group

#include <unistd.h>pid_t getpgid (pid_t pid); int Setpgid (pid_t pid, pid_t pgid);

A process group is a collection of a set of processes. The process creates or joins a process group by calling the Setpgid method. If the process ID and the process group ID are equal, the process is called the leader (leader). The life cycle of a process group starts at creation and ends in the last process in the group (the leader may hang up without a relationship). During the shell's job-control process, the parent process, when the child process is fork, calls the Setpgid method to ensure that the child process joins the process group.

Session

A session is a collection of set of process groups. For example, the process group layout in a session is typically generated by the shell pipeline:

PROC1|PROC2 & PROC3|PROC4|PROC5


#include <unistd.h>pid_t getsid (pid_t pid);p id_t setsid (void);

The process calls Setsid to create the session, which becomes the session leader, creating a new process group and invoking the process as the process group leader. Process call Setsid, which is already the leader of the process group, will fail, so the common process is to terminate the parent process after it has been forked out of the child process, and let the child process create the session because the process group ID of the child process inherits from the parent process, so it must not be the leader.

Control Terminal

A session can have only one control terminal. The session leader that establishes the connection with the control terminal is called the control process. The foreground process group is the process group that owns the control terminal, and the interrupt signal (CONTROL+C) is sent to all processes in the foreground process group.



Include <unistd.h> 45pid_t tcgetpgrp (int filedes); int tcsetpgrp (int filedes, pid_t pgrpid)

The foreground process group can be obtained and set up through Tcgetpgrp and TCSETPGRP. Only processes that have control terminals can invoke the Tcsetpgrp method. These two methods are typically used for job control and are not called directly by the application program.

Job Control

A job is typically a group of processes that consist of a set of processes through a pipeline connection. After the command line plus & Start is the background job. Type CTRL + Z and send a SIGTSTP signal to pause the foreground job and turn it back into the background. Only the foreground job can receive the terminal input, when the background job tries to read the terminal input, the terminal driver sends the sigttin signal to the background job, the background job pauses, and reports the job status on the standard output. Try it on your Mac, enter FG view to turn the background job into foreground, hang up. This is said to be a Mac bug. The background job output to the terminal is not a problem, but you can disable the output through the Stty tostop command.

Summarizes the interaction between the foreground process group, the background process group, and the terminal in the session. Where the Sigttou signal occurs when the background process group wants to output.


Shell Run Program

Experiment with shell execution procedures and process leasing, control terminal, session and job control concepts such as the corresponding relationship.

Mac is not very good, this time to look at Linux, the first is the pipeline:


All three processes belong to the same session (26071), PS and Cat are the child processes of Bash (28629), belong to the same foreground process group (17121), have control terminal, PS is the leader, and bash belongs to a process group (28629) alone.

Try the background job again:


The biggest difference is that the foreground process group with the control terminal becomes the group of Bash.

Finally verify the continuous pipeline, it seems that Bash fork out three sub-processes.


Orphan Process Group

When the parent process of all members of a process group is either in that group, or is not part of the session (the child process that the parent process hangs prematurely is "adopted" by Init), the process group is called the orphan process group. The system will send Sighup (the default behavior is to terminate the process) and Sigcont (continue running) signals to the orphan process group.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Reading notes-apue Third Edition-(9) 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.