process group/session(understand)

 Process Group Overview

Process group: also called a job. Represents a collection of one or more processes. Every process belongs to a process group.

        When a parent process creates a child process, the child process and the parent process belong to the same process group by default.

        Process group ID==first process ID (group leader process). Therefore, the group leader process ID: its process group ID == its process ID

        You can use kill -SIGKILL -process group ID (negative) to kill all processes in the entire process group.

        The group leader process can create a process group, create the processes in the process group, and then terminate. As long as there is a process in the process group, the process group exists, regardless of whether the group leader process terminates.

Lifetime: The process group is created until the last process leaves (terminates or transfers to another process group).

                 A process can set a process group ID for itself or its child processes

 Related function description

 conversation (understanding)

A session is a collection of one or more process groups.

  • A session can have one controlling terminal. This is usually a terminal device or pseudo-terminal device;
  • The first session process that establishes a connection with the control terminal is called the control process;
  • Several process groups in a session can be divided into one foreground process group and one or more background process groups;
  • If a session has a control terminal, it has a foreground process group, and other process groups are background process groups;
  • If the terminal interface detects a disconnection, a hangup signal is sent to the controlling process (session leader process).

 Notes on creating a session

1) The calling process cannot be the process group leader. The process becomes the first process of the new session (session header).

2) If the calling process is the group leader process, an error will be returned.

3) The process becomes the leader process of a new process group

4) Root permission is required (not required for ubuntu)

5) The new session discards the original control terminal, and the session does not have a control terminal.

6) When establishing a new session, first call fork, the parent process terminates, and the child process calls setsid

 API function introduction

 Exercise: fork a child process and have it create a new session. Check the changes of process group ID and session ID before and after

Guess you like

Origin blog.csdn.net/weixin_43200943/article/details/129858699