操作系统实验指导书 联系客服

发布时间 : 星期三 文章操作系统实验指导书更新完毕开始阅读d221c4e690c69ec3d4bb757a

include/linux/fs.h中。file_operations的数据结构如下: struct file_operations {

struct module *owner;

loff_t (*llseek) (struct file *, loff_t, int);

ssize_t (*read) (struct file *, char_user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char _user *, size_t, loff_t *);

ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); int (*readdir) (struct file *, void *, filldir_t);

unsigned int (*poll) (struct file *, struct poll_table_struct *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *, fl_owner_t id); int (*release) (struct inode *, struct file *);

int (*fsync) (struct file *, struct dentry *, int datasync); int (*aio_fsync) (struct kiocb *, int datasync); int (*fasync) (int, struct file *, int); ... };

⑴ open 入口点:

open函数负责打开设备、准备I/O。任何时候对设备文件进行打开操作,都会调用设备的open入口点。所以,open函数必须对将要进行的I/O操作做好必要的准备工作,如清除缓冲区等。如果设备是独占的。则open函数必须将设备标记成忙状态。

⑵ close入口点

close函数负责关闭设备的操作,当最后一次使用设备完成后,调用close函数,关闭设备文件。独占设备必须标记为可再次使用。

close()函数作用是关闭打开的文件。 ⑶ read入口点

read函数负责从设备上读数据和命令,有缓冲区的I/O设备操作一般是从缓冲区里读数据。

⑷ write入口点

25

write函数负责往设备上写数据。对于有缓冲区的I/O设备操作,一般是把数据写入缓冲区里。对字符设备文件进行写操作将调用write函数。

⑸ ioctl入口点

ioctl函数执行读、写之外的操作,主要实现对设备的控制。 四、实验步骤

(1) file_operations结构体设计 (2) 模块初始化、模块卸载函数实现 (3) 读写函数的实现 (4) 测试程序编写 (5) 驱动程序编译和加载 (6) 驱动程序测试 实验报告要求: (1) 驱动程序工作理说明

(2) 驱动程序中使用的数据结构及符号说明。 (3) 流程图。

(4) 源程序并附上注释。 (5) 程序运行结果和分析。

五、思考题及其它

块设备驱动程序的设计。

附件

(1)驱动程序string.c

#include #include #include #include #include #include MODULE_LICENSE(\

#define MAJOR_NUM 254 //主设备号

static char str[20]=\ //\设备的数组

static ssize_t string_read(struct file *filp, char *buf,size_t len,loff_t *loff) {

//将str[]中的内容从内核空间复制到用户空间 if (copy_to_user(buf, &str, sizeof(str))) {

return -1; }

26

//读函数 return sizeof(str); }

static ssize_t string_write(struct file *filp,const char *buf,size_t len,loff_t *off) //写函数 {

//将用户空间的数据复制到内核空间的str[]数组中 if(copy_from_user(&str,buf,sizeof(str))) {

return -1; }

return sizeof(str); }

//初始化字符设备驱动的file_operations结构体

struct file_operations string_fops= {

read: string_read, //将标准的读取函数指向对应于设备的具体函数 write: string_write, //将标准的写函数指向对应于设备的具体函数 };

static int __init string_init(void) {

int ret;

//注册设备驱动

ret = register_chrdev(MAJOR_NUM, \ if (ret) {

printk(\ //注册失败 } else {

printk(\ //注册成功 }

return ret; }

static void __exit string_exit(void) {

int ret;

//销毁设备驱动

ret = unregister_chrdev(MAJOR_NUM, \ if (ret) {

printk(\ } else {

printk(\ } }

module_init(string_init); module_exit(string_exit); (2)测试程序test.c: #include #include #include

27

#include //包括文件操作函数,如open()、close()、read()等 main() {

int fd;

char str[20];

//打开”/dev/string”

fd = open(\打开设备文件 if (fd != -1 ) //打开设备文件成功 {

read(fd, &str, sizeof(str)); //初次读str[] printf(\

printf(\ scanf(\

write(fd, &str, sizeof(str)); //写str[]

read(fd, &str, sizeof(str)); //再次读str[] printf(\ //关闭”/dev/string” close(fd); }

else //打开设备文件失败 {

printf(\ } }

(3)调试说明

编译驱动程序string.c(如图1):

图1 编译驱动程序

模块加载(如图2)

28