UNIX advanced environment programming (12) Process Relationships-terminal logon Process, Process group, Session, unixrelationships
In the previous chapter, we learned that processes are associated with each other:
- Each process has a parent process;
- When a child process exits, the parent process can perceive and obtain the exit status of the child process.
In this chapter, we will understand:
- More details about the process group;
- Sessions content;
- The relationship between login shell and the process we started from login shell.
One-Terminal login (Terminal Logins) BSD Terminal Logins
The BSD logon program has not changed over the past 35 years.
The above process is shown in:
The real user ID of the process created by the init process fork is 0, the valid user ID is 0, and they all have super user permissions.
The role of the program getty: Call the open function for the terminal device. Once the device is opened, the file descriptor 0, 1, and 2 are set to the device. Then, getty outputs some prompts, waiting for us to enter the user name. After entering the user name, the getty operation is complete, and then the login function is executed by calling the exec function, as shown below.
execle(“/bin/login”, “login”, “-p”, username, (char *)0, envp);
After the login program is added, the process is shown in:
The process fork in has the superuser permission, because they are all from the init process fork, and the init process has the superuser permission.
The process IDs of the following processes are the same, because the exec function does not change the process ID, and their parent process IDs are all 1.
Now the login program is transferred to the login program for execution, and the login program will do the following:
If we log on correctly, the login program will continue to do the following:
The process is shown in:
After the shell is started, it reads the Startup file (. profile,. bash_profile,. bash_login, or. profile, different system startup files have different names ). These startup files are used to add system environment variables, set some global variables, links, and so on.
2. Network Logins)
The difference between physical logon and network logon is: whether the connection from the logon terminal to the host is point-to-point.
In the case of network logon, logon is an available service, just like other services, such as FTP or SMTP.
The network login service is characterized by an unknown number of login requests. Therefore, the kernel is not waiting for every possible login, but waiting for a network connection login request through the network interface driver (network interface drivers.
To uniformly process physical and network logins, a software-driven virtual terminal (pseudo terminal) is used to map behavior requests after network logon to real terminals.
BSD Network Logins
The process inetd is waiting to process most network connections.
Next we will understand the network login process.
The process of starting the telnetd program is shown in:
After the telnetd process is started, the action is:
The process is shown in:
3. Process Group)
Each process belongs to a process group.
A process group is a collection of processes that are often associated with the same job and receive signals from the same terminal.
Each process group has a unique process group ID.
The getpgrp function returns the ID of the process group that calls the process.
Function declaration:
#include <unistd.h>
pid_t getpgrp(void);
// Returns: process group ID of calling process
pid_t getpgid(pid_t pid);
// Returns: process group ID if OK, -1 on error
The getpgid (0) of the function call is the same as the getpgrp () of the function call. the ID of the process group that calls the process is returned.
Each process group has a head process. The process ID is the same as the process group ID.
The life cycle of a process group: Starting from the creation of a group by a process, only until the process in the group is terminated or becomes the process in another group.
A process can call the setpgid function to join another process group or create a process group.
Function declaration:
#include <unistd.h>
int setpgid(pid_t pid, pid_t pgid);
The process group ID of the process whose process ID is pid is pgid.
If the pid and pgid are the same as the process ID of a process, the process pid becomes the header process of a process group.
If the pid is 0, the process to be set is the current process.
4 Sessions
A session is a collection of one or more Process Groups.
For example:
A process creates a new session by calling the setsid function.
Function declaration:
#include <unistd.h>
pid_t setsid(void);
// Returns: process group ID if OK, -1 on error
If the calling process is not a group header process, three things will occur:
The getsid function returns the ID of the Process Group of a session leader process.
Function declaration:
#include <unistd.h>
pid_t getsid(pid_t pid);
// Returns: session leader’s process group ID if OK, -1 on error
If the pid is 0, the getsid function returns the process group number of the session leader process where the calling process is located.
References:
Advanced Programming in the UNIX Envinronment 3rd