Introduction to Linux Kernel

There are two types of Linux kernels, stable and under development. The kernel is usually divided into three segments with a dot number, the main version number. Updated version number, for example, 2.26.0. An even number indicates a stable release, and an odd number indicates a development release.

From the kernel

The kernel source code is usually located in /usr/src/linux or /usr/src/kernel. For details, see installing the Linux kernel.

Ls defaults to lexicographical vertical sorting.

Process management

In modern operating systems, processes provide two virtual mechanisms: virtual processors and virtual memory. Threads share virtual memory but have their own virtual processors.

Process descriptors: The kernel places processes in a two-way circular linked list called a task queue. Each item in the linked list is a structure called a process descriptor.

Process family tree: All processes in the system are descendants of the init process with PID 1. The kernel starts the init process at the last stage of boot, which reads the system initialization script and executes related programs.

Process creation: Unix system process creation is unique. Many other systems provide spawn to create a new process. Unix uses fork and exec to execute the target program, which has the same effect as spawn.

Linux fork uses copy on write. When fork is called, the kernel does not immediately copy the entire address space of the process. Instead, it shares the memory space with the parent process, but only at write time. COW is an effective way to delay replication, so this optimization is important.

Implementation of threads in Linux

Linux is unique in the way it implements threads. It does not have a concept of threads. Linux implements all threads as processes, and these threads are simply treated as a process that shares some resources with other processes. The parent process and the Forbidden City are called threads, and the parent process and Forbidden City are called threads.

The implementation of threads is very different in Linux and Windows and Solaris. These systems all provide mechanisms in the kernel to support processes specifically, and these systems often refer to threads as “lightweight processes.” For Linux, threads are processes that know how to share resources between processes, and Linux processes are lightweight enough.

Process scheduling

A basic job of the scheduler is to select one of a set of runnable processes to execute.

Multitasking systems fall into two categories: preemptive and non-preemptive. Modern operating systems use preemption, in which the scheduler decides when to stop a program. The amount of time a process can run before being preempted is a preset time slice. Instead of a preemptive system, the process will continue to execute unless it voluntarily stops running.

Time slice: A time slice is a number, but not a fixed value, indicating how long a process can run before being preempted.

Linux CFS scheduler: Linux the CFS scheduler adopt the strategy of divide the processor time, when only two processes running in the system, such as a text editor and video decoder, they each get 50% processor time, but most of the time, waiting for user input text editor, so after each time the user input in the sleep state. The video processor consumes all the CPU time each time, so CFS finds that the text editor is consuming less than 50% of the promised CPU, so CFS schedules the text editor to run first.

The system calls

There is no

Kernel data structure

Built-in data structures commonly used by the kernel: linked lists, queues, maps, binary trees.

Binary trees include binary search trees, balanced binary search trees (the depth difference between all leaf nodes is less than 1), and self-balanced binary search trees (binary search trees where all operations attempt to maintain equilibrium, such as red-black trees). Red-black tree is the most important binary tree data structure in Linux.

Interrupts and interrupt handling

In most architectures, interrupts simply send a signal via an electrical signal to a specific pin of the processor. The processor immediately stops what it’s doing and jumps to a predefined location in memory and starts executing the code there.

Introduction to Kernel Synchronization

There is no

Kernel synchronization method

Atomic manipulation is the cornerstone of other synchronization methods. Atomic operations ensure that instructions are executed atomically, that is, without interruption.

The kernel provides two sets of atomic operation interfaces. The integer of atomic operation is defined as automic_t, and the operations include automic_set() and automic_add().

Atomicity does not guarantee orderliness, which ensures that even if two or more instructions appear in separate threads of execution, the order in which they should be executed remains the same. Orderliness is achieved through memory barriers.

The most common lock in the Linux kernel is the spin lock, using the spin_lock_irq() method.

Semaphore: Counting semaphore and binary semaphore. A binary semaphore, which allows only one lock holder at any one time, is also a mutex. Counting semaphores allow multiple lock holders and cannot be used to enforce mutual exclusion, as the kernel rarely uses them.

Orderliness and memory barriers: The compiler may reorder reads and writes to improve efficiency. To achieve sequentiality, the CPU has machine instructions to ensure that the order is required, or it can instruct the compiler not to reorder a given sequence of instructions around it. These instructions to ensure order are called barriers.

The RMB () directive provides a “read” memory barrier that ensures that load actions before RMB () are not rearranged after RMB () calls, and load actions after RMB () are not rearranged before RMB ().

Timers and time management

Time management plays a very important role in the kernel. A large number of functions in the kernel are time-driven, such as schedulers and screen refreshes, which need to be executed periodically.

Periodically generated events are driven by the system timer, such as every 10ms, timer is a programmable hardware chip, it can generate interruption at a fixed frequency, the interruption is timer interrupt.

Programs in Linux that take advantage of clock interrupts to work are:

  • Update the system running time
  • Process time slice scheduling

Memory management

The kernel uses the physical page as the basic unit of memory management. From the perspective of virtual memory, a page is the smallest unit. Most 32-bit systems support 4KB pages, while 64-bit systems support 8KB pages.

Virtual file system

Block I/O layer

Block devices and character devices: Character devices are accessed in an ordered manner as a byte stream, whereas block devices are accessed in an unordered, random manner and have a fixed size.

The smallest addressable unit of a block device is the sector, a common sector of 512 bytes. However, blocks are an abstraction of the file system and can only be accessed based on blocks. All disk operations performed by the kernel are based on blocks, which are integer multiples of 2 of the sector and cannot be larger than the page size, so block sizes are typically 512B, 1KB, or 4KB.

Process space address

The memory of a process in user space is called the process address space.

Page caching and page write back

Page caching refers to the caching of physical data on disk into physical memory.

Write caching: What cache strategy is used for writing to disk? There are three strategies

  • No cache: The system skips the cache and writes data to the disk, invalidating the cached data. This strategy is rarely used.
  • Write operations automatically update the memory cache: this is called write through cache.
  • Write back: This is a policy adopted by Linux. Write operations are directly written to the cache without updating the disk. The cache is marked as a dirty page and added to the dirty page list. The pages in the dirty page list are then periodically written to disk by a process (write back process) and the dirty page flag is cleaned up. Dirty pages refer to data on disk, not in memory.

Equipment and Modules

There is no

debugging

Kernel debugging.

portability

Data type and other related portability code

Patches, development, and community

Kernel coding style and how to share kernel code