linux系统中三个关键结构体

嵌入式技术

1335人已加入

描述

第一:struct inode结构体

struct inode {
  ······
  struct hlist_node  i_hash;
  struct list_head  i_list;    /* backing dev IO list */
  struct list_head  i_sb_list;


  //主次设备号
  dev_t      i_rdev;


  struct list_head  i_devices;
  //用联合体是因为该文件可能是块设备文件或者字符设备文件
  union {
    struct pipe_inode_info  *i_pipe;  //管道文件
    struct block_device  *i_bdev;  //块设备文件
    struct cdev    *i_cdev;  //字符设备文件
  };
  
  //私有数据
  void      *i_private; /* fs or device private pointer */
};

功能:struct inode结构体是用来表示一个静态文件的,每个文件都会对应唯一的struct inode结构体,结构体里描述了文件的详细信息。

第二:struct file结构体

struct file {
  union {
    struct list_head  fu_list;
    struct rcu_head   fu_rcuhead;
  } f_u;
  ······
  
  const struct file_operations  *f_op;  //该文件对应的操作方法
  
  unsigned int     f_flags;  
  fmode_t      f_mode;  //打开文件的权限,比如:只读打开、只写打开、读写打开
  loff_t      f_pos;  //文件指针的偏移量
  
  /* needed for tty driver, and maybe others */
  void      *private_data;  //私有数据
};

功能:struct file结构体 用来表示一个动态的设备,每当open打开一个文件时就会产生一个struct file结构体 与之对应。

第三:struct 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 *);
  int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
  long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  int (*open) (struct inode *, struct file *);
  int (*release) (struct inode *, struct file *);
};

功能:struct file_operations结构体是用来描述设备的操作方法的。在linux中一切皆是文件,不管是普通文件还是设备驱动文件上层应用都是用同一的open、read函数去操作。每一个struct file结构体 中都保存了一个struct file_operations结构体,虽然上层应用都是调用的read函数,但是在内部实际通过函数指针调用了不同的read函数。

第四:struct cdev结构体

struct cdev {
  struct kobject kobj;
  struct module *owner;
  const struct file_operations *ops; //设备的操作方法
  struct list_head list;
  dev_t dev;  //主次设备号
  unsigned int count;  //引用计数
};

第五:结构体之间的关联

1、struct inode结构体和struct file结构体

(1)struct inode结构体和struct file结构体 都是用来描述文件信息的,struct inode结构体是描述静态的文件,struct file结构体描述动态的文件(也就是打开的文件);

(2)每个文件只有唯一的struct inode结构体,但是可以有多个struct file结构体,文件每被打开一次就多一个 struct file结构体 ;

2、struct file_operations结构体和struct cdev结构体

(1)struct file_operations结构体描述设备的操作方法,struct cdev结构体描述字符设备全部的信息;

(2)struct cdev结构体包含struct file_operations结构体,在注册驱动时需要将struct file_operations结构体指针赋值给struct cdev结构体;

3、struct inode结构体和struct cdev结构体

(1)上层应用访问设备驱动是通过设备节点,设备节点就是一个文件,在创建设备节点时需要指明主次设备号,主次设备号就会保存在设备节点对应的 struct inode结构体的i_rdev变量中;

(2)在向内核注册字符设备驱动时就是将对应的struct cdev结构体注册到chrdevs全局变量中,其中struct cdev结构体就保存了主次设备号;

(3)struct inode结构体的联合体中有struct cdev结构体指针,将来找到对应的struct cdev结构体会对该指针赋值;

(4)联系:在用open打开设备节点时从struct inode结构体中获取初次设备号,然后用这个主次设备号去chrdevs全局变量中找到对应的struct cdev结构体。

总结:依靠主次设备号联系起来。

编辑:黄飞

 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分