linux内核版本和mudule不一致,Linux内核模块中的module_init和init_module有什么区别?(What is the difference between module_i...

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.

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

#define MODULE

#include 

#include 

int init_module(void)

{

printk(KERN_INFO "Hi n");

return 0;

}

void cleanup_module(void)

{

printk(KERN_INFO "Bye n");

}

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

#include 

#include 

#include 

static int hi_init(void)

{

printk(KERN_ALERT "Hi n");

return 0;

}

static void hi_exit(void)

{

printk(KERN_ALERT "Bye n");

}

module_init(hi_init);

module_exit(hi_exit);

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)));

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驱动程序,并且意识到内核版本2.4与2.6之间存在实质性差异。

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

#define MODULE

#include

#include

int init_module(void)

{

printk(KERN_INFO Hi n);

返回0;

}

void cleanup_module(void)

{

printk(KERN_INFO Bye n);

}

但是,对于2.6版本的内核,必须为模块-

#include

#include

#include

static int hi_init(void)

{

printk(KERN_ALERT Hi n);

返回0;

}

static void hi_exit(void)

{

printk(KERN_ALERT Bye n);

}

module_init(hi_init);

module_exit(hi_exit);

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

解决方案

如果您查看新函数的定义:

/ *每个模块必须使用一个module_init()。 * /

#define module_init(initfn)

静态内联initcall_t __inittest(void)

{return initfn; }

int init_module(void)__attribute __((alias(#initfn)));

/ *仅当您要卸载时才需要。 * /

#define module_exit(exitfn)

静态内联exitcall_t __exittest(void)

{return exitfn; }

void cleanup_module(void)__attribute __((alias(#exitfn)));

您会看到它确保包含正确的样板,因此可以通过以下方式正确处理这些特殊功能编译器。这就是Linux内部API的作用,如果有更好的解决方法,它就会发展。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值