[Linux] Process signal (Part 2)

1. Issues related to signal processing

Signal processing is not something that can be processed immediately, but at the right
time


When is the right time? When the process switches
from the kernel mode back to the user mode , the process will detect and process the signal under the guidance of the operating system


Understanding of the concepts of kernel mode and user mode

User state: When executing the code you wrote , the state the user is
in Kernel state: When executing the code of the operating system , the state the process is in


Executing the operating system code:
1. When the process time slice is up and needs to be switched, the process switching logic must be executed.
2. System calls


0-3G belongs to user space
3-4G belongs to kernel space


Load the executable program in the disk into the physical memory
. This page table is called the user-level page table
. The user address space is mapped to the physical memory through the user-level page table.
All codes and data belong to their own code and data.


The operating system also has code and data
How to find the code and data of the operating system?
There is actually a kernel-level page table


All processes 0-3G are different, each process has its own user-level page table
All processes 3-4G are the same, each process can see the same kernel-level page table
so all processes can Through a unified window, you can see the same operating system.
The essence of operating system operation: run in the address space of the process.
No matter how the process is switched, 3-4G remains unchanged. The content of the operating system has nothing to do with process switching.


The essence of the system call: it is equivalent to calling the method in the library function, performing a function jump in its own address space and returning


Why are there user mode and kernel mode?

In order to solve the problem of casually accessing the code and data in the operating system through virtual addresses,
the user mode and kernel mode are proposed.


If it is in user mode, when you want to access the code and data in the operating system, the CPU will refuse to execute the code, and the operating system can identify illegal access, that is, hardware exception, and send a signal to the target process to terminate the process

Use of the CR3 register

How to know whether the user mode or kernel mode is currently running?
There is a register in the CPU, called the CR3 register. If the corresponding bit
is 3, it represents the running process. The execution level is user mode.
If it is 0, it represents the running process. The execution level is kernel state


Who will change the execution level?
Users cannot directly change
all the system calls provided by the operating system. When the call logic is officially executed internally, the execution level will be modified.

The overall process of signal processing

insert image description here
When the process returns, signal detection is required.
When a certain task is executed, first find the corresponding process and detect the corresponding signal.
If block is 0 (blocking signal is not executed), pending is 1 (signal is received), the processing
method To ignore the signal, change pending from 1 to 0


If it is a custom capture, when the system jumps to the past and executes the method written by oneself,
the method written by oneself is actually implemented by user mode.
Although it is possible to use the kernel mode to execute the method implemented by oneself, it is not allowed to do so. Because there may be some illegal operations inside the method


It is divided into four steps
. 1. When the code is executed, it is switched to the kernel mode because of the system call or the time slice.
2. Before returning to the user mode, the signal detection is performed first. There is no block (blocking), and it has been pending (received)
by the kernel. Switch from user mode to user mode and execute the custom handler method
3. After executing the handler method, switch from user mode to kernel mode
4. Finally execute a specific system call and return to user mode to continue execution

Guess you like

Origin blog.csdn.net/qq_62939852/article/details/130547398