hellop.c 文件内容如下:
Makefile 文件内容如下:
$ make 编译
$ sudo insmod hellop.ko howmany=10 whom="Tom" 加载模块
在/var/log/syslog中打印信息如下:
阅读(287) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
- /*
- * $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);
- # 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
$ 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
相关热门文章
给主人留下些什么吧!~~
评论热议