Design ideas for linux (notes)

Source: Internet
Author: User
Design concepts (notes) that can be used for reference in linux 1. c language embedded structure implementation inheritance (typical Kobject); Encapsulation: packages a type of information through struct to facilitate subsequent extension and upgrade, even if the information has only one simple data, such as kref, the access to struct members should be implemented through functions...
Design concepts (notes) that can be used for reference in linux 1. c language embedded structure implementation inheritance (typical Kobject); Encapsulation: packages a type of information through struct to facilitate subsequent extension and upgrade, even if the information has only one simple data, such as kref, the access to struct members is encapsulated by functions, so that the struct is oriented to functional services and is easy to expand.
Abstract: By embedding a special-purpose struct (including specific functions) in a structure to implement abstract design, we can regard an object as a certain type of object and implement a certain service abstraction, similar to C ++ virtual. For example, in the cpu balancing section, the ready queue is embedded in the balanced structure and processing. when the upper layer receives a balanced notification, sched_class is assigned for specific processing. Another example is scheduling. it processes sched_entity, which can be a process or a process Group (different users ).
2. data structure: implementation of the hash in linux: defines the hash interval (generally not too large), determines the array, on each array item, double-stranded tables are used to store data items with the same hash items, while the ideal hash function is equal probability distribution. Red/Black Tree: An ordered queue is generated through deep priority search (DFS). For example, a queue is generated based on the waiting time. During scheduling, only one on the far left of scheduling is OK. (Time complexity is logarithm) two-way cyclic linked list: bitmap secondary index: 3. copy-to-write replication technology: obtain all resources of the parent object by copying copies, modify the copy as an independent object without affecting the parent object. (Process Structure: task_struct, namespace nsproxy); fork copies task_struct. In fact, there are two identical processes that run on the same address. In linux, the difference between processes and threads is only reflected in the differences in shared resources. each thread has a unique task_struct, and fork and clone have only slight differences.
4. structure-related: macro definition: perform structure-related operations through macro definition to hide specific details and facilitate transplantation. for example, in a struct, the relative position of variables cannot be assumed, the macro-defined method (container_of) should be used for calculation. for example, a different bit of data represents different meanings. small inline functions: provide encapsulation, and the efficiency is fast. 5. reasonable goto structure: although the chaotic use of goto is easy to disrupt the program structure, it is criticized, but the code is quite concise when the goto is used in the following matching method (stack) during initialization: if (A) gotooutA; if (B) gotooutB; if (C) gotooutC ;... outC :... outB :... outA :... 6. Improve efficiency reasonably: when the context of a migration device cannot be restored for a running process, consider merging partial storage and partial recovery block device data read requests, and predicting the read policies above. the best performance pursuit is: 7, four device control methods, on similar issues, consider whether a general template can be provided to support expansion: Device operations, data transmission, according to the particularity of data (control commands), to achieve device control, a few devices, basically abandon the addition of a series of special control functions (different from the conventional file system): not conducive to expansion, not used. Add ioctl, which is a command function, similar to invoke: slightly outdated, the application layer and kernel commands must be modified and matched, so sysfs: The Unified attribute modification method is easy to expand. 8. interrupt handling mechanism: to modify the global state, first save the state to be modified, change it to the new state, and use the new state to restore the original state. (for example, in a drawing-related program, if this method is not used to modify the properties of the drawing device, the running results are often not normal at random and non-reproducible.) place the necessary content in the interrupt handler, put unnecessary processing in the lower half to improve performance. (for example, in the onpaint of a drawing, is it only necessary to contain the drawing? when the frame rate cannot be met, whether it is possible to put unnecessary content outside and double-buffered plotting can actually be seen as a way to put the processing part of the drawing outside the main drawing thread, its main thread is only responsible for copying frame content) 9, object lifetime management: reference counting method who allocates who releases Principle 10, design mode concept: the bio object in the block device driver encapsulates read/write requests. ioctl: draws on the concept of command mode. open in file_operations in VFS: the process follows the sequence of common files and special device files (if it is not my case), it implies the responsibility chain Thoughts. Io read/write request scheduling (separated functions and implementation methods): classifier mode; VFS: provides unified external interfaces for different file systems, it can be used as an intermediary between different types of file systems or an adapter. 11. general-purpose default processing and modification-specific processing techniques for simplified development: most of the standard interfaces provide general processing, for example, all file_operation interfaces have the default processing function. you can define mainstream styles in html. the default processing function is applicable to the entire html display element. you can also modify the appearance and features of individual elements, generally, the common part can be set and processed through the style factory; 12. callback: the callback is an interface that tells a third party in advance that you can receive messages or events. a typical "something callme ", interruption is essentially a matter of time. Application scenarios: architecture design. In C, the callback function pointer is generally defined in the corresponding data structure and assigned a value during initialization, such as the structure defined by file_operation in the driver. Generally, callback at is not limited to the function itself. for example, the browser initially only positions the display of html interface elements and introduces the scripting language to enhance the unsupported capabilities of the browser. it can be seen that the browser provides a callback interface, expand the algorithm processing capabilities that the browser does not have. 13. process/thread/user process/kernel thread: Process and thread: linux does not provide support for individual threads. a thread is also a process, however, when clone is performed with other processes, the sharing flag is set to share some resources (including address space) with other processes, with independent task_struct. the kernel thread and common thread: A common process or thread has an independent virtual address space, and the kernel thread does not have an independent virtual address space, all of which are run in the kernel address space. 14. character device drivers and block device drivers: difference: in terms of architecture, the difference between the two is limited to adding a buffer layer (the request queue management and scheduling needs to be added) to the drive of the block device to improve performance. Contact: provides a unified file operation interface to the upstream (VFS; 15. preemption and interruption: it belongs to the scheduling category and can be generated in the user space or kernel space. it belongs to the kernel preemption and user preemption, and is characterized by the occurrence or end of an event, processes with higher priorities need to run in a quasi-real-time manner; interruptions: interruptions to the current execution are real-time; 16. There are two ways to switch the kernel space of a user space: hardware interruption (neither sleep nor preemption); System Call ( (which is a soft interrupt, which can be sleep or preemptible, must ensure code reentrant) 17. interrupt: apply for the interrupt number (some of the interrupt numbers are fixed, such as the clock, and other devices such as PCI can be dynamically applied, shared usage (with many devices and few interrupt numbers) defines the interrupt processing function, that is, the callback function at registration can be sleep (specifically, the kernel memory allocation function can be sleep ), therefore, you cannot enter a sleep area. When the device is initialized or the device is connected to the driver, make sure that the device has been initialized before the registration is interrupted, should I release the interrupt handler, or even disable disconnection 18? why cannot I block or sleep in the interrupt handler? Because the interrupt handler is not managed by the process scheduler, it cannot be recovered through scheduling after blocking. 19. interrupt handler/lower half handler data collection/data processing are not in the same thread: ensure reliable data receiving; for example, data receiving and computing/Display belong to different threads: ensure that the display is not dead and the response is slow. The aim is to reconcile the two contradictions. 20. the linux operating system runs in two time series: the kernel, as an independent runtime, is executed in sequence. scheduling is only a selection of execution paths during execution, A process is a plug-in used to manage the execution body of the kernel. interrupted execution: triggered by hardware to break the execution sequence. 21. system call: it is the only channel for user programs to access system resources or use the system. it performs security checks to improve reliability. implementation mechanism: soft interrupt (int80); implementation method: system_call (system call number, parameter table) similar to invoke, it belongs to the command mode category 22, soft interrupt, tasklet, work queue soft interrupt, tasklet: it is running in the interrupt context, it is soft interrupt, tasklet is a kind of synchronization, serialization encapsulation, and simplified encapsulation of soft interruptions. it cannot be scheduled or sleep. working queue: it runs in the process context, it is a kernel thread with a process control block; it can be scheduled and sleep;
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: 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.