《实验09编写设备驱动程序.ppt》由会员分享,可在线阅读,更多相关《实验09编写设备驱动程序.ppt(8页珍藏版)》请在三一办公上搜索。
1、实验内容,编写设备驱动程序,文件操作数据结构,struct file_operations int(*seek)(struct inode*,struct file*,off_t,int);int(*read)(struct inode*,struct file*,char,int);int(*write)(struct inode*,struct file*,off_t,int);int(*readdir)(struct inode*,struct file*,struct dirent*,int);int(*select)(struct inode*,struct file*,int,se
2、lect_table*);int(*ioctl)(struct inode*,struct file*,unsined int,unsigned long);int(*mmap)(struct inode*,struct file*,struct vm_area_struct*);int(*open)(struct inode*,struct file*);int(*release)(struct inode*,struct file*);int(*fsync)(struct inode*,struct file*);int(*fasync)(struct inode*,struct file
3、*,int);int(*check_media_change)(struct inode*,struct file*);int(*revalidate)(dev_t dev);,相关说明,这个结构的每一个成员的名字都对应着一个系统调用。用户进程利用系统调用在对设备文件进行诸如read/write操作时,系统调用通过设备文件的主设备号找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着把控制权交给该函数。这是linux的设备驱动程序工作的基本原理。所以,编写设备驱动程序的主要工作就是编写子函数,并填充file_operations的各个域。,mydevice.c程序示例,重新编译内核,把mydevice.c放在相应的字符设备驱动程序所在目录,并修改该目录的Makefile文件。,添加设备文件,mknod/dev/mydevice c 231 0,测试,main()int testdev;int i;char buf10;testdev=open(/dev/mydevice,O_RDWR);if(testdev=-1)printf(Cannt open file n);exit(0);read(testdev,buf,10);for(i=0;i close(testdev);编译运行,看看是不是打印出全1?,小结,