First, you need to build the source code tree of the system kernel, because the module is a kernel programming, and the C language of the user space becomes different. It depends on the existence of the kernel source code tree. A mechanism of the kernel module allows the Kernel File (zImage or bzImage) to not contain a certain component, but when the component needs to be used, dynamic addition to the running kernel Linux provides a mechanism called & ldquo; kernel module & rdquo; to achieve the above effect. Features & bull; modules are not compiled
First, you need to build the source code tree of the system kernel, because the module is a kernel programming, and the C language of the user space becomes different. It depends on the existence of the kernel source code tree.
Kernel module
A mechanism allows the Kernel File (zImage or bzImage) to not contain a certain component, but when the component needs to be used, dynamic addition to the running kernel Linux provides a mechanism called "kernel module" to achieve the above results.
Features
• The module itself is not compiled into the Kernel File (zImage or bzImage)
• Dynamic installation or uninstallation during kernel running as needed.
Step 1: Create source code: hello. c
Root @ Ubuntu:/home/Mydocument/Modules # vi hello. c
# Include
# Include
MODULE_LICENSE ("Dual BSD/GPL ");
MODULE_AUTHOR ("fudan_abc ");
Static int _ init hello_init (void)
{
Printk (KERN_ALERT "Hello, world! \ N ");
Return 0;
}
Static void _ exit hello_exit (void)
{
Printk (KERN_ALERT "Goodbye, cruel workd! \ N ");
}
Module_init (hello_init );
Module_exit (hello_exit );
Step 2: Create Makefile
Root @ ubuntu:/home/Mydocument/Modules # vi Makeile
Obj-m + = hello. o
All:
Make-C/usr/src/linux-headers-$ (shell uname-r) M = $ (shell pwd) modules
Clean:
Make-C/usr/src/linux-headers-$ (shell uname-r) M = $ (shell pwd) clean
Step 3: Compile
Root @ ubuntu:/home/Mydocument/Modules # make
Root @ ubuntu:/home/Mydocument/Modules # ls
Hello. c hello. mod. o Module. symvers hello. ko
Hello. o Makefile hello. mod. c modules. order
Step 4: Install the module
Root @ ubuntu:/home/Mydocument/Modules # insmod hello. ko
Root @ ubuntu:/home/Mydocument/Modules # lsmod
Module Size Used
Hello 12393 0
Rfcomm 38408 0
Bnetp 17923 2
Bluetooth 148839 10 rfcomm, bnep
Parport_pc 32114 0
Step 5: view the output and uninstall the module
Root @ ubuntu:/home/Mydocument/Modules # dmesg
[1, 29665.689269] Hello, world!
Root @ ubuntu:/home/Mydocument/Modules # rmmod hello
Root @ ubuntu:/home/Mydocument/Modules # lsmod