The basic concept (process, process group, session relationship) process of linux kernel

Process is a core concept of the operating system. Each process has its own unique identity: Process ID, also has its own life cycle. A typical life cycle of the process shown in Figure 4-1.

 
 
Process has a parent process, the parent process also has a parent process, which formed a init process is the root of the family tree. In addition, there are other hierarchy process: process, process group and session.
Process group and the process of formation of a conversation between two levels: process group is a collection of related processes of a group session is a collection of related process group.
In this light, a process will have the following ID:
· PID: a unique identification process. For multithreaded process, all threads calls getpid function returns the same value.
· PGID: process group ID. Each process will have a process group ID, process group that represents the process belongs. By default, a newly created process will inherit the parent's process group ID.
· SID: Session ID. Each process also has a session ID. By default, a newly created process inherits the parent process ID of the session.
You can call the following command to view the hierarchy of all processes:
  1. ps -ejH
  2. ps axjf
Process, you can call the following function to get the process group ID with the session ID.
  1. pid_t getpgrp(void);
  2. pid_t getsid(pid_t pid);
As mentioned earlier, the new process inherits the parent's default process group ID and session ID, if all default, then traced seen that all processes should be a common process group ID and session ID. But to call ps axjf can see that is not the case, a lot of different sessions exist in the system, at every session there are different process group.
Why is this so?
Like the family business, from the beginning, all family members are rigid, conformist, by default, will only have a company, a department. But also the children of some "rebellious" willing to explore and expand the family company, is willing to set up a new department. The new department is to process the newly created group. If you have children, "deviant" and even do not want to stay in the family company, he distinguished from the world, to create a separate company, the new company that conversation group is newly created. Thus, the system must be changed and set the process group ID and session ID function interface, otherwise, there will be only one session in the system, a process group.
Process group and the session is to support shell job control concepts introduced.
When a new user logs on Linux, the logon process creates a session for the user. The user's login shell is the first course session. The first session will process ID as ID entire session. A session is a collection of one or more processes group encompasses all activities logged-on user.
In the login shell, the user may use the pipeline to allow multiple processes to each other with the completion of a task, this group belong to the same process a process group.
When the user through the SSH client tool (putty, xshell, etc.) even when the Linux, log on to the above scenario is similar.
4.2.1 Process Group
Modify process group ID of the interface as follows
  1. int setpgid(pid_t pid, pid_t pgid);
The implication of this function is to find the process ID for the process pid will modify its process group ID is pgid, if pid is 0, it means that you want to modify process group ID of the calling process. This interface is generally used to create a new process group.
Consistent with the meaning of the following three interfaces, it is the creation of a new process group, and the specified process will be the first process of the process group. If the value of the parameter pid and pgid do not match, then the function will setpgid a process migration process from the original group belongs to pgid corresponding process group.
  1. setpgid(0,0)
  2. setpgid(getpid(),0)
  3. setpgid(getpid(),getpid())
setpgid function has a number of limitations:
· Pid parameter must be specified as a function call setpgid process or sub-process and can not modify process group ID is not related to the process, if this rule is violated, then return -1, and set errno for ESRCH.
· Pid parameter can specify a child process of the calling process, but if the child process exec function has been executed, you can not change the process group ID of the child process. If this rule is violated, then return -1, and set errno for EACCESS.
• The inter-process group is moved, the calling process, pid specifies the process and target process group must be within the same session. This is better understood, not to join the company (session), you can not join the department (Process Group) Company subsidiary, or is the department going to rebel rhythm. If this rule is violated, then return -1, and set errno to EPERM.
· Pid specified process, the session is not the first process. If this rule is violated, then return -1, and set errno to EPERM.
 
With create an interface process group, process group does not have to inherit the newly created process group ID of the parent process. The most common scenario is to create process groups in execution pipeline command shell, code is as follows: CMD1  CMD2  CMD3
Here with a simple command to illustrate the relationship between its process shown in Figure 4-2.
ps ax|grep nfsd 
 
 
 
Ps processes and sub-processes bash grep processes are created, both by pipeline to complete a work cooperative, they belong to the same process group, which ps process is the process group leader.
The concept process group is easy to understand the relationship between people can make an analogy. Colleagues working together, naturally more intimate than irrelevant passers-by. People work together in the process of shell belong to the same process group, just as working together as part of the same department.
Introduces the concept of group process, can more easily manage the process of this group. For example, gave up the job, do not all need to send a signal to each process, you can send a direct signal to the process group, all processes in the process group to receive the signal.
        Previously it has been mentioned that, once the child process executed exec, the parent process can not call setpgid function to set the child process group ID, and this rule will affect the operational control of the shell. For insurance reasons, usually the parent process after calling fork to create a child process, calls setpgid function sets the process group ID of the child, while the child process should call setpgid function to set its own process group ID. Once these two calls are extra, but doing so can guarantee both the parent process before execution, or the child process before execution, the child must have entered the specified process group. Because after the fork, the execution order of parent and child is uncertain, so if you do not do this, it will cause within a certain window of time, the child process can not determine whether to enter the corresponding process group.
    The user can simultaneously execute a plurality of commands in the shell. For a long time consuming command (such as compiling a large project), the user does not have to wait for the command to run silly finished before executing the next command. When users execute commands, you can add the "&" symbol at the end of the command, said it would command into the background. In this way the command corresponding process group is the background process group. At any time, there may be multiple simultaneous background process group, but no matter what time can have only a foreground process group. Only the process can be read in the control input terminal in the foreground process group. When the user terminal generates a character in the input signal terminal (e.g. ctrl c, ctrl + z, ctr + \ +, etc.), a signal corresponding to a foreground process group only.
The concept shell can contain multiple process groups, either the foreground or background process group process group, they are more or less there is a certain link, in order to better control the process group (or called a job), the system introduces the session . Meaning that the session will include a lot of work in one terminal, select one as a reception terminal to receive input signals directly and, on the other jobs are executed in the background.
 
Conversation
A session is a collection of one or more process group, the user login system, for example, there may be the case as shown in Figure 4-3.
 Setsid system provides functions to create a session, the interface is defined as follows:
  1. #include <unistd.h>
  2. pid_t setsid(void);
If the calling process is not the function of the process group leader, then call this function will occur the following things:
1) Create a new session, the session ID equal to the process ID, the process of the calling process to become the first session.
2) create a process group, process group ID equal to the process ID, the calling process to become head of the process group.
3) The process does not have control terminal, if you call before setsid, the process has a control terminal, this link will be broken.
Setsid function call process can not be a process group leader, or the call will fail, return -1, and set errno to EPERM.
This restriction is reasonable. If the process is allowed to migrate to the new head of the session, while other members of the group are still in the process of the old session, then the process will be the same process group belonging to the situation arise among the different sessions, which destroyed strict hierarchy of process groups and sessions.
 
 

Guess you like

Origin www.cnblogs.com/aspirs/p/12503123.html
Recommended