|  | 
 
| 今天在写模块时,犯了一个错误,为了大家别再犯,特记下: 
 Makefile文件:
 
 DFLAGS = -D __KERNEL__ -D MODULE
 CFLAGS = -O2 -g -Wall -Wstrict-prototypes -pipe -I/usr/include/linux/
 
 hello.o:hello.c
 gcc -c hello.c $(DFLAGS) $(CFLAGS) -o hello.o
 
 clean:
 rm -f *.o *.dat
 
 原文件:hello.c
 
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/mm.h>
 #include <linux/errno.h>
 #include <linux/fs.h>
 #include <linux/string.h>
 
 #include <asm/fcntl.h>
 #include <asm/processor.h>
 #include <asm/uaccess.h>
 
 static int count;
 static char* string;
 
 MODULE_PARM(count,"i");
 MODULE_PARM(string,"s");
 
 int init_module(void)
 {
 int result;
 unsigned long page=0;
 struct file* filp=NULL;
 mm_segment_t old_fs;
 
 printk("Hello,I am the kernel module.\n");
 
 if( !(page=__get_free_page(GFP_KERNEL)) )
 {
 return -ENOMEM;
 }
 printk("count=%d.\n",count);
 filp=filp_open("/usr/huym/module/test.dat",O_CREAT|O_WRONLY,0);
 if(!filp)
 {
 return 0;
 }
 /*block_write(filp,"write module test.\n",19,0);*/
 if(filp->f_op && filp->f_op->write)
 {
 if(string)
 {
 /*注意的地方*/
 old_fs=get_fs();
 set_fs(get_ds());
 result=filp->f_op->write(filp,string,strlen(string),&filp->f_pos);
 if(result<0)
 {
 printk("write error.\n");
 }
 set_fs(old_fs);
 }
 }
 
 filp_close(filp,NULL);
 printk("string=%s.\n",string);
 
 free_page(page);
 
 return 0;
 }
 
 void cleanup_module(void)
 {
 printk("Goodbye.\n");
 }
 
 命令:
 
 make
 
 insmod hello.o count=2 string="i am write."
 
 删除可:
 
 rmmod hello
 
 make clean
 | 
 |