1. Hello.c



优先级低于3的才被打印
hello.c
#include
#include <linux/module.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("PD");
static int hello_init(void)
{
printk(KERN_SOH"hello_init \n");
return 0;
}
static void hello_exit(void)
{
printk("hello_exit \n");
return;
}
module_init(hello_init); //insmod
module_exit(hello_exit);//rmmod
Makefile
ifneq ($(KERNELRELEASE),)
obj-m:=hello.o
else
KDIR :=/lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.symvers *.cmd *.mod.c *.order
endif
Log
w@w:~/linux_kernel/hello$ ls
hello.c Makefile
w@w:~/linux_kernel/hello$ sudo make
make -C /lib/modules/5.4.0-104-generic/build M=/home/w/linux_kernel/hello modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-104-generic'
CC [M] /home/w/linux_kernel/hello/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC [M] /home/w/linux_kernel/hello/hello.mod.o
LD [M] /home/w/linux_kernel/hello/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-104-generic'
w@w:~/linux_kernel/hello$ ls
hello.c hello.mod hello.mod.o Makefile Module.symvers
hello.kohello.mod.c hello.o modules.order
w@w:~/linux_kernel/hello$ sudo insmod hello.ko
w@w:~/linux_kernel/hello$ sudo lsmod
Module Size Used by
hello 16384 0
w@w:~/linux_kernel/hello$ dmesg
[ 549.186132] �hello_init
w@w:~/linux_kernel/hello$ sudo rmmod hello.ko
w@w:~/linux_kernel/hello$ dmesg
[ 549.186132] �hello_init
[ 584.376325] hello_exit
w@w:~/linux_kernel/hello$ lsmod
Module Size Used by
网友评论