Skip to content

Kernel (operating system)

本节讨论OS的kernel,本节是对Kernel的概述,参考的是 wikipedia Kernel (operating system)

wikipedia Kernel (operating system)

The kernel is a computer program that is the core of a computer's operating system, with complete control over everything in the system. On most systems, it is one of the first programs loaded on start-up (after the bootloader). It handles the rest of start-up as well as input/output requests from software, translating them into data-processing instructions for the central processing unit. It handles memory and peripherals (外设) like keyboards, monitors, printers, and speakers.

The critical code of the kernel is usually loaded into a separate area of memory, which is protected from access by application programs or other, less critical parts of the operating system. The kernel performs its tasks, such as running processes, managing hardware devices such as the hard disk, and handling interrupts, in this protected kernel space. In contrast, everything a user does is in user space: writing text in a text editor, running programs in a GUI, etc. This separation prevents user data and kernel data from interfering with each other and causing instability and slowness, as well as preventing malfunctioning application programs from crashing the entire operating system.

NOTE:隔离带来安全

The kernel's interface is a low-level abstraction layer. When a process makes requests of the kernel, it is called a system call. Kernel designs differ in how they manage these system calls and resources. A monolithic kernel runs all the operating system instructions in the same address space for speed. A microkernel runs most processes in user space, for modularity.

img

A kernel connects the application software to the hardware of a computer.

Function of Kernel

NOTE: 关于内核的功能,在本篇文章中没有进行详细的分类介绍,所以此处进行省略;在Operating systemKernel章节进行了非常好的介绍,推荐去阅读。

Kernel design decisions

原文本节所描述的是在设计一个kernel的时候需要考虑哪些问题,读者应该对这些问题有些了解,这些问题直接影响了kernel的实现。

Kernel-wide design approaches

原文本节所描述的是实现kernel(本质上kernel是一个software)时,采取怎样的软件架构。目前主流的的架构有两种。原文中还对这两种软件架构背后的philosophy即 separation of mechanism and policy 进行了分析,我读完仍然一头雾水。

While monolithic kernels execute all of their code in the same address space (kernel space), microkernels try to run most of their services in user space, aiming to improve maintainability and modularity of the codebase. Most kernels do not fit exactly into one of these categories, but are rather found in between these two designs. These are called hybrid kernels. More exotic designs such as nanokernels and exokernels are available, but are seldom used for production systems. The Xenhypervisor, for example, is an exokernel.

Monolithic kernels

Main article: Monolithic kernel

NOTE: 由于Linux 采用的是 monolithic kernel,所以需要对它进行详细分析,后面补充了“Monolithic kernel”章节。

img

Diagram of a monolithic kernel

In a monolithic kernel, all OS services run along with the main kernel thread, thus also residing in the same memory area. This approach provides rich and powerful hardware access. Some developers, such as UNIXdeveloper Ken Thompson, maintain that it is "easier to implement a monolithic kernel" than microkernels. The main disadvantages of monolithic kernels are the dependencies between system components – a bug in a device driver might crash the entire system – and the fact that large kernels can become very difficult to maintain.

NOTE: Linux就是典型的monolithic kernel

Microkernels

Main article: Microkernel

img

Microkernel (also abbreviated μK or uK) is the term describing an approach to operating system design by which the functionality of the system is moved out of the traditional "kernel", into a set of "servers" that communicate through a "minimal" kernel, leaving as little as possible in "system space" and as much as possible in "user space". A microkernel that is designed for a specific platform or device is only ever going to have what it needs to operate. The microkernel approach consists of defining a simple abstraction over the hardware, with a set of primitives or system calls to implement minimal OS services such as memory management, multitasking, and inter-process communication. Other services, including those normally provided by the kernel, such as networking, are implemented in user-space programs, referred to as servers. Microkernels are easier to maintain than monolithic kernels, but the large number of system calls and context switches might slow down the system because they typically generate more overhead than plain function calls.

Monolithic kernel

“monolithic”的意思是 集成的、一体化的,monolithic kernel的意思是“集成核”、“一体化核”,Linux采用的就是这种结构。

wikipedia Monolithic kernel

A monolithic kernel is an operating system architecture where the entire operating system is working in kernel space. The monolithic model differs from other operating system architectures (such as the microkernel architecture)[1][2] in that it alone defines a high-level virtual interface over computer hardware. A set of primitives or system calls implement all operating system services such as process management, concurrency, and memory management. Device drivers can be added to the kernel as modules.

img

Structure of monolithic kernel,microkernel and hybrid kernel-based operating systems

Loadable modules

NOTE: 这是一种Plugin architecture。

Modular operating systems such as OS-9 and most modern monolithic operating systems such as OpenVMS, Linux, BSD, SunOS, AIX, and MULTICS can dynamically load (and unload) executable modules at runtime.

This modularity of the operating system is at the binary (image) level and not at the architecture level. Modular monolithic operating systems are not to be confused with the architectural level of modularity inherent in server-client operating systems (and its derivatives sometimes marketed as hybrid kernel) which use microkernels and servers (not to be mistaken for modules or daemons).