Linux Kernel (1)

Source: Internet
Author: User

The kernel is the core of an operating system. It manages system processes, memory, device drivers, files, and network systems, and determines the system performance and stability. Is a system software that provides hardware abstraction layer, disk and file system control, multitasking, and other functions. A kernel is not a complete operating system. The core of Linux, usually in the root directory, is a file named vmlinuz. We use this file to control our entire PC, including peripheral devices, hard and soft hosts, CD-Rom, and sound card. Simply put, the core is the operating system itself.

From the programmer's point of view, the operating system kernel provides an extended or virtual computing platform equivalent to the computer hardware. It abstracts a lot of hardware details. A program can process data in a unified way, while a programmer can avoid many hardware details. From another perspective, a common user regards the operating system as a resource manager. With its help, users can organize their own data in an easy-to-understand manner, complete your work and share resources with others.

Linux supports multiple tasks in a unified manner, which is transparent to user processes. Each process runs as if only one of them runs on a computer, the memory and other hardware resources are exclusive. In fact, the kernel runs several processes concurrently and allows several processes to use hardware resources fairly and reasonably, it also allows processes to run securely without interfering with each other.

 

The Linux operating system consists of four parts:

1. user process-a user application is a large collection of software running at the highest level of the Linux operating system. When a user program runs on the operating system, it becomes a process in the operating system.

2. System Call interface-in an application, you can call a specific process in the operating system kernel to implement a specific service. For example, if you schedule a system call to create a process in a program, the operating system kernel creates a new process for it.

A system call is also a process composed of several commands. But it is different from the general process. The main difference is that the system call is run in the kernel state (or system state), while the general process is run in the user State. In Linux, system calls are part of kernel code.

3. Linux kernel is the soul of the operating system. It manages files and memory on disks, starts and runs programs, and receives and sends packets from the network. In short, the kernel is actually an interface between Abstract resource operations and specific hardware operation details.

4. Hardware-this subsystem includes all possible physical devices required for Linux installation. Such as CPU, memory, hard disk, and network hardware.

The preceding division divides the entire Linux operating system into four levels. The reason why user processes are also included in the operating system is that the operation of user processes is closely related to the operating system, and the system call interface can be said to be an extension of the operating system kernel, hardware is the material condition on which the operating system kernel depends. The dependency between the four layers is shown as the upper-layer dependency layer.

The embedded Linux system consists of three parts: bootloader, zimage, and file system. From the operating system perspective, the final goal of bootloader is to boot and load the kernel image, which can be divided into stage1, stage2, stage1 initializes the basic hardware, prepares the memory space for stage2, copies stage2 to the memory space, sets the stack pointer, and jumps to stage2; initialize the hardware devices used in this phase in stage2, check the memory ing of the system, load the kernel image and the root file system, set kernel startup parameters, and call the kernel.

Linux consists of a user space and a kernel space, at the top of which is a user (or application) space. This is where your application runs. The user space is the kernel space, and the Linux kernel is located here.

Gnu c library (glibc) is also here. It provides a system call interface to connect to the kernel, and a conversion mechanism between the user space application and the kernel. This is very important because the kernel and user space applications use different protective address spaces. Each user space process uses its own virtual address space, while the kernel occupies a separate address space.

The Linux kernel can be further divided into three layers. At the top is the system call interface, which implements some basic functions, suchreadAndwrite. The kernel code under the System Call interface can be more accurately defined as the kernel code independent of the architecture. These codes are common to all the processor architectures supported by Linux. The code that relies on the architecture forms part of the BSP (board support package. These codes are used as processors and platform-specific code for a given architecture.

The Linux kernel implements many important architecture attributes. At a high or low level, the kernel is divided into multiple subsystems. Linux can also be seen as a whole because it will integrate all these basic services into the kernel. This is different from the microkernel architecture, which provides some basic services, such as communication, I/O, memory and process management, more specific services are inserted into the microkernel layer.

Generally, kernel space and user space are two different states of program execution. Arm implements seven working modes. It runs in different modes and has different permissions, that is, the access registers are also different. Dividing user space and kernel space helps to protect the kernel. When some applications or applications run incorrectly in the user space, the kernel space will not be modified, if the kernel space is modified, the system may be damaged.

Transfers the kernel space to the user space through system calls and hardware interruptions. The Linux kernel framework consists of seven parts: System Calling Interface, virtual file system, process management, memory management, device driver, architecture, and network protocol stack.

System Call Interface

The Sci layer provides some mechanisms for executing function calls from user space to the kernel. This interface depends on the architecture. Sci is actually a very useful function call multiplexing and multi-path decomposition service.

Process Management

Process Management focuses on process execution. In the kernel, these processes are called threads and represent independent processor Virtualization (thread code, Data, stacks, and CPU registers ). In user space, the term process is usually used. However, the Linux implementation does not differentiate between processes and threads. The kernel uses SCI to provide an application programming interface (API) to create a new process (fork, exec, or portable operating system interface [POSIX] function ), stop processes (kill and exit) and communicate and synchronize between them (signal or POSIX mechanism ).

Process Management also includes the need to process CPU sharing between active processes. The kernel implements a new scheduling algorithm. No matter how many threads compete for the CPU, this algorithm can be operated within a fixed period of time. This algorithm is called the O (1) scheduler, and the name indicates that the time used to schedule multiple threads is the same as the time used to schedule one thread. O (1) scheduler can also support multi-processor (called Symmetric multi-processor or SMP ).

Memory Management

Another important resource managed by the kernel is memory. To improve efficiency, if the virtual memory is managed by hardware, the memory is managed in the so-called Memory Page mode (4 kb for most architectures ). Linux includes the management of available memory and the hardware mechanism used by physical and virtual ing.

Linux provides an abstraction of a 4 KB buffer, such as an slab distributor. This memory management mode uses a 4 KB buffer as the base, then allocates the structure from it, and tracks the Memory Page usage, such as which memory pages are full and which pages are not fully used, which pages are empty. In this way, the memory usage can be dynamically adjusted according to the system requirements.

To support memory usage by multiple users, the available memory is sometimes consumed. For this reason, the page can be removed from the memory and placed into the disk. This process is called swap because the page will be switched from memory to hard disk.

Virtual File System

Virtual File System (VFS) is a very useful aspect of the Linux kernel, because it provides a general interface abstraction for the file system. VFS provides an exchange layer between sci and the file system supported by the kernel. In VFS, it is a general API abstraction for functions such as open, close, read, and write. Under VFS is the file system abstraction, which defines the implementation method of upper-layer functions. The file system layer is a buffer cache, which provides a common function set for the file system layer (not related to a specific file system ). This cache layer optimizes access to physical devices by retaining data for a period of time (or by pre-reading the data to be available as needed. The buffer cache is a device driver that implements interfaces for specific physical devices.

Network stack

The network stack is designed to follow the layered architecture of the simulation protocol. Internet Protocol (IP) is the core network layer protocol under the transmission protocol (usually known as the transmission control protocol or TCP. TCP is the socket layer, which is called through Sci.

The socket layer is the standard API of the network subsystem. It provides a user interface for various network protocols. From accessing the original frame to the IP Protocol Data Unit (PDU), to TCP and user datamprotocol (UDP), the socket layer provides a standardized method to manage connections, move data between endpoints.

Device Driver

The Linux kernel contains a large amount of code in the device drivers that can run specific hardware devices. The Linux source code tree provides a sub-directory of the driver, which is further divided into various supporting devices, such as Bluetooth, I2C, and serial.

Code that depends on the Architecture

Although Linux is largely independent from the operating architecture, some elements must consider the architecture to operate normally and achieve higher efficiency. The kernel source code depends on the architecture and contains a variety of architecture-specific sub-directories (BSP ). For a typical desktop system, the i386 directory is used. Each architecture subdirectory contains many other subdirectories. Each subdirectory focuses on a specific aspect of the kernel, such as boot, kernel, and memory management.

 

All subsystems in the kernel depend on some common resources. These resources include the processes used by all subsystems. For example, the process of allocating and releasing memory space, printing warnings or error messages, and debugging routines of the system.

System Data Structure

Task_struct

The Linux kernel uses a data structure (task_struct) to represent a process. The data structure pointer of the process forms a task array (in Linux, tasks and processes are in the same terminology ), this type of pointer array is also called a pointer vector. The size of this array is determined by nr_tasks, indicating the maximum number of processes that can run simultaneously in Linux. When a new process is created, Linux assigns a task_struct structure to the new process and stores the pointer in the task array. The scheduler maintains a current pointer pointing to the currently running process.

Mm_struct

The virtual memory of each process is represented by a mm_struct structure, which actually contains information about the currently executed image and contains a set of pointers pointing to the vm_area_struct structure, the vm_area_struct Structure describes a region of the virtual memory.

Inode

Files and directories in the Virtual File System (VFS) are represented by corresponding index nodes (inode. The content of each VFS index node is provided by the file system-specific routine. The VFS index node only exists in the kernel memory and is actually saved in the cache of the VFS index node. If the two processes are opened with the same process, the inode data structure can be shared. The data block in the two processes points to the same inode.

 

Linux kernel source code structure

The Linux kernel source code is located in the/usr/src/Linux directory.

/Arch: the directory contains all the core Code related to the architecture. Each subdirectory under it represents a Linux-supported architecture. For example, i386 is the subdirectory of the Intel cpu and its compatible architecture. Generally, PCs are based on this directory.

/Block: block Device Driver I/O scheduling.

/Crypto: Common encryption and hash algorithms (such as AES and Sha), as well as some compression and CRC verification algorithms.

/Drivers: the directory contains all the device drivers in the system. It is further divided into several types of device drivers, each with corresponding sub-directories, such as the driver of the Sound Card corresponding to/Drivers/sound.

The/documentation directory contains some documents, which are specific descriptions of the role of each directory.

/Fs: Directory stores the file system code supported by Linux and various types of file operation code. Each subdirectory supports a file system. For example, the ext3 file system corresponds to the ext3 subdirectory.

/Include: the directory contains most of the header files required for compiling the core. For example, if the header files related to the platform are in the/include/Linux subdirectory, header files related to Intel CPU are in the/include/asm-i386 subdirectory, while the/include/SCSI directory is the header file directory for the SCSI device.

/Init: the directory contains the core initialization Code (not the System Boot Code), including the main. C and version. c files.

/IPC: the directory contains the communication code between core processes.

/Kernel: Core code of kernel management. The files in this directory implement kernel functions of most Linux systems. The most important files are sched. c. At the same time, the Code related to the processor structure is stored in the/ARCH/*/kernel directory.

/Lib: the directory contains the core library code, but the library Code related to the processor structure is stored in the/ARCH/*/lib/directory.

/MM: The directory contains all memory management code independent of the CPU architecture, such as page-based storage management memory allocation and release. The Memory Management Code related to the specific hardware architecture is located in the/ARCH/*/mm directory, for example,/ARCH/i386/MM/fault. C.

/Modules: the directory stores the created modules that can be dynamically loaded.

/NET: the directory contains the core network code and implements various common network protocols. Each subdirectory corresponds to one aspect of the network.

/Scripts: the directory contains the script files used to configure the core.

/Security: mainly includes the SELinux module.

/Sound: the core driver code of ALSA, OSS audio device, and common device drivers.

/Usr: Implements cpio for packaging and compression.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.