加载驱动时传参数--module_param

加载驱动时传参数的实例:

#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static char *string = "param test\n";
static int     times = 1; 

module_param(times, int, S_IRUGO);
module_param(string, charp, S_IRUGO);


static int hello3_init(void)
{
	int i = 0;

	printk(KERN_ALERT "Hello, World!\n");

	for(i = 0; i < times; i++)
	{
		printk("string: %s\n", string);
	}

	return 0;
}

static void hello3_exit(void)
{
	printk(KERN_ALERT "Goodbye, cruel world!\n");
}


module_init(hello3_init);
module_exit(hello3_exit);


运行结果:

[root@(none)/]#insmod hello3.ko 
Hello, World!
string: param test
[root@(none)/]#rmmod hello3
Goodbye, cruel world!
[root@(none)/]#
[root@(none)/]#insmod hello3.ko times=5 string="I AM IN hello3.c\n"
Hello, World!
string: I AM IN hello3.c\n
string: I AM IN hello3.c\n
string: I AM IN hello3.c\n
string: I AM IN hello3.c\n
string: I AM IN hello3.c\n
[root@(none)/]#rmmod hello3
Goodbye, cruel world!
[root@(none)/]#


虽然是入门新手,但坚持下去就会有收获!

加油加油加油!

linux就是黑暗中的一片光明!

你可能感兴趣的:(linux,String,Module)