内核模块Init和Exit函数的调用顺序错误

Ben*_*Ben 9 c linux kernel module kernel-module

我正在创建一个非常简单的hello world内核模块并获得一些疯狂的行为.这一直有效,直到我升级到内核3.3.8,现在它......好吧,它init在退出时调用函数,并exit在初始化时调用函数.我确保我的名字是正确的

// Needed for module definitions
#include <linux/module.h>
// Needed for initilization modules
#include <linux/init.h>

// Must declare some license
MODULE_LICENSE("Dual BSD/GPL");

// Function to be called on insmod
// Returns 0 on success
static int __init mymod_init(void)
{
        // Prints kernel alert.  Check /var/log/syslog
        printk(KERN_ALERT "Module was loaded, this is the printk.");

        return 0;
}

// Function to be called on rmmod
static void __exit mymod_exit(void)
{
        // Prints kernel alert.  Check /var/log/syslog
        printk(KERN_ALERT "Module was unloaded, this is the printk");
}

// Register these functions
module_init(mymod_init);
module_exit(mymod_exit);
Run Code Online (Sandbox Code Playgroud)

样本输出:

root @ cop4610:/home/cop4610/Downloads/linux-3.3.8/mymodule#insmod mymodule.ko root @ cop4610:/home/cop4610/Downloads/linux-3.3.8/mymodule#tail/var/log/syslog Oct 12 10:08:20 cop4610内核:[633.567832]模块已卸载,这是printk

以下是现场直播的视频:http://www.youtube.com/watch?v = 8aJNSpCd7as&feature = youroutu.be

Ben*_*Ben 17

它需要换行!!!!!! Arrggg!

 printk(KERN_ALERT "Module was unloaded, this is the printk\n");
Run Code Online (Sandbox Code Playgroud)

 printk(KERN_ALERT "Module was loaded, this is the printk\n");
Run Code Online (Sandbox Code Playgroud)

它似乎并没有真正按顺序执行,它只是出现了,因为第一个没有出现,直到第二个发布,因为缓冲区没有刷新.

  • 感谢您分享解决方案.我遇到了同样的问题并且感到困惑.看到你的答案后,它现在有意义了. (2认同)