(Study notes - system structure) Linux kernel and windows kernel

kernel

A computer is composed of various external hardware devices, such as memory, CPU, hard disk, etc. If each application needs to communicate with these hardware devices, it will be too tiring, so the kernel is responsible for this middleman, and the kernel acts as the middleman . The application is a bridge connecting hardware devices . The application only cares about interacting with the kernel, and does not care about the details of the hardware.

 

 Modern operating system kernels generally provide four basic capabilities:

  • Manage processes and threads, decide which process and thread use CPU, that is, process scheduling capability
  • Manage memory, determine the allocation and recovery of memory, that is, the ability of memory management
  • Manage hardware devices and provide communication capabilities between processes and hardware devices, that is, hardware communication capabilities
  • Provide system calls. If the application program wants to run services with higher privileges, it needs to be called by the system, which is the interface between the user program and the operating system.

How does the kernel work?

The kernel has very high authority and can control hardware such as cpu, memory, and hard disk, while the application program has very little authority, so most operating systems divide the memory into two areas:

  • Kernel space, this memory space can only be accessed by kernel programs
  • User space, this memory space is dedicated to applications

Code in user space can only access a local memory space, while code in kernel space can access all memory spaces. Therefore, when a program uses user space , we often say that the program is executed in user mode , and when the program uses kernel space , the program is executed in kernel mode .

If the application needs to enter the kernel space, it needs to pass the system call, the system call process:

The kernel program is executed in the kernel state, and the user program is executed in the user state. When an application uses a system call, an interrupt is generated. After an interrupt occurs, the CPU will interrupt the currently executing user program and jump to the interrupt handler, that is, start executing the kernel program. After the kernel processing is completed, it will actively trigger an interrupt, return the CPU execution authority to the user program, and return to the user state to continue working.


Design of Linux

The Linux kernel design concept mainly has these points:

  • MultiTask, multitasking
  • SMP, symmetric multiprocessing
  • ELF, Executable Linking Format
  • Monolithic Kernel, macro kernel

MultiTask

MultiTask means multitasking , which means that Linux is a multitasking operating system

Multitasking means that multiple tasks can be executed at the same time, here can be concurrent or parallel:

  • For a single-core CPU, each task can be executed for a short period of time, and another task is switched when the time is up. From a macro perspective, multiple tasks are executed within a period of time, which is called concurrency.
  • For multi-core CPUs, multiple tasks can be executed by CPUs with different cores at the same time, which is called parallelism.

SMP

SMP means symmetric multiprocessing , which means that each CPU has the same status and the same permission to use resources. Multiple CPUs share the same memory, and each CPU can access complete memory and hardware resources.

This feature determines that the Linux operating system will not have a single CPU to serve application programs or kernel programs, but each program can be assigned to any CPU for execution.

ELF

ELF means Executable Link Format , which is the storage format of executable files in the Linux operating system. The structure is as follows:

ELF divides the file into segments, and each segment has its own role. In addition, the ELF file has two indexes, and the Program header table records the first address of each segment in the binary file.

How are ELF files generated?

The code we write is first compiled into assembly code by [compiler], then converted into object code by [assembler], that is, object files, and finally multiple object files and various function libraries called by [linker] Link them to form an executable file, which is an ELF file.

How are ELF files executed?

When executing the ELF file, the ELF file will be loaded into the memory through the [loader], and the CPU reads the instructions and data in the memory, so the program is executed.

Monolithic Kernel

Monolithic Kernel means the macro kernel, and the Linux kernel architecture is the macro kernel, which means that the Linux kernel is a complete executable program with the highest authority.

The feature of the macro kernel is that all modules of the system kernel, such as process scheduling, memory management, file system, device drivers, etc., run in the kernel mode.

However, Linux also implements the function of dynamically loading kernel modules. For example, most device drivers exist in the form of loadable modules, which are decoupled from other kernel modules, making driver development and driver loading more convenient and flexible.

The opposite of the macrokernel is the microkernel . The kernel of the microkernel architecture only retains the most basic capabilities, such as process scheduling, virtual memory, interrupts, etc., and puts some applications in user space, such as drivers and file systems. In this way, services are isolated from each other, and if a single service fails or is completely attacked, it will not cause the entire operating system to hang up, which improves the stability and reliability of the operating system. 

The microkernel has few functions and high portability. Compared with the macrokernel, one disadvantage is that because the driver is not in the kernel, and the driver generally calls the underlying capabilities frequently, the interaction between the driver and hardware devices requires Frequent switching to kernel mode will cause performance loss. The core architecture of the Hongmeng system is the microkernel.

There is also a type of kernel called a hybrid kernel . Its architecture is a bit like a microkernel. There will be a corner version of the kernel inside the kernel, and then other modules will be built on this basis. When implemented, it will be similar to the macro kernel. To form a complete program, most of the services are in the kernel, which is like wrapping a microkernel in the way of a macrokernel.


windows design

The Windows kernel, also known as the NT kernel, is the internal core of the Windows system. Each Windows distribution has a kernel version number, also known as the NT version number, which has been named since the Windows NT 3.1 era. The full name of NT is called New Technology.

 Like Linux, Windows also supports MultiTask and SMP, but the difference is that the kernel design of Windows is a hybrid kernel . There is a MicroKernel module in the kernel in the above figure. This is the smallest version of the kernel. The entire kernel implementation is a complete version. Contains a lot of modules.

The executable file format of Windows is also different from that of Linux, so the executable files of these two systems cannot run on each other. The executable file format of Windows is called PE, called portable executable file, and the extension is usually .exe , .dll , .sys , etc. The structure of PE is as follows:


Summarize

 There are generally three types of kernel architectures:

  • Macro kernel, including multiple modules, the whole kernel is like a complete program
  • Microkernel, which has a minimal version of the kernel, and some modules and services are managed by userland
  • The hybrid kernel is a combination of macrokernel and microkernel. The concept of microkernel is abstracted in the kernel, that is, there will be a small kernel in the kernel, and other modules are built on this basis. The whole kernel is a complete program. .

The Linux kernel design uses a macro kernel, while the Windows kernel design uses a hybrid kernel.

The executable file format of the two operating systems is also different, the Linux executable file format is called ELF, and the Windows executable file format is called PE.

Guess you like

Origin blog.csdn.net/qq_48626761/article/details/131956213