Kernel &引用;Printk";打印顺序模糊

Kernel &引用;Printk";打印顺序模糊,kernel,linux-device-driver,Kernel,Linux Device Driver,我正在加载一个简单的内核模块,它有一个init和一个exit函数,分别显示一条消息。 我使用日志级别的KERN_警报来显示消息,问题是先显示Exit消息,然后显示Init消息 #include <linux/init.h> #include <linux/module.h> static int my_init(void){ printk(KERN_ALERT "Hello Kernel"); return 0; } static void my_ex

我正在加载一个简单的内核模块,它有一个init和一个exit函数,分别显示一条消息。 我使用日志级别的KERN_警报来显示消息,问题是先显示Exit消息,然后显示Init消息

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

static int my_init(void){
    printk(KERN_ALERT "Hello Kernel");
    return 0;
}

static void my_exit(void){
    printk(KERN_ALERT "bye-bye");
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");

我所缺少的颠倒顺序背后有什么原因吗?

您的内核模块没有问题。我建议添加
'\n'
以刷新缓冲区。你可以得到适当的输出

printk(KERN_ALERT "Hello Kernel \n");
                                |
                                |
                                V
                           For flushing buffer.

注意:通过
dmesg-c
清除内核消息,并建议仔细检查输出

始终在printk末尾使用\n进行适当的刷新,否则这些消息会被缓冲,而延迟是由于我设置了消息的优先级。优先级定义如下

#define KERN_EMERG "<0>" /* system is unusable*/
#define KERN_ALERT "<1>" /* action must be taken immediately*/
#define KERN_CRIT "<2>" /* critical conditions*/
#define KERN_ERR "<3>" /* error conditions*/
#define KERN_WARNING "<4>" /* warning conditions*/
#define KERN_NOTICE "<5>" /* normal but significant condition*/
#define KERN_INFO "<6>" /* informational*/
#define KERN_DEBUG "<7>" /* debug-level messages*/
#define KERN_EMERG”“/*系统不可用*/
#定义内核警报“”/*必须立即采取措施*/
#定义KERN_CRIT”“/*临界条件*/
#定义KERN_ERR”“/*错误条件*/
#定义内核警告“/*警告条件*/
#定义KERN_NOTICE“/*正常但重要的条件*/
#定义内核信息“/*信息性*/
#定义内核调试“/*调试级消息*/
默认数字为4,这允许console仅在 至少在KERN_警告中。这可能是我无法看到登录的原因 内核信息级别

#define KERN_EMERG "<0>" /* system is unusable*/
#define KERN_ALERT "<1>" /* action must be taken immediately*/
#define KERN_CRIT "<2>" /* critical conditions*/
#define KERN_ERR "<3>" /* error conditions*/
#define KERN_WARNING "<4>" /* warning conditions*/
#define KERN_NOTICE "<5>" /* normal but significant condition*/
#define KERN_INFO "<6>" /* informational*/
#define KERN_DEBUG "<7>" /* debug-level messages*/