【问题标题】:"Printk" print order ambiguity“Printk”打印顺序不明确
【发布时间】:2014-08-19 15:11:32
【问题描述】:

我正在加载一个简单的内核模块,它有一个 init 和一个 exit 函数,每个都显示一条消息。 我正在使用日志级别 KERN_ALERT 来显示消息,问题是 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");

我得到的信息是,

[ 6310.329500] bye-bye
[ 6324.158871] Hello Kernel

我错过了这个颠倒的顺序背后有什么原因吗?

【问题讨论】:

    标签: kernel linux-device-driver


    【解决方案1】:

    您的内核模块没有问题。我建议添加'\n' 来刷新缓冲区。你可能会得到正确的输出。

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

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

    【讨论】:

      【解决方案2】:

      总是在 printk 的末尾使用 \n 以进行正确的刷新,否则这些消息会被缓冲并 c

      【讨论】:

        【解决方案3】:

        延迟是由于我为消息设置的优先级。优先级定义如下。

        #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*/
        

        默认数字是 4,这允许控制台仅在 至少在 KERN_WARNING 中。这可能是我看不到登录的原因 KERN_INFO 级别。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多