传入参数

[root@bogon modules]# cat first.c
#include<linux/kernel.h>
#include<linux/stat.h>
#include<linux/moduleparam.h>
#include<linux/init.h>
#include<linux/module.h>
static short int a=1;
static int b=2;
static long int c=3;
static char *d="bp";
static int myintArray[2]={-1,-1};
static int arr_argc=0; module_param(a,short,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
MODULE_PARM_DESC(a,"a short integer");
module_param(b,int ,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
MODULE_PARM_DESC(b,"an integer");
module_param(c,long,S_IRUSR);
MODULE_PARM_DESC(c,"a long integer");
module_param(d,charp,0000);
MODULE_PARM_DESC(d,"a char string");
module_param_array(myintArray,int,&arr_argc,0000);
MODULE_PARM_DESC(myintArray,"an array of integers");
static int __initdata hellodata=3;
static int __init bp_init(void){
int i;
printk(KERN_ALERT "hello world\n");
printk(KERN_ALERT "a is a short integer:%d\n",a);
printk(KERN_ALERT "b is a integer:%d\n",b);
printk(KERN_ALERT "c is a long integer:%d\n",c);
printk(KERN_ALERT "d is a string:%s\n",d);
for(i=0;i<(sizeof(myintArray)/sizeof(int));i++)
printk(KERN_ALERT "myintArray[%d] is %d\n",i,myintArray[i]);
printk(KERN_ALERT "\nhi,this is bp %d \n",hellodata);
return 0;
}
static void __exit bp_exit(void){
printk(KERN_ALERT "\ngoobye bp\n");
}
module_init(bp_init);
module_exit(bp_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("by bp");
MODULE_DESCRIPTION("this is test of bp");
MODULE_SUPPORTED_DEVICE("testdevice");
[root@bogon modules]# cat Makefile
obj-m=first.o
default:
make -C /usr/src/kernels/`uname -r` M=$(PWD) modules
clean:
make -C /usr/src/kernels/`uname -r` M=$(PWD) clean
[root@bogon modules]# make
make -C /usr/src/kernels/`uname -r` M=/root/modules modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
[root@bogon modules]# insmod first.ko a=100
[root@bogon modules]# rmmod first
[root@bogon modules]#

运行效果

linux内核编程helloworld(中级)-LMLPHP

04-14 02:36