The kernel module is an external plug-in provided by the Linux kernel. It is called the dynamic LoadableKernelModule (LKM. The Linux Kernel provides a module mechanism because it is a single kernel (monolithickernel ). The biggest advantage of a single kernel is high efficiency, because all the content is integrated, but its disadvantage is the poor scalability and maintainability. The module mechanism aims to make up for this defect. I. What is a module?
The Kernel Module is an external plug-in provided by the Linux Kernel. It is called the dynamic Loadable Kernel Module (LKM. The Linux kernel provides a module mechanism because it is a single kernel ). The biggest advantage of a single kernel is high efficiency, because all the content is integrated, but its disadvantage is the poor scalability and maintainability. The module mechanism aims to make up for this defect.
I. What is a module?
A module is a program with independent functions. It can be compiled but cannot run independently. It is linked to the kernel at runtime and runs in the kernel space as part of the kernel, which is different from the process running in the user space. A module is usually composed of a group of functions and data structures to implement a file system, a driver, or other kernel upper-layer functions.
Comparison between applications and Kernel Modules
To better understand the kernel module, table 1 shows the comparison between applications and kernel module programs.
Table 1 Comparison between applications and kernel module programs
From table 1, we can see that the kernel module program cannot call functions in the libc library. It runs in the kernel space and can only be run by Super Users. In addition, the module program must use the module_init () and module-exit () functions to tell the kernel "I am coming" and "I am going ".
2. Compile a simple module
Both modules and kernels run in the kernel space. In a sense, module programming is kernel programming. Due to each change in the kernel version, some function names also change accordingly. Therefore, module programming is closely related to the kernel version. The following is an example of Ubuntu 9.10 kernel 2.6.31-14-generic.
1Program example
# Include
# Include
# Include
MODULE_LICENSE ("GPL ");
Static int _ init lkp_init (void)
{
Printk (KERN_ALERT "Hello World! \ N ");
Return 0;
}
Static void _ exit lkp_cleanup (void)
{
Printk (KERN_ALERT "Bye World! \ N ");
}
Module_init (lkp_init );
Module_exit (lkp_cleanup );
MODULE_AUTHOR ("heyutao ");
MODULE_DESCRIPTION ("hello ");
Description
All modules must use the header file module. h, which must be included.
The header file kernel. h contains common kernel functions.
The header file init. h contains macro _ init and _ exit, which allow you to release the memory occupied by the kernel.
Lkp_init is the initialization function of the module. It must contain such content as the code to be compiled and the initialized data structure.
The printk () function is defined by the kernel and similar to the printf () function in the C library. It outputs the information to be printed to the terminal or system log.
Lkp_cleanup is the exit and cleanup function of the module. Here we can do all the cleanup work related to terminating the driver.
Module_init () and cleanup_exit () are the two most basic and necessary functions in module programming.
Module_init () is the entry point for driver initialization. Cleanup_exit () cancels all functions provided by the module.