Linux Kernel Modules
《Operating-System-Concepts(9th)》
Part I—Creating Kernel Modules
The first part of this project involves following a series of steps for creating and inserting a module into the Linux
kernel. You can list all kernel modules that are currently loaded by entering the command
lsmod
This command will list the current kernel modules in three columns: name, size, and where the module is being
used. The following program (named simple.c and available with the source code for this text) illustrates a very
basic kernel module that prints appropriate messages when the kernel module is loaded and unloaded.
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* This function is called when the module is loaded. */
int simple_init(void) {
printk(KERN INFO "Loading Module\n");
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN INFO "Removing Module\n");
}
/* Macros for registering module entry and exit points. */
module init(simple_init);
module exit(simple_exit);
MODULE LICENSE("GPL");
MODULE DESCRIPTION("Simple Module");
MODULE AUTHOR("SGG");