The need to use MACROS, for example module_init and module_exit, when writing loadable kernel modules
What are the MACRO, such as module_init
, and module_exit
when writing loadable kernel modules? Also, why are we using MACRO such as __init
or __exit
. Even though we can do this job without using them.
-
Without MACROS
/* Without using MACROS Author: Sricharan Chiruvolu Date: 14 Dec 2014 */ #include <linux/module.h> #include <linux/kernel.h> int init_module(void){ printk(KERN_ALERT "This is our first program."); return 0; } void cleanup_module(void){ printk(KERN_ALERT "End of our first program."); }
-
With MACROs
/* Edited first.c; Added macros module_init and module_exit Author: Sricharan Chiruvolu Date: 14 Dec 2014 */ #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int __init first_init(void) { printk(KERN_ALERT "This is our first program."); return 0; } static void __exit first_exit(void) { printk(KERN_ALERT "End of our first program."); } module_init(first_init); module_exit(first_exit);
What is the difference?
source to share
module_{init,exit}()
adds the required template to initialize / clean the module and runs the I / O code when the module file is loaded / unloaded into or from kernel space.
__init
tells the kernel that this function is executed once and is never returned mainly for embedded drivers, but rather module_init()
to initialize the module when it is in insmod.
"
The __init attribute causes the initialization function to be discarded, and its memory is fixed after initialization is full. However, it only works for built-in drivers; it has no effect on modules. __exit instead leads to the omission of the noted when the driver is not built as a module; again, in modules, this has no effect.
Using __init (and __initdata for items) can reduce the amount of memory used by the kernel. There is no harm in marking a module's initialization function with __init, although there is currently no benefit. Initialization section management has not yet been implemented for modules, but this is a possible improvement for the future.
"
source to share