First, what is the Linux kernel:
The most important part of the kernel-and OS, the kernel will be loaded into RAM when the system boots, which contains many key routines to the operating system. The kernel is the most critical part of the OS, and people often equate the OS (OS) with the kernel.
Kernel, is the core of an operating system. It is responsible for managing the system's processes, memory, device drivers, files, and network systems, which determine the performance and stability of the system.
Imagine what the source program with the kernel means to you? We can see how the system works. By reading through the source code, we can understand how the system works, which is virtually impossible under Windows. We can also tailor the system to suit our situation, so we need to recompile the kernel.
Linus Torvalds developed Linux, in fact, he developed the kernel, according to the official homepage of the kernel understanding, the kernel is Linux, and other extensions and applications are built around the kernel. All Linux applications have direct or indirect contact with the kernel, such as the need for kernel support for the hardware, the need for kernel support for network communication, and the need for kernel support for file systems ...
Second, why need to compile kernel and management kernel
Hardware requires kernel support, some hardware support is not programmed into the kernel, which also requires us to re-compile the kernel, the kernel contains not only the device driver, there are other content, such as network protocol support, firewall support ... For example, the implementation of iptables, some functions are required kernel support, if the kernel and iptables related content is not programmed, iptables related functions will not be implemented.
The source tree of the Linux kernel is basically categorized by function.
Kernel: Basic functions
MM: Memory Management
FS: Virtual file system. Subdirectories are various file system related codes
NET: various network protocol related.
Ipc:system V IPC (Shared memory, Semaphore, message)
Init: Kernel boot-related, various initialization
Crypto: Encryption processing
Blocks: Block device handling
Drivers: Various device drivers
Sound: Audio Driver
ARCH:CPU architecture-related code
Include: various header files. When the kernel compiles, it uses
About half of the code for the Linux kernel is in drivers. We can also divide the main functions of the Linux kernel into the following categories from the operating system point of view:
Process Management
Memory management
File System Management
Network management
Inter-process Communication management
Other control management (process scheduling, interrupts and delays, clocks, system calls, synchronous mutexes)
Three, kernel important function:
3.1 Process Management
Each process in Linux has a separate data structure that holds information such as the ID, priority, address space, and so on for the process. The so-called process management is the management of the data structure.
The process ID is the number used to identify the process, in addition to the process ID, and the group ID of the process, and the session ID. In general, processes that have the same control terminal also have the same session ID.
The process in Linux must be generated by the system call of the fork. The process called fork is called the parent process, and the resulting process is called a child process. In general, the child process at the time of birth, with almost the same data structure as the parent process, the same process space, the same role directory, the same signal setting state and so on, the only difference is the process ID. After the fork is finished, the child process is immediately added to the dispatch queue. Fork out a child process if the exact same thing is not the same as the ID and the parent process, then it is usually called by an exec system call after the fork is finished, to load a new program and give the child process a new task.
What exec does is re-assign the information about the process space, and the files, directories, and so on will not change.
Exit system call can end the life of a process. Release the space used, close the file, and other recycling work. Also, since the child process joins the dispatch queue immediately after it is forked, the parent process and the child process are almost identical, so the next action after the fork is the parent process or the child process is often indeterminate. If you call the wait system call in the parent process, the parent process will reflect the greatness of the parent, let the child process take precedence of the CPU, after the child process died, the parent process continues its own path, and for the deceased child process to retain the deathbed state-normal death (normal end), accidental illness (abnormal end), Was murdered (killed by signal 9).
Signal is a mechanism of asynchronous communication between processes in Linux. Any process can send a signal. Any process can also accept the signal, and the action after receiving the signal can be the default action, ignoring or specifying the action of three cases. The default actions that correspond to different kinds of signals are often different. to ignore it is not to ignore it. Specifies that the action is to specify a piece of code, when the corresponding signal to execute the code, stop the current action, after execution and then back to the original execution trajectory, this and the interrupt is very similar.
Thread. Threads in Linux are implemented as a simulation process. The difference is that a process space can be shared between threads, and there is more information about the relationship between threads. From a scheduling standpoint, there is no difference between a process and a thread. The thread function called in the program is often a function in the POSIX thread library, and the function in the library calls into the Linux kernel.
3.2 Memory Management
There are two kinds of memory management in Linux, one is physical memory management, the other is virtual memory management. The memory used in the application is generally referred to as virtual memory.
Physical memory management. The Linux kernel manages the physical memory on a page-by-unit. The size of a page is different depending on the CPU. The buddy algorithm is used in the management of physical memory in Linux. The adjacent idle address space is combined with the buddy algorithm to form a larger address space. And sometimes the Linux kernel also needs to use a smaller address space, smaller than a page smaller, at this time, Linux is using the slab algorithm.
Virtual memory management. One of the benefits of virtual memory is that even though the physical memory used by a program is fragmented, the address space is continuously assigned to it from the perspective of the application, which is the virtual address space rather than the physical address space. Each process has its own virtual address space, which is independent of each other, meaning that the virtual address space of the different processes can be duplicated. The benefits of this feature are obvious, and the process can determine its own allocation and utilization of its virtual address space without regard to other processes. The address space of a process is generally divided into sections of code, data segments, heaps, stacks, etc., where the code snippet is write-protected. There is also a paging mechanism in Linux, although the process has a large virtual address space, but not every virtual address will have a physical address corresponding to it. It is only when it is used that it is assigned to a specific physical address, and this mechanism is paging. In general, the use of low-frequency virtual address space, after the use of the physical address they occupy to release, and is mapped to the swap partition, the partition is generally on the hard disk, this operation is based on the page as the basic unit. In addition, the preceding fork, because the child process and the parent process has an almost identical environment, so do not have to assign the child process to separate the physical address space immediately after the fork, but with the parent process to share the same physical address space, only when one of the parties need to write operations to allocate additional physical address space, This mechanism is copy on Write. This is a time-saving and space-saving method.
3.3 File System
In front of Linux, everything can be treated as a file, whether it's data on a hard drive, devices, memory, etc. The kernel does not care about the type of data within the file, but is identified by the application above. Linux allocates an inode for each file, which is a data structure that manages file properties and locates the physical location of the file. All files are organized in a directory that is rooted in/.
To read and write a file, first open it with the system call. If there is no error, the function returns a unique positive integer greater than 2, which is referred to as a file descriptor. Any subsequent operation of the file is done through this file descriptor. (in the case of an exception, when a process has an open file, if a child process is forked, the child process shares the file descriptor with the parent process, that is, the child process can read and write the file directly without open.) 0,1,2 These three file descriptors are pre-defined special file descriptors, which represent standard input, standard output, and standard error output, respectively. Standard input, standard output is typically specified as a terminal, or can be assigned to a file or pipe and other places.
The read and write to the file is generally not directly to the file itself, but in the middle to pass memory. If you want to read a file, read the relevant data of the file into memory, and then read the memory, if you want to modify a file, the memory of the relevant data, the file itself can not be immediately modified, there is usually a delay. Because the CPU accesses memory more quickly than external devices, this strategy can improve the system response speed.
Linux supports many file systems, in addition to the standard EXT2, but also supports VFAT,NFS and so on. Many file systems can be supported because there is a virtual file system (VFS) on a variety of specific file systems. With VFS, the operation of different file systems can be done through the same interface.
3.4 interprocess Communication
Pipeline is the oldest process communication method of UNIX, and the process of communication between parent and child can achieve various complex tasks. Communication between non-parent and child processes can be accomplished through named pipes.
Sockets are also one of the means of interprocess communication, which can be two processes in the same operating system or two very remote processes on different computers. Various communication protocols can be used to communicate with sockets. The socket interface in Linux is implemented in the kernel. The main data structure is called the socket buffer. In order to make the system by the very good response speed, uses the strategy which does not copy the communication data as far as possible. At the time of the trust, the resolution of each layer protocol is realized by pointing to the movement of the pointer receiving data, and by simply appending a protocol header to the header of the data to be sent.
The System V IPC is also a common method of process communication. Includes shared memory, semaphores, and message-to-column.
Futex is an inter-process communication method that Linux2.6 begins to use. It is used for process synchronization. Synchronization of shared resources is achieved by sharing a lock variable. It is characterized by a basic operation in the user space without the need to enter the kernel state.
3.5 Other control management
process scheduling. The Linux kernel provides a multi-tasking environment. You can perform multiple tasks on Linux at the same time, but we know that, in fact, a CPU can only perform one task at a time, but from a macro point of view we do see multiple tasks at the same time. This effect is actually the kernel through the process scheduling to achieve. It's like an acrobat can throw multiple balls at the same time with two hands and keep all the balls in the air. The difference is that the ball in the hands of the acrobat is often the same size and weight, so they go from the acrobat to the air, and the time to return to the hands is almost the same. The tasks that Linux handles tend to be different components, so the CPU time that each task consumes is often unequal. The concept of this component is called priority here, and the concept of priority is added to the process scheduling, which is complex. I'll go over it in detail later.
Interrupts and delays. Interrupts occur primarily in device drivers that control hardware systems and in clocks. When an interrupt occurs, the kernel pauses the processing of the current task and enters the appropriate interrupt handler. After the interrupt handler finishes, the kernel immediately resumes the previous task processing. In order to improve the response of interrupt processing and also alleviate the burden on the whole system due to interrupt processing, Linux uses a method called soft interrupt, which can make some non-urgent interrupt processing delay. This approach is different from the way that traditional UNIX is used for interrupt prioritization.
Linux has its own clock. The clock interrupts that occur at the time of the hardware are captured at certain times. Clock interrupt processing In addition to timing, is also responsible for processing a variety of timing tasks, such as network protocol stack, device drivers and so on. Linux is highly efficient in clock processing. Detailed later.
System calls. System calls are the only means for applications to request services from the kernel. From the application's point of view, system calls are just a few functions. Once a system call occurs, the application's own executing code is paused, and the kernel code is executed into the kernel, which is called the kernel state. After the system call executes, revert to the original execution trajectory of the application to continue, the so-called user state. If the system call fails, it will return an error like any other function. System invocation is a good mechanism to protect the kernel area code and data from being destroyed, because when an application accesses the kernel zone, it must enter the kernel state through the system call, and cannot directly access the kernel area.
Synchronous mutex. In Linux, there are often multiple execution streams, both of which have independent action tasks and environments. Regardless of hardware resources or software resources are limited, so there is inevitably a number of execution flow between how to reconcile the problem. To do this, Linux prepares a number of synchronous and mutex methods. The semaphore can be used when multiple processes request the same resource. In the case of multiple processors, the coordination between multiple CPUs can be mutually exclusive. In addition, there are rcu, sequential locks and other methods available.
Linux Kernel Overview