结构体file_operations

      对与应用层的每个系统调用,驱动程序都有一个与之对应的函数.对于字符设备驱动程序,这些函数集合在一个file_operations类型的数据结构中,该结构体在Linux内核的include/linux/fs.h文件中定义.

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);
      int (*lock) (struct file *, int, struct file_lock *);
      ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
      unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
      int (*check_flags)(int);
      int (*flock) (struct file *, int, struct file_lock *);
      ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
      ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
      int (*setlease)(struct file *, long, struct file_lock **);
};

      当应用程序使用open、read、write、ioctl等系统调用时,驱动程序的file_operations结构体中的成员函数就会被调用.从这个角度来说,编写字符设备驱动程序就是为具体的硬件的file_operations结构体编写各个函数(并不需要全部实现该结构体中的成员).

      在字符驱动程序中有一个初始化函数,在安装驱动程序时会调用它.在初始化函数中,会将驱动程序的file_operations结构体连同其主设备号一起向内核进行注册.对与字符设备使用如下函数进行注册:

      int register_chrdev(unsigned int major ,const char *name ,struct file_operations *fops)

      这样,内核通过/dev目录下的设备文件名找到该设备的主设备号和file_operations结构体,从而找到了系统调用所对应的驱动函数.因此,当应用层使用系统调用函数时,内核就通过这种方法找到了相应的驱动函数.

你可能感兴趣的:(数据结构,struct,Module,File,user,linux内核)