linux内核编程基础(一)

linux内核模块(Module)编程
编写一个简单的模块
//helloworld.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
static int __init lkp_init(void){
 printk("<1>hello world! from kernel space\n");
}
static void __exit lkp_cleanup()
{
 printk("<1>goodbyte  world! leaving  kernel space\n");
}
module_init(lkp_init);
module_exit(lkp_cleanup);
MODULE_LICENSE("GPL");


#Makefile3.0
obj-m:= helloworld.o
CURRENT_PATH:= $(shell pwd)
LINUX_KERNEL:= $(shell uname -r)
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)
all:
        make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
clean:
        make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean


make命令运行Makefile
insmod helloworld.ko
dmesg
rmmod hello


你可能感兴趣的:(linux内核编程基础(一))