20179203 "Linux kernel Fundamentals and Analysis" Tenth week work

Source: Internet
Author: User

The 17th Chapter equipment and module one, equipment type 1. Linux and UNIX systems:

Block devices
Character device
Network equipment

2. Block Device:

Usually abbreviated as BLKDEV, it is addressable, in blocks, block sizes vary by device, and block devices typically support relocation operations, which are random access to data.
Block devices are accessed through special files called "Block device Nodes", which are often mounted as file systems.

3. Character Device:

It is usually abbreviated as CDEV, which is not addressable and provides only streaming access to the data, which is a character or byte.
Examples of character devices are keyboards, mice, printers, and most pseudo-devices. A character device is accessed through a special file called a "character device node." Unlike block devices, applications interact with character devices by directly accessing device nodes.

4. Network equipment:

The most common types are sometimes referred to as Ethernet devices, which provide access to networks such as the Internet, through a physical adapter and a specific protocol.
Network devices break the Unix "Everything is a file" design principle, it is not accessed through the device node, but through the socket API such a special interface to access.

5. Pseudo-Device:

Most device drivers represent physical devices, but not all device drivers represent physical devices. Some device drivers are virtual and only provide access to kernel functionality. We call it "pseudo-devices".

Common, such as:

Kernel random number generator (accessed via/dev/random and/dev/urandom)
Empty device (accessed via/dev/null)
0 devices (accessed via/dev/zero)
Full device (accessed via/dev/full)
Memory devices (accessed via/DEV/MEM)

Second, Module 1. Overview:

The Linux kernel is modular, allowing the kernel to dynamically insert or remove code from it at runtime. These codes (including related subroutines, data, function population, and function exits) are combined in a separate binary image, known as loadable kernel modules, or simply modules.
The benefit of the support module is that the basic kernel image can be as small as possible, since optional features and drivers can be provided in the form of modules. The module allows us to easily remove and reload kernel code, as well as facilitate debugging efforts. And when hot-swappable new devices, you can load new drivers with commands.
All initialization functions of the module must conform to the form: int my _ init (void);
The exit function must conform to the form: void My_exit (void);

2. Build and install

Install the compiled module: Make Modules_install

Generates dependency information and updates each time it is started.
Generate kernel dependency information, the root user can run the command: Depmod
Generate dependency information for the new module only, without generating all dependencies, the root user can run the command: depmod-a
The module dependency information is stored in the/lib/modules/version/modules.dep file.

Load, root user can run command: insmod module. Ko

The module is inserted into the kernel via modprobe, and the root user can run the command: zmodprobe module[module parameters] (The parameter module specifies the module name to be loaded).

To unload the module from the kernel, the root user can run the command: MODPROBE-R modules

Three, equipment model
    • 1.kobject:
      The core part of the device model. Where struct kobject is represented, defined in the header file . is usually embedded in other structures.

    • 2.ktype: The
      Kobject object is associated to a special type: Ktype. Represented by the Kobj_type struct, defined in the header file . In order to describe the universal characteristics of a family of kobject.

    • 3.kset:
      in the Linux kernel, there are only a few ktype, but there are multiple kset. Represented by the Kset struct, defined in the header file <linux/kobject.h>:

    • 4.kobject, Ktype, kset relationship:
      Kobject, which has kobject for those structures that contain it of the feature. Ktype defines some kobject-related default attributes. Kset provides two functions: first, where the embedded Kobj is authored as the base class for the Kobject group. Second, kset the associated kobject together.

    • 5. Manage and manipulate Kobject:
      Kobject is initialized with the function koject_init, which is defined in file : void Kobject_init ( struct Kobject kobj, struct kobj_type ktype); The first parameter is the Kobject object that needs to be initialized; Before the initialization function is called, the kobject must be emptied, not emptied, and memset () can be called: memset ( Kobj, 0, sizeof (*kobj)); You should call Kobject_createo to create Koject.

    • 6. Count:
      Kobject's main function: Provides a unified reference counting system. After initialization, the reference count of Kobject is set to 1, the reference count is nonzero, and the object continues to remain in memory. The reference count falls to 0 o'clock, the object can be revoked, and the associated memory is freed. The reference count for Koject is implemented by the Kref struct, which is defined in the header file .

19th. Portability of one, portable operating system

Some operating systems design with portability as one of the top priorities, minimizing the number of machine-related code involved. There is less assembly code, which is to take advantage of code performance optimizations in exchange for code portability. Minix,netbsd and many of the research systems are this highly portable operating system.
There are also some operating systems completely disregard and portability, to the greatest extent possible to pursue the performance of the code, as much as possible to use the assembly code, so that the use of code portability in exchange for code performance optimization capabilities. Such systems are more difficult to maintain than portable systems. DOS and WINDOWS95 are such an operating system.

Second, word length and data type

The Ansic standard stipulates that the length of a char must be 1 bytes; although it is not specified that the length of the int type is 32 bits, it is 32 bits in the architecture currently supported by Linux. The short type is similar, although it is not expressly stated in all currently supported architectures, it is 16-bit. The length of pointers and long should never be assumed, and in the architectures currently supported by Linux, they can vary in 32-bit and 64-bit. For different architectures long is different in length, it should never be assumed that sizeof (int) =sizeof (long). Similarly, do not assume that the pointer and int are equal in length.

Third, this chapter feels

To write a good, concise and suitable kernel code, the following two points should be noted:

    • 1. Encode as far as possible the maximum common factor: assuming anything can happen, any potential constraints may exist. Minimum number of Conventions for encoding: Do not assume that a given kernel feature is available, only minimal architectural functionality is required.

    • 2. There are many issues to be considered when writing portability code: Word length, data type, padding, alignment, byte order, symbol, byte order, page size, and processor load/store sequencing. For the vast majority of kernel developers, the main concern is to ensure that data types are used correctly.

20th Patch, development and community

This chapter focuses on the Linux coding style:

    • Indent-The indentation style is 8 character lengths per indent with tab stops.
    • The case tag of the switch statement--switch statement should be indented to align with the switch declaration, which helps reduce the typographic indentation that is brought by the TAB key of 8 characters.
    • The Space--linux coding style stipulates that spaces are placed around the keyword, and there is no space between the function name and the parenthesis.
    • Curly braces--the kernel chooses the style is the opening parenthesis immediately following the end of the statement, with the statement on the same line. The closing parenthesis is a new line, as the first character of the line.
    • The length of each line of code-as much as possible in the source code to ensure that each line of code is no more than 80 characters in length, because doing so may make the code most suitable for display on standard 80*24 terminals.
    • Naming conventions--The use of camel spelling, Studly caps, or other mixed uppercase and lowercase characters is not allowed in naming.
    • Function--based on experience, the code length of the function should not exceed two screens and local variables should not exceed 10. A function should be functionally single and precise.
    • Note-In general, you should describe what your code does and why you do it, rather than how it is implemented in a specific way.
    • typedef--use typedef with caution and use it only when it is really needed.
    • Use something out of the box--don't work behind closed doors. The kernel itself provides string manipulation functions, compression functions, and a list interface, so please use them.
    • Reduce the use of ifdef--in the source code is not in favor of using the ifdef preprocessor directive in the source code.
    • struct initialization-When the structure is initialized, it must precede its members with a structure identifier.
    • The post-correction--indent is a good tool that can be found in most Linux systems, and it is possible to format the source code as specified.

20179203 "Linux kernel Fundamentals and Analysis" Tenth week work

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.