• Linux系统编程,Linux中的文件读写文件描述符


    Linux系统编程,Linux中的文件读写操作

    1.open函数,打开文件

    我们来看下常用的open函数

    在这里插入图片描述

    这个函数最终返回一个文件描述符struct file

    我们查看一下它的Ubuntu内核源代码

    struct file {
    
      union {
    
            struct list_head fu_list; //文件对象链表指针linux/include/linux/list.h
    
            struct rcu_head fu_rcuhead; //RCU(Read-Copy Update)是Linux 2.6内核中新的锁机制
    
       } f_u;
    
      struct path f_path; //包含dentry和mnt两个成员,用于确定文件路径
    
      #define f_dentry f_path.dentry //f_path的成员之一,当前文件的dentry结构
    
      #define f_vfsmnt f_path.mnt //表示当前文件所在文件系统的挂载根目录
    
      const struct file_operations *f_op; //与该文件相关联的操作函数
    
      atomic_t f_count; //文件的引用计数(有多少进程打开该文件)
    
      unsigned int f_flags; //对应于open时指定的flag
    
      mode_t f_mode; //读写模式:open的mod_t mode参数
    
           loff_t     f_pos;//当前文件指针位置
    
      off_t f_pos; //该文件在当前进程中的文件偏移量
    
      struct fown_struct f_owner; //该结构的作用是通过信号进行I/O时间通知的数据。
    
      unsigned int f_uid, f_gid;// 文件所有者id,所有者组id
    
      struct file_ra_state f_ra; //在linux/include/linux/fs.h中定义,文件预读相关
    
      unsigned long f_version;//记录文件的版本号,每次使用之后递增
    
      #ifdef CONFIG_SECURITY
    
           void *f_security;
    
      #endif
    
      /* needed for tty driver, and maybe others */
    
      void *private_data;//使用这个成员来指向分配的数据
    
      #ifdef CONFIG_EPOLL
    
      /* Used by fs/eventpoll.c to link all the hooks to this file */
    
          struct list_head f_ep_links;
    
          spinlock_t f_ep_lock;
    
      #endif /* #ifdef CONFIG_EPOLL */
    
      struct address_space *f_mapping;
    
      };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    我们使用open函数的时候,传入的参数就会被记录在内核中,

    int open(const char *pathname, int flags, mode_t mode);
    
    • 1

    返回int就是对应的file在数组中的位置,struct file[],

    flags --》unsigned int f_flags; //对应于open时指定的flag

    mode --》mode_t f_mode; //读写模式:open的mod_t mode参数

    loff_t f_pos;//当前文件指针位置,文件读写的时候的偏移地址

    我们看下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 *, 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 **);
    };
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    Gitea 仓库事件触发Jenkins远程构建
    Python 获取两个数组中各个坐标点对之间最短的欧氏距离
    110. 平衡二叉树
    常用的开源网关 API Gateway
    CSRF 跨站请求伪造
    激光雷达反射率标定可提高自动驾驶道路安全
    计算机毕业设计django基于python爬虫系统(源码+系统+mysql数据库+Lw文档)
    图像信号处理板设计原理图:2-基于6U VPX的双TMS320C6678+Xilinx FPGA K7 XC7K420T的图像信号处理板
    不要轻易更换zotero同步的官网账号
    多态概述、多态中成员变量的访问原则、引用数据类型的向上向下转型、多态的应用、包和访问权限
  • 原文地址:https://blog.csdn.net/weixin_46039528/article/details/134356382