Where are the kernel mechanisms used?
1. The communication between processes (the address space is allocated in the virtual memory of the cpu, and the address space of each process is completely independent; the number of processes executed at the same time does not exceed the number of cpu) communication requires the use of a specific kernel mechanism.
2. Inter-process switching (the number of processes executing at the same time does not exceed the number of CPUs), and the kernel mechanism is also needed.
Process switching also needs to save the state like FreeRTOS task switching, and put the process in an idle state/recovery state.
3. Process scheduling. Confirm which process is running for how long.
Linux process
1. With a hierarchical structure, each process depends on a parent process. The kernel starts the init program as the first process. This process is responsible for further system initialization operations. The init process is the root of the process tree, and all processes originate directly or indirectly from this process.
2. Query through the pstree command. In fact, the first process of the system is systemd, not init (this is also a questionable point)
3. Each process in the system has a unique identifier (ID), and the user (or other process) can use the ID to access the process.
Linux mechanism for creating new processes;
1. Use the fork function.
Function: Create a copy of the current process. The parent process and the child process only differ in PID (process ID). The content of the parent process is copied. (Copy-on-write: you can understand this concept)
2. Use the exec function.
Function: Load a new program into the memory of the current process and execute it. The memory page of the old program will be flushed out, and its content will be replaced with the new data. Then start the new program.
Linux thread
A process can be regarded as an executing program, and a thread is a program function or routine that runs in parallel with the main program.
Linux creates a new thread
Use the clone method to create threads.
Kernel thread
Features:
1. Not associated with any specific user space process.
2. Compared with the kernel running in the interrupt context, the kernel thread can enter the sleep state and can also be tracked by the scheduler like a normal process in the system.
Purpose:
1. Data synchronization between the slave memory and the block device.
2. Help the scheduler allocate processes on the CPU.
How to view kernel threads:
Command: ps fax The kernel thread is in square brackets.
Address space and privilege level
Before the formal introduction, introduce a few units:
KiB 2^10 bytes
MiB 2^20
GiB 2^30
Address space
Virtual address and physical address:
Virtual address: refers to a physical address that identifies a virtual (non-physical address) in computer-specific terms.
Physical address: A tangible address. In the memory, information is stored in bytes. In order to store or obtain information correctly, each byte unit is given a unique memory address, which is called a physical address.
Mapping relations:
Use page tables to assign virtual addresses to physical addresses
Note: Not all pages of the virtual address are mapped to a page frame.
the reason:
1. The page has no memory to use
2. The data is not yet used and not loaded into memory.
3. The page has been swapped out of the hard disk, and then swapped back to the memory when needed.
Page frame: physical memory page
Page: specifically refers to the page in the virtual address space.
Page table: A data structure that maps virtual address space to physical address space.
Multi-level paging: A method of establishing a virtual address to physical address mapping, which can effectively reduce memory usage.
CPU accelerates the memory access process, method:
1.MMU (Memory Management Unit) memory management unit
2.TLB (Translation Lookaside Buffer) address translation backup buffer
The CPU of IA-32-bit structure only needs to use the two-level page table, the Linux kernel always uses the four-level page table, and the remaining part is simulated by the kernel through the control page table.
Concept note:
1. The word length of the CPU determines the maximum length of the address space that can be managed. For 32-bit systems it is 2^32 B = 4GiB
2. Virtual address space: The maximum length of the address space has nothing to do with the amount of physical memory actually available.
The virtual address space is divided into two parts:
Kernel space
User space
There is an equivalent name worth mentioning:
User layer: refers to the application itself. Refers to things that are not part of the kernel.
User space: It can not only refer to the application, but also refers to a part of the virtual address space where the application runs. Contrast with kernel space.
Note: Each process has the above independent virtual address space.
Privilege level
Linux has two states, two states represent two privilege levels:
Core state: the state of running in the kernel space of the virtual address.
User state: the state of running in the user space of the virtual address.
Note: Access to kernel space is prohibited in user state
The difference between these two states is: access to a memory area higher than TASK_SIZE.
System call
Definition: The switch from the user state to the core state is completed by a specific method called by the system.
Method:
1. The kernel executes code on behalf of the user program
2. Asynchronous hardware interrupt is activated and then performed in the interrupt context. Note: The kernel cannot go to sleep while the interrupt context is running.
note:
Most of the CPU is executing code in user space. When the application executes a system call, it will switch to the core state, and the kernel will complete its request. During this period, the kernel can access the user part of the virtual address space. After the system call is completed, the CPU switches back to the user state.
The hardware interrupt also makes the CPU switch to the core mode, but in this case the kernel cannot access the user space.
What are the system calls:
Process management
Signal
File
Directory and file system
protection mechanism
Timer function
To
For example, on the IA-32 processor, Linux uses a dedicated software interrupt to execute system calls.
Device drivers, block devices, and character devices
The task of the device driver is to support the application to communicate with the device via the device file. In other words, it makes it possible to read/write data on the device in an appropriate way.
Definition of device driver: used to communicate with input/output devices connected to the system. Such as hard disk, floppy drive, various interfaces, sound card, etc.
Peripherals are divided into two categories:
1. Character equipment. Provide a continuous data stream, the data can be read sequentially, usually does not support random access. Support byte/character to read and write data.
Typical: Modem
2. Block device. The device data can be accessed randomly, and the program can determine where to read the data by itself. Character-based addressing is not supported, and data reading and writing can only be performed in multiples of the block.
Typical: hard disk.
Complexity: It is more complicated than character devices because it is designed to cache mechanism.
The internet
The network card can also be controlled by the device driver.
But in the kernel, the network card cannot be accessed as a device file. Because data is packaged into various protocol layers during network communication, memory processing data must be processed at the protocol layer to effectively connect the data to the application.
In order to support the file interface to handle network connections. Linux provides an abstraction of sockets, which can be seen as a proxy between application programs, file interfaces, and kernel network implementations.
File system
Files are stored on hard disks or other block devices (such as CDs, etc.), using a hierarchical file system.
The file system uses the directory structure to manage the stored data and associates other original information (such as owner, access permissions, etc.).
Linux supports many different file systems:
Such as standard Ext2 and Ext3 file systems, ReiserFS, XFS, VFAT, etc.
Note: The software layer (Virtual Filesystem virtual file system) kernel must provide an additional software layer to isolate the specific characteristics of the various underlying file systems from the application layer.