1.helloworld.c
#include <linux/module.h>//information related to module
#include <linux/kernel.h>
#include <linux/init.h > //init-related functions
static int __init hellokernel_init (void)
{
printk (kern_info "Hello kernel!\n");
return 0;
}
static void __exit hellokernel_exit (void)
{
PRINTK (kern_info "Exit kernel!\n");
}
Module_init (hellokernel_init);
Module_exit (hellokernel_exit);
Module_license ("GPL");
Module_author ("xxxx");
2.Makefile
<pre name= "code" class= "CPP" >obj-m: = HELLOWORLD.O
PWD : = $ (Shell PWD) all
:
make-c/lib/ modules/$ (Shell uname-r)/build m=$ (PWD) modules clean: RM-RF *.o
core. *~ *.cmd. *.mod.c
3. Implementation and Results
Execute Make:
After the success of the compilation will generate a corresponding KO file, that is, we want to drive the
Driver-related actions:
To view the information of the KO module modinfo insert module insmod Helloworld.ko Uninstall module Rmmod HelloWorld also has a modprobe feature that later describes viewing driver printing information: Use DMESG to view the related printing information in the driver 。 Now there are examples of the following printed content:---------------------log start----------------------------[27520.195551] Exit kernel! [27948.531569] Hello kernel! ---------------------Log End----------------------------