hellop.c 文件内容如下:
- /*
-
* $Id: hellop.c,v 1.4 2004/09/26 07:02:43 gregkh Exp $
-
*/
-
#include <linux/init.h>
-
#include <linux/module.h>
-
#include <linux/moduleparam.h>
-
-
MODULE_LICENSE("Dual BSD/GPL");
-
-
/*
-
* These lines, although not shown in the book,
-
* are needed to make hello.c run properly even when
-
* your kernel has version support enabled
-
*/
-
-
-
/*
-
* A couple of parameters that can be passed in: how many times we say
-
* hello, and to whom.
-
*/
-
static char *whom = "world";
-
static int howmany = 1;
-
module_param(howmany, int, S_IRUGO);
-
module_param(whom, charp, S_IRUGO);
-
-
static int hello_init(void)
-
{
-
int i;
-
for (i = 0; i < howmany; i++)
-
printk(KERN_ALERT "(%d) Hello, %s\n", i, whom);
-
return 0;
-
}
-
static void hello_exit(void)
-
{
-
printk(KERN_ALERT "Goodbye, cruel world\n");
-
}
-
-
module_init(hello_init);
-
module_exit(hello_exit);
Makefile 文件内容如下:
- # To build modules outside of the kernel tree, we run "make"
-
# in the kernel source tree; the Makefile these then includes this
-
# Makefile once again.
-
# This conditional selects whether we are being included from the
-
# kernel Makefile or not.
-
ifeq ($(KERNELRELEASE),)
-
-
# Assume the source tree is where the running kernel was built
-
# You should set KERNELDIR in the environment if it's elsewhere
-
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
-
# The current directory is passed to sub-makes as argument
-
PWD := $(shell pwd)
-
-
modules:
-
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
-
-
modules_install:
-
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
-
-
clean:
-
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
-
-
.PHONY: modules modules_install clean
-
-
else
-
# called from kernel build system: just declare what our modules are
-
obj-m := hellop.o
-
endif
$ make 编译
$ sudo insmod hellop.ko howmany=10 whom="Tom" 加载模块
在/var/log/syslog中打印信息如下:
- Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364464] (0) Hello, Tom
-
Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364468] (1) Hello, Tom
-
Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364470] (2) Hello, Tom
-
Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364471] (3) Hello, Tom
-
Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364473] (4) Hello, Tom
-
Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364474] (5) Hello, Tom
-
Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364476] (6) Hello, Tom
-
Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364477] (7) Hello, Tom
-
Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364479] (8) Hello, Tom
-
Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364480] (9) Hello, Tom
阅读(2913) | 评论(0) | 转发(2) |