1. Linux Introduction to the kernel
kernel : In computer science, it is used to manage the data sent out by software I/O( Input and output ) The required computer program , Transfer these requests to Translated into data processing instructions and handed over to the central processing unit (CPU) And other electronic components in the computer , Is the most basic of modern operating systems Part of . It is part of the software that provides secure access to computer hardware for many applications , Such visits are limited , And it's up to the kernel to decide When and how long does a program operate on a piece of hardware . Direct operation on hardware is very complicated . So the kernel usually provides a kind of hardware Abstract methods , To do this . Through inter process communication mechanism and system call , The application process can indirectly control the required hardware resources ( special It's the processor and IO equipment ).
2. GNU/Linux Operating system and Linux Kernel relations
What we usually say Linux It's actually the kernel , namely Linux kernel . and Linux The operating system is actually GNU/Linux operating system , That is to use Linux Kernel GNU System .
3.GNU/Linux The basic architecture of the operating system
3.1 User space
At the top is the user ( Or applications ) Space .
This is where the user application is executed . Under user space is kernel space ,Linux This is where the kernel is .GNU C Library (glibc) Here, too . It provides a system call interface to the kernel , It also provides conversion between user space applications and the kernel The mechanism of . This is very important , Because kernel and user space applications use different protected address spaces . Process per user space All use their own virtual address space , The kernel takes up a separate address space .
3.2 Linux The architecture of the kernel
Kernel is the core of operating system , It has many basic functions , It is responsible for managing the process of the system 、 Memory 、 Device driver 、 File and network system , Determines the performance and stability of the system .
Linux The kernel consists of the following parts : memory management 、 Process management 、 file system 、 Device driver and network interface program .
3.2.1 System call interface (System Call Interface abbreviation SCI)
SCI Layer provides mechanisms to perform function calls from user space to the kernel . This interface depends on Architecture , Even in the same place The same is true of Li Qi family . SCI It's actually a very useful function call multiplexing and demultiplexing service .
stay ./linux/kernel You can find SCI The implementation of the , And in ./linux/arch Find architecture dependent parts of .
3.2.2 memory management
For any computer , Its memory and other resources are limited . In order to make the limited physical memory meet the application of memory A large demand for ,Linux It uses a method called “ Virtual memory ” Memory management mode of .Linux Divide memory into manageable “ Memory page ” ( For most architectures 4KB).Linux Includes ways to manage available memory , And physical and virtual mapping The hardware mechanism used . But memory management has more to manage 4KB buffer .Linux Provide for the right to 4KB Abstraction of buffer
for example slab distributor . This memory management mode uses 4KB Buffer as base , And then allocate the structure from it , And track memory page usage condition , For example, which memory pages are full , Which pages are not fully used , Which pages are empty . This allows the pattern to be dynamic according to the needs of the system Adjust memory usage .
To support multiple users using memory , Sometimes the available memory is consumed . For this reason , Pages can be moved out of memory And put it on the disk . This process is called exchange , Because pages will be swapped from memory to hard disk . The source code of memory management can be found in ./linux/mm Find .
3.2.3 Process management
A process is actually a running entity of a specific application . stay Linux In the system , Ability to run multiple processes at the same time ,Linux adopt Running these processes in turn over a short time interval “ multitasking ”. This short time interval is called “ Time slice ”, Let the process take turns The method of doing is called “ Process scheduling ” , The program that completes the schedule is called the scheduler .
Process scheduling control process pair CPU The interview of . When you need to select the next process to run , The scheduler selects the most worthwhile process to run .
A runnable process means that the process status of the process is TASK_RUNNING, This means that the process is runnable ： Either it's running , Either in runqueue Waiting to run .
about 「 User space 」（ Refers to the space in memory where user processes run ） For running processes , It can only be operational .
about 「 Kernel space 」（ The space in memory where the kernel runs and provides services ） For running processes , Operational states are also possible .
One runqueue （ Run the queue ） yes scheduler （ Scheduler ） The basic data structure inside , It holds a list of runnable processes , The processes in these lists are then CPU perform .
scheduler （ Scheduler ） Also known as process scheduler （ Process scheduler ）, It's part of the kernel , It assigns... To different runnable processes CPU Time ,Linux Using a simple priority based process scheduling algorithm to select a new process .
Through multi task mechanism , Each process can be considered to have its own exclusive computer , So as to simplify the programming .
Each process has its own address space , And can only be accessed by this process , In this way, the operating system avoids interference between processes and “ bad ” The program may cause Hazards of .
To accomplish a specific task , Sometimes it is necessary to integrate the functions of two programs , For example, a program output text , Another program sorts the text . So , The operating system also provides interprocess communication mechanisms to help accomplish such tasks .
Linux The common interprocess communication in The mechanism has a signal 、 The Conduit 、 Shared memory 、 Semaphores and sockets, etc .
The kernel passes through SCI Provides an application programming interface (API) To create a new process (fork、exec or Portable Operating System Interface [POSIX] function ), Stop the process (kill、exit), And communicate between them And synchronization (signal perhaps POSIX Mechanism ).
3.2.4 file system
and DOS Different operating systems ,Linux A separate file system in the operating system is not defined by a drive letter or drive name ( Such as A: or C: etc. ) To mark . contrary , and UNIX The operating system is the same ,Linux The operating system combines separate file systems into a single layer The secondary tree structure , And a separate entity represents the file system .s Linux Passing the new file system through a file system called “ Hang up ” or “ Hang up ” To mount it to a directory , So that different documents The system is integrated into a whole .Linux An important feature of the operating system is that it supports many different types of file systems . Linux The most commonly used file system in is Ext2, So is it Linux Native file system . but Linux Can also support FAT、VFAT、FAT32、MINIX And so on , It can exchange data with other operating systems conveniently . because Linux Supports many different file systems , And organize them into a unified virtual file system .
Virtualizing file systems :
Virtual file system (VFS) yes Linux A very useful aspect of the kernel , Because it provides a general interface for the file system like .VFS stay SCI And the file system supported by the kernel . namely VFS Provides a... Between the user and the file system Switch layer .
Virtualized file systems hide the details of various hardware , Separate file system operations from the specific implementation details of different file systems , by All devices provide a unified interface ,VFS Dozens of different file systems available . Virtual file systems can be divided into logical file systems And device drivers . Logical file system means Linux Supported file systems , Such as ext2,fat etc. , Device drivers refer to each type of hardware The device driver module written by the controller .
3.2.5 Device driver
The device driver is Linux Main parts of the kernel . Similar to the rest of the operating system , Device drivers run in a highly privileged processor environment , So that the hardware can be operated directly , But that's why , Any device driver error can lead to To crash the operating system .
Device drivers actually control the interaction between the operating system and hardware devices . Device drivers provide a set of abstract interfaces that the operating system can understand to complete the interaction with the operating system , The specific operation details related to hardware are completed by the device driver become .
generally speaking , Device driver is related to device control chip , for example , If the computer hard disk is SCSI Hard disk , You need to use SCSI The driver , instead of IDE drive Program .
3.2.6 Network interface program
Provides access to various network standards and support for various network hardware . Network interface can be divided into network protocol and network driver . The Internet The protocol part is responsible for implementing every possible network transmission protocol . as everyone knows ,TCP/IP Agreement is Internet Standard agreement for , meanwhile It's also a de facto industrial standard .Linux Network implementation support for BSD Socket , Support all of TCP/IP agreement .Linux The web of the kernel The collateral part is composed of BSD Socket 、 Network protocol layer and network device driver .
The network device driver is responsible for communicating with the hardware device , Every possible hardware device has its own device driver .
Linux Kernel video learning materials +qun720209036 obtain
more Linux Advanced server development includes C/C++,Linux,Nginx,ZeroMQ,MySQL,Redis,MongoDB,ZK, Streaming media ,P2P,K8S,Docker,TCP/IP, coroutines ,DPDK Multiple advanced knowledge points to share . Click the link ：C/C++Linux Server development / Back office architect - Learning video