第6章 模块编程实验.ppt_第1页
第6章 模块编程实验.ppt_第2页
第6章 模块编程实验.ppt_第3页
第6章 模块编程实验.ppt_第4页
第6章 模块编程实验.ppt_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、模块编程实验 _ 1 成都信息工程学院 徐虹,第6章 模块编程实验,. / 期望该模块所能实现的一些功能函数,如open()、release()、write()、 6.1 实验目的 6.2 准备知识 6.2.1 模块的组织结构 / 其他header信息 int init_module() #include ,模块编程实验 _ 2 成都信息工程学院 徐虹,. / 声明是一个模块 . / 加载时,初始化模块的编码 #include / 说明是个内核功能 / read()、ioctl()等函数 . void cleanup_module() . / 卸载时,注销模块的编码 ,模块编程实验 _ 3 成

2、都信息工程学院 徐虹,6.2.2 模块的编译 #gcc O2 g Wall DMODULE D_KERNEL_c filename.c / filename.c为自己编写的模块程序源代码文件 6.2.3 模块的加载 6.2.4 模块的卸载 6.2.5 模块链接到内核的示意图 6.2.6 模块程序中管理模块的几个文件操作,模块编程实验 _ 4 成都信息工程学院 徐虹,int(*lseek)(),int(*read)(),int(*write)(),int(*readdir)(),int(*select)(), int (*ioctl)(),int (*mmap)(),int (*open)(),

3、void (*release)(), int(*fsync)(), int(*fasync)(), int(*check_media_change)(), int(*revalidate)(),模块编程实验 _ 5 成都信息工程学院 徐虹,图6-1 模块链接到内核的示意图,模块编程实验 _ 6 成都信息工程学院 徐虹,NULL, / modulename_lseek, 改变模块结构中的操作位置 modulename_read, modulename_write, NULL, / modulename_readdir,读取某个子目录中的内容 struct file_operations modu

4、lename_fops = NULL, / modulename_select,允许应用程序响应来自模块的事件,模块编程实验 _ 7 成都信息工程学院 徐虹,NULL, / modulename_ioctl,应用程序通过I/O控制系统的系统调 / 用来控制模块行为 NULL, / modulename_mmap,模块地址空间到用户地址空间的映射 modulename_open, modulename_release, NULL, / modulename_fsync,同步内存与磁盘上的数据状态,输,模块编程实验 _ 8 成都信息工程学院 徐虹,/ 出缓冲区里尚未写到磁盘的数据写出去 NULL,

5、 / modulename_fasync,改变模块行为 NULL, / modulename_check_media_change,检查自上次操作后, / 介质(软盘和CD-ROM)是否更换 NULL / modulename_revalidate,若更换了介质,则更新信息 ,模块编程实验 _ 9 成都信息工程学院 徐虹,6.3 实验内容 6.3.1 编写一个简单的内核模块 6.3.2 模块加载前后的比较 6.3.3 向模块中再添加一些新函数 6.4 实验指导 6.4.1 一个简单的内核模块,模块编程实验 _ 10 成都信息工程学院 徐虹,#include / 在内核模块中共享 #includ

6、e / 一个模块 / 处理CONFIG_MODVERSIONS #if CONFIG_MODVERSIONS = 1 #define MODVERSIONS #include #endif int init_module() / 初始化模块,模块编程实验 _ 11 成都信息工程学院 徐虹, printk(Hello! This is a testing module! n); return 0; void cleanup_module() / 取消init_module()函数所做的打印功能操作 printk(Sorry! The testing module is unloading now

7、! n); ,模块编程实验 _ 12 成都信息工程学院 徐虹,rootlinux /# gcc O2 Wall DMODULE D_KERNEL_ c testmodule.c rootlinux /# ls s / 在当前目录下查看生成的目标文件testmodule.o rootlinux /# insmod f testmodule.o Hello! This is a testing module!,模块编程实验 _ 13 成都信息工程学院 徐虹,rootlinux /# rmmod testmodule Sorry! The testing module is unloading no

8、w! 6.4.2 模块加载前后的比较 #include / 用户空间的标准I/O出头文件 void GetCr3() ,模块编程实验 _ 14 成都信息工程学院 徐虹,int iValue; _ _asm_ _volatile_ _(movl %cr3,%0: =r (a); printf(“the value in cr3 is: %d”,a); / 用户空间的标准输出函数 int main() ,模块编程实验 _ 15 成都信息工程学院 徐虹,GetCr3(); return 0; #include int init_module() int iValue; _ _asm_ _volati

9、le_(,模块编程实验 _ 16 成都信息工程学院 徐虹,movl %cr3,%0: =r(iValue); printf(cr3:%d,iValue); return 0; void cleanup_module(void) printk(uninstall GetCr3! n); ,模块编程实验 _ 17 成都信息工程学院 徐虹,DFLAGS=D_KERNEL_DMODULE CFLAGS=O2 g Wall Wstrict-prototypes pipe l/user/include/linux/ gcc c GetCr3.c $(DFLAGS) $(CFLAGS) o GetCr3.o

10、 clean: rm f*.o GetCr3.o: GetCr3.c,模块编程实验 _ 18 成都信息工程学院 徐虹,rootlinuxserver root# /sbin/insmod GetCr3.o Cr3:234320012 / 这个数值有可能不一样 rootlinuxserver root# /sbin/rmmod GetCr3 Uninstall GetCr3!,模块编程实验 _ 19 成都信息工程学院 徐虹,6.4.3 向模块中添加新函数 int open(struct inode*inode,struct file*filp) MOD_INC_USE_COUNT; / 增加该模

11、块的用户数目 printk(This module is in open!n); return 0; ,模块编程实验 _ 20 成都信息工程学院 徐虹,void release(struct inode*inode,struct file*filp) MOD_DEC_USE_COUNT; / 该模块的用户数目减1 printk(This module is in release!n); return 0; #ifdef DEBUG,模块编程实验 _ 21 成都信息工程学院 徐虹,printk(release(%p,%p)n,inode,filp); #endif int read(struct

12、 inode*inode,struct file*filp,char*buf,int count) int leave; if(verify_area(VERIFY_WRITE,buf,count) = DEFAULT) return DEFAULT; for(leave=count;leave0;leave -) ,模块编程实验 _ 22 成都信息工程学院 徐虹,_ _put_user(1,buf,1); buf +; return count; Int write(struct inode*inode,struct file*filp,const char*buf,int count) return count; ,模块编程实验 _ 23 成都信息工程学院 徐虹,6.4.4 模块的测试 #mknod /dev/moduledev c major minor rootlinux /#cat /proc/modules | awk $2= moduledev print$1,模块编程实验 _ 24 成都信息工程学院 徐虹,#include #include #include #include main ( ) int i,testmoduledev; char buf10; testmoduledev=open(/dev/moduledev,O_RDWR); if(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论