Linux内核模块-实例3(二)
2014-04-06 19:50
399 查看
2.3 Hello World(P3):命令行参数传递
环境:Linux内核 2.6
1.示例代码(hello-2.c)
2.运行过程
首先创建Makefile文件(参照2.1中Makefile的创建过程),然后装载卸载模块。
[root@localhost test]# insmod hello-2.ko myint=2014 myString="welcome" myintArray=4,5
[root@localhost test]# rmmod hello-2.ko
[root@localhost test]# dmesg | tail -10
Hello, world 2
=============
myshort is a short integer: 1
myint is an integer: 2014
mylong is a long integer: 9999
myString is a string: welcome
myintArray[0] = 4
myintArray[1] = 5
got 2 arguments for myintArray.
Goodbye, world 2
相关链接:
内核模块编程入门:http://www.tldp.org/LDP/lkmpg/2.6/html/index.html
环境:Linux内核 2.6
1.示例代码(hello-2.c)
/* * hello-2.c - Demonstrates command line argument passing to a module. */ # include <linux/module.h> # include <linux/moduleparam.h> # include <linux/kernel.h> # include <linux/init.h> # include <linux/stat.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("Star"); static short int myshort = 1; static int myint = 420; static long int mylong = 9999; static char *myString = "blah"; static int myintArray[2] = {-1, -1}; static int arr_argc = 0; /* * module_param(foo, int, 0000) * The first param is the parameters name * The second param is it's data type * The final argument is the permissions bits, * for exposing parameters in sysfs(if non-zero) at a later stage. */ module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); MODULE_PARM_DESC(myshort, "A short integer"); module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); MODULE_PARM_DESC(myint, "An integer"); module_param(mylong, long, S_IRUSR); MODULE_PARM_DESC(mylong, "A long integer"); module_param(myString, charp, 0000); MODULE_PARM_DESC(myString, "A character string"); /* * module_param_array(name, type, num, per) * The first param is the parameter's (in this case the array's) name * The second param is the data type of the elements of the array * The third argument is a pointer to the variable that will store the number * of elements of the array initialized by the user at module loading time * The fourth argument is the permission bits */ module_param_array(myintArray, int, &arr_argc, 0000); MODULE_PARM_DESC(myintArray, "An array of integers"); static int __init hello_2_init(void) { int i; printk(KERN_INFO "Hello, world 2\n=============\n"); printk(KERN_INFO "myshort is a short integer: %hd\n", myshort); printk(KERN_INFO "myint is an integer: %d\n", myint); printk(KERN_INFO "mylong is a long integer: %ld\n", mylong); printk(KERN_INFO "myString is a string: %s\n", myString); for(i=0; i < (sizeof myintArray / sizeof(int)) ; i++) { printk(KERN_INFO "myintArray[%d] = %d\n", i, myintArray[i]); } printk(KERN_INFO "got %d arguments for myintArray.\n", arr_argc); return 0; } static void __exit hello_2_exit(void) { printk(KERN_INFO "Goodbye, world 2\n"); } module_init(hello_2_init); module_exit(hello_2_exit);
2.运行过程
首先创建Makefile文件(参照2.1中Makefile的创建过程),然后装载卸载模块。
[root@localhost test]# insmod hello-2.ko myint=2014 myString="welcome" myintArray=4,5
[root@localhost test]# rmmod hello-2.ko
[root@localhost test]# dmesg | tail -10
Hello, world 2
=============
myshort is a short integer: 1
myint is an integer: 2014
mylong is a long integer: 9999
myString is a string: welcome
myintArray[0] = 4
myintArray[1] = 5
got 2 arguments for myintArray.
Goodbye, world 2
相关链接:
内核模块编程入门:http://www.tldp.org/LDP/lkmpg/2.6/html/index.html
相关文章推荐
- Linux内核模块开发实例学习
- Linux内核模块-实例1(二)
- Linux内核模块-实例2(二)
- Linux内核模块-实例4(二)
- Linux内核模块与Makefile实例
- Linux内核模块与Makefile实例
- Linux内核模块实例
- Linux内核模块开发实例学习
- 最简单的linux内核模块实例
- ASP.NET TreeView树型菜单操作实例(代码调试通过)
- 〖ExtJS〗之Panel实例
- Tapestry4+Spring+Hibernate整合实例及说明
- HID - Keyboard键盘和Mouse鼠标实例
- S3C2440上LCD驱动(FrameBuffer)实例开发讲解(二)
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- java多线程交互 经典实例__生产者与消费者模型
- K2 Blackpearl中从数据库直接删除流程实例之K2Server表
- apache伪静态规则及常见规则用法实例
- javascript树形菜单简单实例
- hadoop+zookeeper+hbase安装、配置及应用实例