Linux 内核模块中的 module_init 和 init_module 有什么区别? [英] What is the difference between module_init and init_module in a Linux kernel module?

查看:34
本文介绍了Linux 内核模块中的 module_init 和 init_module 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试移植一些 linux 驱动程序,并意识到 linux 的内核版本 2.4 和 2.6 之间存在重大差异.

I have been trying to port few linux drivers and realized that there is substantial difference between kernel version 2.4 and 2.6 of linux.

在2.4版本的内核中,模块编程如下-

In the 2.4 version of kernel, the module programming was as below -

#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)      
{ 
printk(KERN_INFO "Hi 
"); 
return 0; 
}

void cleanup_module(void)  
{ 
printk(KERN_INFO "Bye 
"); 
}

但是,对于 2.6 版本的内核,必须对模块进行以下操作 -

But, with the 2.6 version of kernel, the following has to be done for modules -

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

static int hi_init(void)
{
    printk(KERN_ALERT "Hi 
");
    return 0;
}

static void hi_exit(void)
{
    printk(KERN_ALERT "Bye 
");
}

module_init(hi_init);
module_exit(hi_exit);

在内核 2.6 中进行此类更改的优势是什么?为什么需要在 linux 内核 2.6 中进行更改?

What is the advantage of such changes in Kernel 2.6 and Why was that change required in kernel 2.6 of linux ?

推荐答案

如果你看一下新函数的定义:

If you look at the definition of the new functions:

/* Each module must use one module_init(). */
#define module_init(initfn)                 
static inline initcall_t __inittest(void)       
{ return initfn; }                  
int init_module(void) __attribute__((alias(#initfn)));

/* This is only required if you want to be unloadable. */
#define module_exit(exitfn)                 
static inline exitcall_t __exittest(void)       
{ return exitfn; }                  
void cleanup_module(void) __attribute__((alias(#exitfn)));

您会看到它确保包含正确的样板文件,以便编译器可以正确处理这些特殊函数.这就是 Linux 的内部 API 所做的,如果有更好的方法来解决问题,它就会发展.

You'll see it ensures that the right boilerplate is included so these special functions can be correctly treated by the compiler. It's what the internal API of Linux does, it evolves if there are better ways of solving the problem.

这篇关于Linux 内核模块中的 module_init 和 init_module 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆