Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of designing and implementing Linux kernel?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge about the design and implementation of the Linux kernel. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

The root cause of the strength of Unix:

Unix is concise, provides hundreds of system calls, and the design purpose is clear.

Everything in Unix is treated as a file

The Unix kernel and related system tools are developed in C language and have strong portability.

The Unix process is created quickly and has a unique fork mechanism

Unix provides simple and stable meta-language for interprocess communication.

Linux is a Unix-like system, which draws lessons from Unix to design and implement API of Unix.

Applications usually call library functions (such as C library functions) and then let the kernel complete various tasks through the system call interface.

Linux supports dynamic loading of kernel modules

Linux supports symmetric multiprocessing (SMP) mechanism

Linux is a preemptive kernel.

Linux does not distinguish between threads and other general processes

Linux provides an object-oriented device model with device classes, hot-plug events, and a device file system for user controls.

Interrupts and interrupt handling

Interrupt is a solution to the difference between processor and speed, sending a signal to the kernel only when the hardware is needed. Interruption is essentially a special electrical signal.

The kernel responds to a specific interrupt, and then the kernel calls a specific interrupt handler, which is part of the device driver.

Terminal handlers in Linux are not reentrant, and the same interrupt handler will not be called at the same time.

The interrupt context cannot sleep (I understand that the current interrupted program needs to continue execution after the end of the interrupt processing)

Interrupt handlers are not in the process context and they cannot block

The interrupt processing is divided into two parts, the upper part is the interrupt handler, which requires execution as quickly as possible, and the lower part (used to reduce the workload of the interrupt handler) performs work that is closely related to the interrupt processing but is not performed by the interrupt handler itself.

The implementation methods of the lower part: soft interrupt, tasklet, work queue

The implementation of the interrupt mechanism: set to generate an interrupt, send a signal to the specific pin of the processor through the electrical signal, and the processor listens to the current processing work, shuts down the interrupt system, and then transfers it to a predefined location in memory (the entry point of the interrupt handler) to start execution. Calculating the terminal number, do_IRQ () responds to the received interrupt and forbids the transmission of the interrupt on this line.

Kernel synchronization

For shared resources, if they are accessed and operated by multiple threads at the same time, the shared data may be covered by each thread, resulting in inconsistent access data.

The synchronization implementation locks the shared resource through the main locking mechanism, only the thread holding the lock can operate the shared resource, and other threads sleep (or poll). After the resource operation is completed, the thread holding the lock releases the lock, and the waiting thread grabs the lock.

Kernel synchronization method:

Atomic operation

Spin lock, the characteristic is that when the thread cannot acquire the lock, it will always be busy (busy, etc.) waiting for the lock to be re-available, which is suitable for short-term lightweight locking.

Read / write spin lock (shared / exclusive lock), one or more tasks can hold the reader lock concurrently, and the writer lock can only be held by one write task.

Semaphores (sleep locks). If a task tries to obtain an occupied credit, the semaphore pushes it into a waiting queue and then puts it to sleep. When the semaphore is available, the task in the waiting queue will be awakened. It is suitable for long-term occupation of the lock.

Mutex (semaphore with a count of 1), which is the most common in programming.

Sequence lock

Barriers, which is used to ensure the execution order of instruction sequence and read and write

Reasons for concurrency in the kernel:

Interrupts can occur asynchronously at almost any time, and may interrupt the currently executing code at any time

With soft interrupts and tasklet, the kernel can wake up or schedule soft interrupts or tasklet at any time, interrupting currently executing code

Kernel preemption

Sleep and synchronization with user space

Symmetrical multiprocessing, where multiple processors execute code simultaneously

Memory management

The kernel takes physical pages as the basic unit of memory management. Memory management units (MMU, which manage memory and convert virtual addresses into physical addresses) usually manage page tables in the system in pages.

The kernel also divides the zone into different zones, which are used to group pages with similar characteristics.

/ / this function allocates 2 consecutive `physical pages'to the order power, and returns a pointer to the page structure staticinlinestructpage * alloc_pages (gfp_tgfp_mask,unsignedintorder) / / release physical page externvoidfree_pages (unsignedlongaddr,unsignedintorder) of * * pages. / / allocate a block of kernel memory in bytes (physically contiguous) static__always_inlinevoid*kmalloc (size_tsize,gfp_tflags) / / release kmalloc allocated memory block voidkfree (constvoid*)

Virtual file system

Virtual file system provides user control programs with file and file system-related interfaces.

The metadata of the file, stored in a separate data structure, is called inode (Inode).

Virtual file system (VFS) has four main object models:

A super block object that represents a specific mounted file system and stores information about a specific file system

An Inode object that represents a specific file that contains all the information the kernel needs to manipulate a file or directory, and an Inode represents a file in the file system

The directory item object, which represents a directory item, is a part of the path. VFS treats the directory as a file, and the directory item object has no corresponding disk data structure.

File object, which represents the file opened by the process, and the process processes the file directly

/ / the data structure of the file object: structfile {union {structllist_node fu_llist; structrcu_head fu_rcuhead;} favou; structpath favopath; structinode * fanciinodemarmand * cached value * / conststructfile_operations * fatomopy; / * Protects f_ep_links, f_flags. * Must not be taken from IRQ context. * / spinlock_tf_lock; atomic_long_tf_count; unsignedintf_flags; fmode_tf_mode; structmutex flocklock; loff_tf_pos; structfown_struct frankowner; conststructcred * fissur credential; structfile_ra_state Ferrari; U64 fdistributive version; # ifdefCONFIG_SECURITY void*f_security; # endif / * needed for tty driver, and maybe others * / void*private_data; # ifdefCONFIG_EPOLL / * Used by fs/eventpoll.c to link all the hooks to this file * / structlist_head f_ep_links Structlist_head _ attribute__ ((aligned (4); # endif/* # ifdef CONFIG_EPOLL * / structaddress_space * fmapping; / * lest something weird decides that 2 is OK * /

Block IPUBO layer

The hardware devices in the system that can randomly access fixed-size data slices (chunks) are called block devices, such as hard drives. Hardware devices that are accessed sequentially according to the character stream are called character devices, such as keyboards.

# the basic container of the Icano device is represented by the bio structure

The Icano scheduler is used to manage the request queue of block devices, determine the order of requests in the queue and when to send requests to the hanging device. This helps to reduce the addressing time of the disk, thus improving the global throughput

The actual scheduling programs used by linux include linux elevator, deadline scheduling, predictive scheduling, and null operation scheduler.

Process address space

The kernel needs to manage the memory of processes in user space, which is called process address space, and all processes in the system share memory in a virtual way.

Process address space consists of process addressable virtual memory, each process has 32-bit or 64-bit address space.

Virtual address space, the legitimate address space that can be accessed is called the memory area:

Memory mapping of executable file code, called code snippets

Memory mapping of initialized global variables of an executable file, called a data segment

Memory mapping containing zero pages of uninitialized global variables, bss (block started by symbol) segments

Zero-page memory mapping for the process user space stack

The code segments, data segments, and bss of each shared library, such as C libraries or dynamic linkers, are loaded into the address space of the process.

Any memory-mapped file

Any shared memory segment

Any anonymous memory mapping, such as memory allocated by malloc

The kernel uses the memory descriptor structure to represent the address space of the process, and the memory descriptor is represented by the mm_struct () structure. Kernel threads have no process address space, no associated memory descriptors, and all kernel threads have no user context

The object operated by the application is the virtual memory mapped to the physical memory, while the processor operates the physical memory. Linux uses a three-level page table to complete address translation, each virtual address points to the page table as an index, and the page table item points to the page table at the next level. A cache that maps a virtual address to a physical address through TLB (translate lookaside buffer) in a multi-level page table

These are all the contents of the article "what is the method of designing and implementing the Linux kernel?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report