linux内核编程
-
1 #include <linux/module.h>
-
2 #include <linux/kernel.h>
-
3 #include <linux/init.h>
-
4 static int __init lkp_init(void);
-
5 static int __exit lkp_exit(void);
-
6
-
7
-
8 static int __init lkp_init(void)
-
9 {
-
10 printk("<1>Hello ,Word!\n");
-
11 return 0;
-
12 }
-
13
-
14 static int __exit lkp_exit(void)
-
15 {
-
16 printk("<2>Hello,Word exit\n");
-
return 0;
-
17 }
-
18 module_init(lkp_init);
- 19 module_exit(lkp_exit);
-
1 ifneq ($(KERNELRELEASE),)
-
2 mymodule-objs:= hello.c
-
3 obj-m += hello.o
-
4
-
5 else
-
6 PWD :=$(shell
pwd)
-
7 KVER := $(shell uname -r)
-
8 KDIR :=/lib/modules/$(KVER)/build
-
9
-
10 all:
-
11 $(MAKE) -C $(KDIR) M=$(PWD)
-
12 clean:
-
13 rm -rf *.o *.mod.c *.ko *.symvers *order *.markers *-
- 14 endif
-
379 # Read KERNELRELEASE from include/config/kernel.release (if it
exists)
-
380 KERNELRELEASE = $(shell cat include/config/kernel.release
2> /dev/null)
- 381 KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL) ,.$(SUBLEVEL)))$(EXTRAVERSION)
-
[#35#caopeng@laptop:~]$cd
kernel/
-
[#36#caopeng@laptop:~/kernel]$ls
-
hello.c Makefile
-
[#37#caopeng@laptop:~/kernel]$make
-
make -C /lib/modules/2.6.32-33-generic/build
M=/home/caopeng/kernel
-
make[1]: 正在进入目录 `/usr/src/linux-headers-2.6.32-33-generic‘
-
LD /home/caopeng/kernel/built-in.o
-
CC [M] /home/caopeng/kernel/hello.o
-
/home/caopeng/kernel/hello.c: In function ‘lkp_exit’:
-
/home/caopeng/kernel/hello.c:17: warning: no
return statement in function returning non-void
-
/home/caopeng/kernel/hello.c: In function ‘__exittest’:
-
/home/caopeng/kernel/hello.c:19: warning: return
from incompatible pointer type
-
Building modules, stage 2.
-
MODPOST 1 modules
-
CC /home/caopeng/kernel/hello.mod.o
-
LD [M] /home/caopeng/kernel/hello.ko
-
make[1]:正在离开目录 `/usr/src/linux-headers-2.6.32-33-generic‘
- [#38#caopeng@laptop:~/kernel]$
-
[#38#caopeng@laptop:~/kernel]$ls
-
built-in.o hello.ko
hello.mod.o Makefile Module.symvers
-
hello.c hello.mod.c
hello.o modules.order
- [#39#caopeng@laptop:~/kernel]$
-
[#47#caopeng@laptop:~/kernel]$sudo
rmmod hello.ko
-
[#48#caopeng@laptop:~/kernel]$sudo
insmod hello.ko
-
[#49#caopeng@laptop:~/kernel]$dmesg -c
-
klogctl: 不允许的操作
-
[#50#caopeng@laptop:~/kernel]$sudo
dmesg -c
-
[14651.331364] Hello,Word exit
-
[14658.553284] Hello ,
- [#51#caopeng@laptop:~/kernel]$
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。