对与应用层的每个系统调用,驱动程序都有一个与之对应的函数.对于字符设备驱动程序,这些函数集合在一个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结构体,从而找到了系统调用所对应的驱动函数.因此,当应用层使用系统调用函数时,内核就通过这种方法找到了相应的驱动函数.

file_operations结构2的更多相关文章

  1. file_operations结构体解析 1

    注:学了这么长时间了,还没有好好看看 file_operations机构体,这其中还有很多的东西,当你学着学着的时候,就会用到这里面的一些系统调用对应的函数了,我在网上搜索之后,记录如下,一边将来查看 ...

  2. linux驱动开发( 五) 字符设备驱动框架的填充file_operations结构体中的操作函数(read write llseek unlocked_ioctl)

    例子就直接使用宋宝华的书上例子. /* * a simple char device driver: globalmem without mutex * * Copyright (C) 2014 Ba ...

  3. Linux字符设备驱动结构(一)--cdev结构体、设备号相关知识机械【转】

    本文转载自:http://blog.csdn.net/zqixiao_09/article/details/50839042 一.字符设备基础知识 1.设备驱动分类 linux系统将设备分为3类:字符 ...

  4. Smart210学习记录-------Linux设备驱动结构

    cdev结构体 1 struct cdev { 2 struct kobject kobj; /* 内嵌的 kobject 对象 */ 3 struct module *owner; /*所属模块*/ ...

  5. Linux字符设备驱动file_operations

    struct _file_operations struct _file_operations在Fs.h这个文件里面被定义的,如下所示: struct file_operations { struct ...

  6. 2018.5.2 file结构体

    f_flags,File Status Flag f_pos,表示当前读写位置 f_count,表示引用计数(Reference Count): dup.fork等系统调用会导致多个文件描述符指向同一 ...

  7. Framebuffer 驱动学习总结(一) ---- 总体架构及关键结构体

    一.Framebuffer 设备驱动总体架构 帧缓冲设备为标准的字符型设备,在Linux中主设备号29,定义在/include/linux/major.h中的FB_MAJOR,次设备号定义帧缓冲的个数 ...

  8. inode file 结构

    inode位图(inode Bitmap) 和块位图类似,本身占一个块,其中每个bit表示一个inode是否空闲可用. inode表(inode Table) 我们知道,一个文件除了数据需要存储之外, ...

  9. 文件存储结构inode与RAM结构建立联系

    linux下一切皆文件,大致可分为以下几类:目录.普通文件.硬连接.软连接.字符设备.块设备.FIFO.Socket,其在物理存储体内存储按inode和数据块存储,inode代表元数据,是除实际数据外 ...

随机推荐

  1. 第六篇、CSS属性

    <!--1.可继承性 visible(可见的):hidden --掩藏,但是结构还保存 cursor(光标样式):pointer(手指)crosshair(十字架) 一般是文字控制属性 内联标签 ...

  2. Core Animation之CABasicAnimation

    在iOS中,图形可分为以下几个层次: 越上层,封装程度越高,动画实现越简洁越简单,但是自由度越低:反之亦然.本文着重介绍Core Animation层的基本动画实现方案. 在iOS中,展示动画可以类比 ...

  3. 2016.7.2this的应用

    this有三个应用: 1.就是在类的方法中参数与成员参数重名了,那么用this.参数名=参数名来区分它们: 2.当一个引用对象要调用另一个已经有具体实例的引用对象,那么通过在类的定义中后面加publi ...

  4. 02_setter注入

    工程截图如下 [HelloWorld.java] package com.HigginCui; public class HelloWorld { private String words; publ ...

  5. async:false同步请求,浏览器假死

    // 异步请求导致数据错乱 // function get_num(){ // $("input[name='monitor']").eq(1).attr('checked',tr ...

  6. JEECG开发总结

    一:datagrid列表 (1)时间:<t:dgCol title="创建时间" field="createtime" width="60&qu ...

  7. MVC控制器给View返回实体

    前言 这几天把vs12更新到了vs12 5了,因为发现我之前装的12有问题,没有mvc,之后就从itellyou上下载了12的update5更新了一下.说实话,从开发到现在,自己只是平时自己玩用mvc ...

  8. LNMP下防跨站、跨目录安全设置,仅支持PHP 5.3.3以上版本

    PHP 5.3.3以上的版本,可以修改/usr/local/php/etc/php.ini在末尾里加入: [HOST=www.vpser.net] open_basedir=/home/wwwroot ...

  9. 用twisted 做一个日志收集系统

    混沌初开 起初我是不会上logging模块的,直接导致了即时有了日志,我也存到了数据库中,而且量也不大,是否能遇到异常只能靠运气了 开天辟地 不得不说,没有任何输出的线上环境真的挺难调试的,当然,聪明 ...

  10. MVC-起始页面设置

    MVC的URL是通过路由映射的,因为我们可以通过修改RouteConfig来改变应用的起始页面. public class RouteConfig { public static void Regis ...