Basic knowledge of the module

Source: Internet
Author: User
Some basic knowledge of the module-general Linux technology-Linux programming and kernel information. The following is a detailed description. For linux kernels, modules are dispensable. This feature must be successful only when appropriate options are selected during kernel compilation. As far as I know, are all release systems? The kernel is set as the preset option by modularization.

We can even design new modules for the system and load and execute the kernel without re-compiling the kernel or restarting the system.

When a module is loaded into the kernel, it becomes a part of the kernel. Therefore:

Can this module use all the functions in the kernel? ?? Why? What is mechanical advice? Fading?

This module is executed at the highest level of the processor. In the architecture of i386, It is the ring level 0 (ring level 0). In this way, this module can perform various access actions for I/O, and execute commands that cannot be executed by General programs.

The code segments and data segments of a module are directly mapped to the physical memory. That is to say, this module cannot perform paging operations. In this case, paging errors cannot occur during module execution.

As we can see, the dynamic loading module has some real-time system features: the dynamic loading module will avoid the time delay caused by paging errors, and the dynamic loading module, all hardware resources can be accessed.

The module can be written in "C language. Here is an example (to execute the commands mentioned below, you 'd better log on to the system as su and root ):

Example1.c

# Define MODULE
# Include
# Include
Static int output = 1;

Int init_module (void ){
Printk ("Output = % dn", output );
Return 0;
}
Void cleanup_module (void ){
Printk ("Adi, Bye, Chao, Ovuar, n ");
}

Compile example1.c using the following line of parameters:

# Gcc-I/usr/src/linux/include/linux-O2-Wall-d1_kernel _-c example1.c
Option-c requires gcc to stop after the target file is generated? Why? Gray? Too many? Mu? What is the last part of the humerus? If it is a target file, example1.o.

The kernel lacks the standard output function, so we cannot use the printf () function, instead of the printk () function provided by the kernel. Printk () and printf () are almost the same, the only difference? E is the result that printk () sends to the ring buffer of the kernel. This buffer zone is the place where all information is concentrated, as shown in the information displayed at startup, can be found in this ring buffer zone. Can we use the dmseg command to view the ring buffer at any time? Or directly check the/proc/kmsg file.

Note that there is no main () function in this module, but there is an init_module () function without any parameters. Cleanup_module () is the function that must be called before the last module is released. Insmod is used to load modules and then execute modules.

# Insmod example1.o

Now we have installed the example1 module and executed the init_module () function of example1. To view the results, use the following command:

# Dmesg | tail-1
Output = 1

The command lsmod will list all modules currently loaded into the core:

# Lsmod
Module Pages Used:
Example1 1 0
Sb 6 1
Uart401 2 [sb] 1
Sound 16 [sb uart401] 0 (autoclean)

Finally, we use rmmod to release the module:

# Rmmod example1
# Dmesg | tail-2
Output = 1
Adi, Bye, Chao, Orvua,

Dmesg shows that the cleanup_module () function has been executed.

Now we have no idea how to pass the parameter to the module. This method is surprisingly simple. You only need to specify the value? Parameters, and then the insmod will pass the parameters to the module. For example:

# Insmod ejemplo1.o output = 4
# Dmesg | tail-3
Output = 1
Ads, Bye, Chao, Orvua,
Output = 4

Now all the things in the module, big and small, should be known. What are you still doing? Learn To write it!

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.