代码实现如下:
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("weipeng jing");
MODULE_DESCRIPTION("Hello World Module");
MODULE_ALIAS("a simplest module");
static char * data="pang";
static char * data2=" ";
module_param(data,charp,S_IRUGO);
module_param(data2,charp,S_IRUGO);
static int hello_init()
{
int len=0;
int i,j;
while(data[len]!='\0')
{
len++;
}
for(i=len-1,j=0;i>=0;i--)
{
data2[j]=data[i];
j++;
}
data2[j]='\0';
printk(KERN_EMERG"%d\n",len);
printk(KERN_EMERG"Hello World!\n");
return 0;
}
static void hello_exit()
{
printk(KERN_EMERG"%s\n",data2);
printk(KERN_EMERG"hello exit\n");
printk(KERN_EMERG"good bye!\n");
}
module_init(hello_init);
module_exit(hello_exit);
2.insmod hello.ko
3.rmmod hello
即会输出逆置后的字符串
特别注意,在用两个字符指针作为全局变量时,一定要对其进行初始化,否则将会出现卡死的后果。
此代码是在原有模块的代码上进行修改的,只修改了源代码,其他并未进行修改。在make clean之后,文件夹中一共有包括源文件在内的四个文件,分别是:hello.c hello.ko.unsigned Makefile modules.order(不可读)。
!!!相信自己,相信知识。后经发现,如果.c文件和Makefile文件一旦写对,即可以实现出现所有的文件,之所以一直不能出现所有的文件,只是因为.c文件没有写对。