Template Information

Trang

The Hello World Module

Thứ Bảy, 4 tháng 6, 2011 / 20:25

Many programming books begin with a “hello world” example as a way of showing
the simplest possible program. This book deals in kernel modules rather than programs;
so, for the impatient reader, the following code is a complete “hello world”
module:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
This module defines two functions, one to be invoked when the module is loaded
into the kernel (hello_init)and one for when the module is removed (hello_exit). The
module_init and module_exit lines use special kernel macros to indicate the role of
these two functions. Another special macro (MODULE_LICENSE)is used to tell the
kernel that this module bears a free license; without such a declaration, the kernel
complains when the module is loaded.

0 nhận xét:

Đăng nhận xét