ArunMKumar
ArunMKumar

Reputation: 718

"Printk" print order ambiguity

I am loading a simple kernel Module that has a init and an exit function, displaying a message each. i am using the log level KERN_ALERT to display the messages, the issue is that the Exit message shows first and then the Init message.

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

the message that i get is ,

[ 6310.329500] bye-bye
[ 6324.158871] Hello Kernel

is their any reason behind this inverted order that i am missing?

Upvotes: 0

Views: 1029

Answers (3)

ArunMKumar
ArunMKumar

Reputation: 718

The delay is due to the priority that i set to the message. The priority is defined as follows.

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

The default number is 4, which allows console to show messages only at least in KERN_WARNING. That could be why i cannot see log in KERN_INFO level.

Upvotes: 0

Saurabh Sengar
Saurabh Sengar

Reputation: 918

always use \n at the end of printk for proper flushing, other wise these messages gets buffered and c

Upvotes: 0

Jeyaram
Jeyaram

Reputation: 9474

There is no issue with your kernel module. I suggest to add '\n' to flush the buffer. You may get proper output.

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

Note :: clear kernel messages by dmesg -c and suggest to double check output.

Upvotes: 1

Related Questions