How program and compile "Hello World" code in kernel mode of linux?
Yes, as the title, I don't know how to program and compile "Hello World" code in kernel mode of linux , please help me in the sh开发者_Go百科ortest and easy to understand way. Thank you ! (Any related document is welcomed too, I'm just new to this)
You can start Here:
/*
* hello-1.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
Wow, that's a question!
Just think first that the Linux kernel has no terminal, no direct interaction with the user. A Hello World cannot be invoked as any other user program on the command line. The best fit I can think of is a character device driver implemented as a kernel module that would read "Hello World" on device /dev/helloworld for example.
I can point you to reading the book from Rubini: Linux Device Drivers. It explains and has examples to create simple Hello World kind of kernel modules.
Additional information: The printk function is provided by kernel and it prints to the file such as /var/log/messages. In Ubuntu, this is the /var/log/syslog file. You can see the output of the hello module in this file. Also, thanks fseto for pointing the Linux Kernel Module Programming Guide. It is awesome.
精彩评论