__init&的用法__exit属性

时间:2013-10-06 12:40:43

标签: operating-system linux-device-driver kernel-module kernel

如果__init& __exit属性用于初始化和退出模块&如果我不使用它会发生什么。示例如下所示。

使用属性

static __init int myinit(void)
{}
static __exit void myexit(void)
{}

Witout属性

static int myinit(void)
{}
static void myexit(void)
{}

2 个答案:

答案 0 :(得分:3)

@Sandy,__ init宏会导致init函数被丢弃,并且一旦init函数完成内置驱动程序,它的内存(vmalloc)就会被释放。当模块内置到内核中时,__ exit宏会导致省略函数。 __init和__exit都不适合LKM。还要浏览这些链接 What does __init mean in the Linux kernel code? http://amar-techbits.blogspot.in/2012/08/understanding-macro-init-and-exit-in.html

答案 1 :(得分:3)

主要区别在于释放内存。

它中的__init token是内核提示给定的function is used only at initialization time.模块加载器drops the initialization function after the module is loaded, making its memory available for other uses. 对于仅在初始化期间使用的数据,存在类似的标记(__initdata)。使用__init和__initdata是可选的,但值得一试。请务必不要使用它们 初始化完成后将使用的任何函数(或数据结构)。

使用__init family of macros to place one-time initialization routines into a common section in the object file.表兄 __initdata,用于标记一次性使用的数据项。功能和 使用这些宏标记为初始化的数据被收集到一个特别命名的ELF部分。

稍后,在使用这些一次性初始化函数和数据对象之后,kernel frees the memory occupied by these items。您可能已经在决赛附近看到了熟悉的内核消息 boot process saying, "Freeing init memory: 296K." .

的一部分

放置的目的 将此函数放入目标文件的特殊部分是memory space that it occupies can be reclaimed when it is no longer needed.