Linux system programming (sessions and processes)


Preface

In this article, we will explain the concepts of sessions and processes. Sessions may be relatively rare, and their English name is session.

1. The concept of conversation

In Linux, a session refers to a period of time during which the user interacts with the operating system. The session concept under Linux is based on the terminal, and the terminal can be a physical terminal, a virtual terminal (such as TTY) or a remote connection (such as SSH).
以下是Linux中会话的几个关键概念:

1. Control terminal:
In a session, there is usually a control terminal that provides users with an interactive interface for input and output. Users interact with the operating system through the control terminal, including entering commands, running programs, and viewing output.

2. Terminal session:
A user can start one or more terminal sessions on the control terminal. Each terminal session is an independent process group, containing a foreground process group and zero or more background process groups.

3. Foreground process group:
The foreground process group is the process group that the current user is interacting with. When the user enters a command from the control terminal, the command is sent to the foreground process of the foreground process group for processing. Only one process group can be in the foreground.

4. Background process group:
A background process group is a process group that runs in a terminal session, but without user input interaction. Users can switch a process from the foreground to the background, allowing it to run in the background without blocking the terminal.

5. Session manager:
Linux manages terminal sessions through a session manager. The session manager is responsible for creating and managing sessions, setting up the controlling terminal, and cleaning up resources when the session ends.

6. Session logout and disconnection:
Sessions are typically terminated when the user logs out or disconnects from the terminal. Session termination causes all processes on the terminal (foreground and background) to receive appropriate signals to clean up and terminate.

Session is an important concept in Linux. It provides an interactive environment between the user and the operating system and manages the user's process group. Knowing and understanding the session concept in Linux is crucial to correctly manage and control user interaction behaviors.

2. The difference between session and terminal

Session and Terminal are two related but not identical concepts.

A session refers to the interaction process between the user and the operating system or application, involving user authentication, persistent connections, state maintenance, etc. In a session, the user can communicate with the system through a terminal or other interactive means.

A terminal is a device or interface that provides user interaction with a computer. Users can enter commands, view output, and interact with the system through the terminal. Terminals can be physical devices (such as physical terminal devices, consoles) or virtual devices (such as virtual terminals, SSH connections). Terminals provide input and output channels between users and the system.

下面是它们之间的区别:

1. Conceptual level:

A session refers to the interaction process between the user and the operating system or application, including user authentication, persistent connections, and state maintenance.
A terminal is a device or interface for users to interact with computers. It provides a channel for user input and output.

2. Association:

Sessions and terminals are often related. A user can start a session through a terminal device or virtual terminal and interact with the system in the session.
A terminal can have multiple sessions, such as multiple virtual terminal sessions opened on the same terminal device.

3. Function:

Sessions involve functions such as user authentication, state maintenance, and persistent connections, and are used to manage the interaction between users and the system.
The terminal provides the functionality for users to enter commands, view output, and interact with the system.

4.Physical properties:

A session is an abstract concept that represents the interaction process between the user and the system and does not involve specific physical devices.
Terminals can be physical devices (such as keyboards, monitors) or virtual devices (such as virtual terminals, SSH connections).

Insert image description here

When the command line shell executes a new command to create a new process:

Use & to create a new process. The newly created process is a background process, and you are still a foreground process.

Do not use & to create a new process. The newly created process is a foreground process and itself is set as a background process.

3. Terminal process group identification

In Linux, each terminal session has a unique process group identifier (PGID). The terminal process group identifier is introduced to implement job control and process management.
The processes in each terminal session belong to a process group. When a user starts a new process on a terminal, by default the process is assigned to the process group of the same terminal session.

终端进程组标识具有以下特点:

1. The process group identifier of the session leader:
In each session, a session leader will create the session. The process group identifier of the session leader is the same as the session ID (Session ID, SID).

2. Foreground process group:
In a terminal session, there can only be one foreground process group (foreground process group). This foreground process group typically receives user input from the terminal and sends output to the terminal. Commands entered by the user on the terminal will be sent to the foreground process in the foreground process group.

3. Background process group:
A terminal session can contain multiple background process groups at the same time. A background process group is a process group that runs within a terminal session but does not accept user input. Background process groups can run in a terminal session without blocking the terminal.

By using the terminal process group identifier, the system can manage job control, including running process groups in the foreground or background, switching between front and back process groups, sending signals, etc.
To view the current terminal process group ID, you can use the echo $$ command. This command will output the process ID (PID) of the current Shell process. The process group ID of the Shell process is the terminal process group ID.

4. Create a session

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    
    
    pid_t pid;

    // 创建一个子进程
    pid = fork();
    if (pid < 0) {
    
    
        fprintf(stderr, "无法创建子进程\n");
        return 1;
    } else if (pid == 0) {
    
    
        // 子进程

        // 创建一个新的会话
        if (setsid() < 0) {
    
    
            fprintf(stderr, "无法创建新会话\n");
            return 1;
        }

        // 关闭标准输入、输出和错误输出
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

        // 从这里开始,子进程就可以执行会话中的任务

        // 示例:打开一个日志文件并写入日志
        FILE* logFile = fopen("/var/log/mylog.txt", "w");
        if (logFile == NULL) {
    
    
            fprintf(stderr, "无法打开日志文件\n");
            return 1;
        }

        fprintf(logFile, "会话已启动\n");
        fprintf(logFile, "执行一些任务\n");

        // 关闭日志文件
        fclose(logFile);

        // 子进程完成任务后退出
        exit(0);
    } else {
    
    
        // 父进程
        // 这里可以选择等待子进程完成或继续执行其他任务
        printf("子进程的PID:%d\n", pid);
    }

    return 0;
}

This example program creates a child process. The child process creates a new session by calling the setsid() function, then closes the standard input, output and error output, and starts executing the tasks in the session (this is only an example, it can be done according to specific needs. Revise). The parent process can choose to wait for the child process to complete or continue performing other tasks.

Summarize

This article will explain it here.

Guess you like

Origin blog.csdn.net/m0_49476241/article/details/133047934