This chapter describes in detail the concepts of process groups and the sessions introduced by POSIX.1. It also describes the relationship between the login shell and all processes initiated from the login shell
Terminal Login
BSD Terminal login. The System Manager creates a file typically named/etc/ttys, where each terminal has a line that describes the device name and the parameters that are passed to the Getty program.
When the system bootstrap, the kernel creates a process with a process ID of 1 (the init process). The init process reads the file/etc/ttys, calls the fork for each terminal device that is allowed to log on, and the child process it generates is the exec Getty program, as shown in:
Getty calls the Open function on the end device to open the terminal in read and write mode. Once the device is turned on, the file descriptor 0, 1, and 2 are set to the device. The Getty then outputs information such as "Login:" and waits for the user to type in the user name.
When the user types the user name, Getty's work is done. It then invokes the login program in a manner similar to the following:
Execle ("/bin/login","login","-p" , username, (char *)0, ENVP);
Shows the status of these processes after login has just been called
If the user is logged in correctly, login will complete the following tasks:
1 Change the current working directory to the user's starting directory (CHDIR)
2 Call Chown to change the ownership of the terminal, which is the owner of the logged-on user
3 Change the access rights of the terminal device to "user Read and write"
4 Calling Setgid and Initgroups set the group ID of the process
5 Initialize the environment with all information from login: Start directory (HOME), Shell (shell), User name (username and logname), and a system default path (PATH)
6 The login process changes to the user ID (setuid) of the logged-on user and invokes the user's login shell in a manner similar to: Execl ("/bin/sh", "-sh", (char *) 0)
Network Login
In the terminal login described in the previous section, Init knows which end-device sections are used to log on and generates a Getty process for each. However, the network logon situation is different, in advance do not know how many such logins.
BSD Network login. As part of system startup, Init invokes a shell to execute the shell script/ETC/RC. This shell script launches a daemon inetd.
INETD waits for a TCP/IP connection request to reach the host, and when a connection request arrives, it executes the fork once, and then the resulting child processes exec the appropriate program.
Telnet is a Telnet application that uses the TCP protocol. Shows the sequence of processes involved in the process of performing the Telnet service
The telnetd process then opens a pseudo-terminal and divides the fork into two processes. The parent process handles communication over the network connection, and the child process executes the login program.
Process Group
Each process, in addition to a process ID, belongs to a process group, which is a collection of one or more processes, with a unique process group ID for each process group.
You can use GETPGRP to return the process group ID of the calling process
#include <unistd.h>pid_t getpgrp (void);
Each process group has a leader process. The process group ID of the leader process equals its process ID
Process call Setpgid can join an existing process group or create a new process group
#include <unistd.h>int setgpid (pid_t pid,pid_t pgid);
The Setpgid function sets the process group ID of the PID process to Pgid. If the two parameters are equal, the process specified by the PID becomes the process group leader.
A process can only set the process group ID for itself or its child processes. After its child process calls exec, it no longer changes the process group ID of the child process.
Session
A session is a collection of one or more process groups.
The process calls the SETSID function to establish a new session.
#include <unistd.h>pid_t setsid (void);
If the process calling this function is not the leader of a process group, this function creates a new session. The following 3 things happen in particular.
1 The process becomes the session first process of a new session
2 The process becomes the leader of 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 calling process is already the leader of a process group, this function returns an error. The GETSID function returns the process group ID of the session's first process
#include <unistd.h>pid_t getsid (pid_t pid); // if the PID is 0, the function returns the process group ID of the session's first process that called the process
Control Terminal
Sessions and process groups also have some other features
11 sessions can have a control terminal. This is usually the end device (in the case of terminal login) or pseudo terminal device (in the case of network logon).
2 establishing a session with the control terminal connection the first process is called the control process.
Several process groups of 31 sessions 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 the other process group is the background process group.
5 whenever you type a terminal's interrupt key (often delete or CTRL + C), the terminal signal is sent to all processes in the foreground process group.
5 whenever you type the terminal's exit key (often ctrl+\), all processes that send out the signal to the foreground process group are said to be exited.
6 If the terminal interface detects that the modem (or network) has been disconnected, send the hang-off signal to the terminal process (session first process)
These features are shown in the
Job Control
a job is just a collection of several processes , usually a process pipeline.
For example: VI Main.c started a job that consisted of only one process in the foreground.
and the following command (after the command & can be a job running in the background):
PR *.C | LPR &
Make All &
Two jobs were started in the background. All processes that have a background job call run in the background.
When a background job is started, the shell assigns it a job identifier and prints one or more process IDs. This is demonstrated below:
When the job finishes and type Enter, the shell notifies the job that it has completed.
Only the foreground job receives the terminal input, and if the background job attempts to read the terminal, a specific signal sigttin is sent to the background job.
the signal typically stops this background job, and the shell sends a notification to the user about the situation, and then the user can then use the shell command to turn the job into a foreground job, and it is readable terminal. This is demonstrated below :
Use the Stty command to allow or disallow background jobs to be output to the control terminal
Apue Study Notes (chapter Nineth process Relationship)