The composition of the Linux kernel subsystem
Composed of the above 7 subsystems.
One, memory management subsystem
Functions:
1, from the virtual address to the physical address mapping management.
2, the physical memory allocation management.
2.1 Virtual address space distribution
The virtual address is used in Linux, but the physical address is used when accessing the hardware. For example, the program uses the malloc function to assign the virtual address, but the hardware is used in the physical address, which has a transformation relationship.
The virtual address space it supports is determined by the hardware address bus select、read width, such as the 32-bit address bus that supports 4GB of virtual memory.
User space (0-3G): User program.
Kernel space (3-4G): Direct mapping area 3G-3G+896MB.
Vmalloc District,
Permanent kernel mappings,
The linear address of the fixed map.
2.2 Virtual address into physical address
CR3 stores the base address of the page directory.
The high 10-bit and CR3 registers add together to find the page catalog entries in the page directory.
The page directory entry holds the base address of the page table.
The page table's base address plus the median 10-bit value can be found on the physical page.
The physical page, plus the last 12-bit offset, will find the physical page (usually 4k) of the storage unit.
Direct mapping Area: Virtual address =3g+ Physical Address
Vmalloc area: Can be mapped to high-end and low-end memory
Permanent mapping Area: high-end memory
Fixed-map linear address: Fixed access to some registers
Allocation of physical memory
The program through the malloc and other functions to apply to the virtual address, when the need to use the physical address of the page through the exception to obtain physical address. The virtual address and physical address are already bound when the virtual address is obtained through Kmalloc.
Second, process management subsystem
Program: An executable image of a series of code and data stored on disk that is a static entity
Process: is an executing program in which he is a dynamic entity.
Process four elements
1. There is a procedure for its implementation. This procedure is not necessarily proprietary to a process and can be shared with other processes.
2. There is a process-specific kernel space stack.
3. There is a TASK_STRUCT data structure in the kernel, commonly referred to as the "Process Control block". With this data structure, the process can become a basic unit of kernel scheduling to accept the scheduling of the kernel.
4. Has the independent user space
Linux process status
1.task_running
The process is being executed by the CPU or is ready to be executed at any time. When a process has just been created, it is in the task_running state.
2.task_interruptinle
A process that is in wait, is awakened when the condition is true, or can be awakened by a signal or interrupt.
3.task_uninterruptinle
A process in wait to wake up when the resource is valid, but not by other processes (signal) or interrupts.
4.task_killable
Linux2.6.25 the newly introduced process sleep state, which is similar in principle to task_uninterruptible, but can be awakened by a fatal signal (SIGKILL).
5.task_traced
Processes that are in the state of being debugged
6.task_dead
The state in which the process exits (calling Do_exit).
Description of the Linux process
In the Linux kernel code, threads, processes are represented by the structure task_struct (sched.h), which contains a large number of information describing processes/threads, which are more important:
pid_t pid; Process number
Long state;//Process State
INT prio;//Process Priority
Linux process scheduling
What knowledge should be mastered in the learning schedule?
1, scheduling strategy
Real-time processes have higher precedence than normal processes.
Sched_normal (Sched_other): a common time-sharing process
Sched_fifo: First-in-first-out real-time process
SCHED_RR: A real-time process of time rotation
Sched_batch: Batch Process
Sched_idle: Processes that can be scheduled to execute only when the system is idle
2. Timing of dispatching
That is, when the schedule () function is invoked.
Active Type:
Call schedule () directly in the kernel. When the process needs to wait for resources and so on and temporarily stop running, will put its own state of suspend (sleep), and actively request scheduling, let the CPU.
Example:
1, current->state = task_interruptible;
2, Schedule ();
Current is a pointer to the task_struct of the currently running process.
Passive: Preemptive type
Divided into: User-state preemption (linux2.4, linux2.6) and kernel-state preemption (linux2.6).
User preemption occurs at:
Returns user space from a system call.
Returns user space from an interrupt handler.
When the kernel is about to return to user space, if the need_resched flag is set, it will cause schedule () to be invoked, that is, user preemption occurs.
When a process runs out of time, it sets the need_resched flag
The need_resched flag is also set when a higher-priority process enters the executable state.
User state preemption Flaw
Once the process/thread has run into the kernel state, it can be executed until it is actively discarded or the time slice is exhausted. This can cause some very urgent processes or threads to run for a long time, reducing the real-time performance of the entire system.
Ways to Improve
Allowing the system to also support preemption in the kernel state, higher-priority processes/threads can preempt low-priority processes/threads that are running in the kernel state.
Kernel preemption can occur when:
The interrupt handler completes before returning to the kernel space.
When the kernel code once again has the ability to preempt, such as unlock and enable soft interrupt.
In the system that supports kernel preemption, preemption is not allowed in some special cases.
The 1 kernel is running interrupt handling.
The 2 kernel is processing the bottom Half (interrupted bottom half) of the interrupt context.
A soft interrupt is performed before the hardware interrupt is returned and is still in the interrupt context.
3 process is holding spinlock spin lock, Writelock/readlock read-write lock, etc.
When these locks are held, they should not be preempted, or the system will be deadlocked because preemption may cause other processes to remain locked for long periods of time.
The 4 kernel is executing the scheduler scheduler. Preemption is the reason for the new scheduling, there is no reason to preempt the scheduler to run the scheduler.
To ensure that the Linux kernel is not preempted in the above case, the preemption kernel uses a variable preempt_count called the kernel preemption count. This variable is set in the THREAD_INFO structure of the process. Each time the kernel enters the above several states, the variable Preempt_count adds 1, indicating that the kernel does not allow preemption. Whenever the kernel exits from the above several states, the variable preempt_count is reduced by 1 while the judgment and dispatch can be preempted.
3. Scheduling steps
The Schedule function workflow is as follows
1) Clean up the current running process;
2 Select the next process to run;
3 Set the running environment for the new process.
4 process Context switch.