Process groups and session __linux in the Linux kernel

Source: Internet
Author: User
Tags goto joins sessions sigint signal

Turn from: http://shake863.javaeye.com/blog/187085

The following concepts are described in the Linux kernel
1) Process Group
2) session
3) Control terminal

1. Concept:
A) Process group
A command line on the Shell forms a group of processes
Each process belongs to a process group
Each process group has a lead process
The lifecycle of a process group to the end of the last process in the group or to a different process group
GETPGRP: Get the process group ID, that is, the PID of the lead process
Setpgid: Join a process group and create a new process group
Foreground process groups and background process groups
#include <unistd.h> int setpgid (pid_t pid, pid_t pgid); pid_t getpgid (pid_t pid); int setpgrp (void); pid_t getpgrp (void);

A process can only set itself and its child processes to the process group ID.
After a child process calls the EXEC function, the ID of that child process cannot be used as the process group ID.
===============================================================================

b) session
One logon to form a session
A session can contain multiple process groups, but only one foreground process group.
Setsid can create a new session
===============================================================================
#include <unistd.h>

pid_t setsid (void);
-------------------------------------------------------------------------------
If the calling process is not the lead process for a process group, the function can establish a new session.
After the Setsid is invoked, the process becomes the lead process for the new session.
The process becomes the lead process for the new process group.
Process loses control terminal
===============================================================================

c) Control terminal
After the lead process of the session opens a terminal, the terminal becomes the control terminal for the session (Svr4/linux)
The session lead process that establishes a connection with the control terminal is called the control process (sessions leader)
A session can have only one control terminal
All processes in the foreground process group that generate input and signal on the control terminal to be sent to the session
When a connection is disconnected on a terminal (such as a network disconnect or Modem disconnect), the suspend signal is sent to the control process (session leader)

In general, since Linux is a multiuser system, at the same time, there are multiple processes in the system that belong to different users. Then, when a user on a terminal presses the CTRL + C key (generating a SIGINT signal), how does the system know which process to send the signal to, so that it does not affect the process run by the user on the other terminal?

The Linux kernel manages multi-user processes by maintaining sessions and process groups. As the following illustration shows, each process is a member of a process group, and each process group is a member of a session. Generally speaking, a new session begins when a user logs on to a terminal. The process group is identified by the lead process in the group, and the process identifier of the lead process is the group identifier for the process group. Similarly, each session should have a lead process.

A process in the same session is connected to a terminal through the lead process of the session, which acts as the control terminal for the session. A session can have only one control terminal, while a control terminal can control only one session. Through the control terminal, the user can send the keyboard signal to the process in the session controlled by the control terminal.

There can be only one foreground process group in the same session, processes that belong to the foreground process group can get input from the control terminal, while the other processes are background processes and may be part of different background process groups.


2. Examples of implementations in Linux to verify the above rules:

Asmlinkage long Sys_getpgid (pid_t pid) {if (!pid) {return current->pgrp;} else {int retval; struct task_struct; Read_lock (&tasklist_lock); p = find_task_by_pid (PID); retval =-esrch; if (p) retval = p->pgrp; Read_unlock (&tasklist_lock); return retval; }/* This needs some heavy checking ... * I just haven ' t the stomach for it. I also don ' t fully * understand SESSIONS/PGRP etc. Let somebody who does explain it. * OK, I am have the protection semantics right ... this is really * only important on a multi-user system anyway, t o Make sure one user * can ' t send a signal to a process owned by another. -TYT, 12/12/91 * * auch. Had to add the ' did_exec ' flag to conform completely to POSIX. * LBT 04.03.94/asmlinkage long Sys_setpgid (pid_t pid, pid_t pgid) {struct task_struct * p; int err =-einval; if (!pid ) PID = current->pid; if (!pgid) pgid = pid; if (Pgid < 0) Return-einval; /* From this point forward we keep holding onto the tasklist lock * so thaT our parent does don't change from under us. -DAVEM * * Read_lock (&tasklist_lock); /* First Prerequisite: First to verify that the process is set to exist, does not exist can not do things/err =-esrch; p = find_task_by_pid (PID); if (!p) goto out; /* Second Prerequisite: First check the permission to do this: the current process can only set itself and its child processes to the process group ID, and the current process and its child processes must belong to the same session (the same group of process must belong to the same session)/if (p->p_pptr = = | | p-& Gt;p_opptr = = current) {err =-eperm////If not part of the same session (same console), do not have the */if (p->session!= current->session) goto out; err =-eacces; /* After a child process calls the EXEC function, the ID of the subprocess can no longer be id*/to the process group if (p->did_exec) goto out; else if (P!= current) goto; err =-eperm; /*boolean value for session Group Leader * * * If it is a conversation leader, it is also not possible to note that the leader process of the process group can also change the group ID * * IF (p->leader) goto out; * Good! A few prerequisites are all satisfied, to do business: but is not the legality of the group number has not been verified? See something!*//* To set the process number is not to set the group number, if it is, directly set, because this means that is added to their own PID as the new group number of the process group, this The process will also become the leader process for the new process group, so there is no need to compare the session number and be sure that you are the same session. If the condition is not satisfied, make these judgments/if (Pgid!= pid) {struct task_struct * TMP; for_each_task (TMP) {/* Can not find a process, the group number is exactly the group number to set, and to set the process belongs to the same console (the same session) to find can be set, in fact, here is to determine the legality of the group number, that is, must be an existing group, andCurrently the same session can operate, this also can not forget, in fact, that is to say: the process of the same group must belong to the same session/if (tmp->pgrp = = Pgid && Tmp->session = = Current->session ) goto Ok_pgid; Goto out; } ok_pgid:p->pgrp = Pgid; Err = 0; Out:/* All paths leads to here, thus we are safe. -DAVEM * * Read_unlock (&tasklist_lock); return err; Asmlinkage long Sys_getsid (pid_t pid) {if (!pid) {return current->session;} else {int retval; struct task_struct *p; Read_lock (&tasklist_lock); p = find_task_by_pid (PID); retval =-esrch; if (p) retval = p->session; Read_unlock (&tasklist_lock); return retval; } asmlinkage long Sys_setsid (void) {struct task_struct * p; int err =-eperm; Read_lock (&tasklist_lock); for_each_t Ask (P) {/* * * If the current process is the leader of a process group, you cannot create a new session/if (p->pgrp = = current->pid) goto out;/* Set the leader of the newly created session to the creator is the current process/C Urrent->leader = 1; /* Clearly see a new process group was born the current process becomes the leader of the new process group the ID of the session is the current process number and the leader/current->session = CURRENT-&GT;PGRP = Current->pid ; /* The current process is out of control terminal/Current->tty = NULL; CuRRENT-&GT;TTY_OLD_PGRP = 0; Err = current->pgrp; Out:read_unlock (&tasklist_lock); return err; }  

3. Some understanding of processes, process groups, sessions

Turn from: http://linux.chinaunix.net/bbs/viewthread.php?tid=766191

(1) A process must belong to a process group, or it can belong to only one process group.
You can include multiple processes in a process group.
The lifecycle of a process group starts at the time it is created, and all processes in it terminate or leave the group.

Gets the process group ID of the current process using the function getpgrp
Create or join other groups using functions Setpgid

(2) Assuming conditions: the PID1 process belongs to the PGID1 process group, Pid2 belongs to the PGID2 process group, and is the PGID2 process group leader, and the process group PGID3,

Call Setpgid (PID2,PGID3) in the PID1 process;

A) when Pid2 and PGID3 are >0 and unequal
Function: Joins the PID2 process to the PGID3 group. The PID2 process is now detached from the PGID2 process group and into the PGID3 process group.
b when Pid2 and PGID3 are >0 and equal
Function: The PID2 process creates a new process group and becomes the new process leader (PGID3=PID2).
c) When pid2==0,pgid>0
Function: Joins the calling process Pid1 into the PGID3. At this time pid1 out of Pgid1, into the PGID3.
D) When pid2>0,pgid==0
Features: Adds PID2 to the PGID1 process group where the calling process resides. At this time pid2 out of Pgid2, into the pgid1.
e) when both Pid2 and PGID3 are ==0, an error is returned.

(3) A session is formed once a login is made, and the session leader is the process that creates the session.
Only processes that are not leader of the process can create a new session.

(4) If the PID1 process belongs to the PGID1 process group and is not a leader, it belongs to the session sid1.
Call Setsid () in the PID1 process;
Function: PID1 process from PGID1 process Group, create a new session SID2 (Sid2 no Control terminal), PID1 process to join PGID2 Group (PGID2==PID1).

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.