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

查看:613
本文介绍了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 \n"); 
return 0; 
}

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

但是,对于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 \n");
    return 0;
}

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

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天全站免登陆