Linux Modules: Introduction: The Linux version 2.0 is also supported by modularization, because some of the kernels often stay in memory, (such as the most commonly used process, such as Scheduler, etc.), but other processes are only required when needed Into. If the MS-DOS file system is only required when the MOUNT is used, this on-demand is loaded, which is the module. It allows the kernel to maintain a small volume, although all stuff can be placed in the kernel, so we don't need modules, but this is usually special use (all the processes to run in advance Case). Another advantage of the module is that the kernel can be loaded, uninstalling them (or automatically completed by the Kerneld daemon).
Writing, Installing, And Removing Modules
Writing Modules: In addition to they run in the kernel space, the module is similar to other programs. So, you must define Module and include header file module.h and other kernel header files. The module can be very simple or complicated.
The general module format is as follows:
#define module # include
/ * ... Module Declarations and functions ... * /
INT init_module () {/ * code kernel will call when installing module * /}
voidcleanup_module () {/ * code kernel will call when remove module * /}
The module using the kernel source is compiled with GCC, and the following options must be used: -i / usr / src / linux / incdude
Note that the variables in all kernels are exported for modules, even if the code is declared in the code. The exported symbol is displayed in the / proc / ksyms file or program ksyms. The current Linux kernel uses the export_symbol (x) macro not only exports symbols, but also the version number (Version Number). If it is a variable defined by the user, uses the export_symbol_novers (x) macro. Otherwise, Linker will not keep this variable in the Kernel Symbol Table. You can use the export_no_symbols macro to do not export variables, the default is that the modules export all variables.
Installing and removing modules: Must be superusers. Use INSMOD to install a module: / sbin / insmod module_name rmmod command Removes a module that has been installed and the reference exported by any of this module. / Sbin / rmmod module_name lists all installed modules using LSMOD.
EXAMPLE: SIMPLE_MODULE.C
/ * Simple_module.c * * This program provides an example of how to install a trivial module * into the Linux kernel. All the module does is put a message into * the log file when it is installed and removed. * * /
#define MODULE # include
/ * clenup_module * the kernel calls this function when it removes the module * / void cleanup_moudle () {printk ("<1> the Simple module is now uninstalled./n");} / * cleanup_module * /
This is the makefile:
# Makefile for Simple_Module
CC = gcc -i / usr / src / linux / include / configcflags = -O2 -D__kernel__ -wall
Simple_module.o: Simple_Module.c
Install: / sbin / insmod simple_module
REMOVE: / SBIN / RMMOD SIMPLE_MODULE