0

I am a beginner. I wrote a hello world module (actually copied from the o'Reilly book). the code is:

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

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
    printk(KERN_ALERT "\nHello World");
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "\nGood bye");
}

module_init(hello_init);
module_exit(hello_exit);

So when i insert the module, and dmesg nothig would appear. But when i remove it, and then dmesg, i can see

Hello World

Good Bye
3
  • 2
    Please always include the actual commands you ran. What's the "hello world module"? Did you write it? Did you check /var/log/kern.log? Which version of Ubuntu is this?
    – terdon
    Mar 12, 2014 at 14:17
  • I hope you know it is better to use grep to search for text in files such as /var/log/syslog and outputs of dmesg.
    – jobin
    Mar 12, 2014 at 21:42
  • The proper site for such questions is Stack Overflow, I think.
    – Danatela
    Mar 13, 2014 at 7:05

1 Answer 1

2

This is because the buffer is not cleared when you print "\nHello world" and cleared when ultimately you remove the module.

To deliberately clear the buffer, instead print

Hello world\n

and similarly,

Good bye\n

Note the newline \n at the end of each printk statement.

Refer to this SO question to know why a newline is necessary:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .