文章目录
- Linux系统编程,Linux中的文件读写操作
- 1.open函数,打开文件
Linux系统编程,Linux中的文件读写操作
1.open函数,打开文件
我们来看下常用的open函数
这个函数最终返回一个文件描述符struct file
我们查看一下它的Ubuntu内核源代码
struct file {union {struct list_head fu_list; //文件对象链表指针linux/include/linux/list.hstruct 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时指定的flagmode_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,所有者组idstruct file_ra_state f_ra; //在linux/include/linux/fs.h中定义,文件预读相关unsigned long f_version;//记录文件的版本号,每次使用之后递增#ifdef CONFIG_SECURITYvoid *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;};
我们使用open函数的时候,传入的参数就会被记录在内核中,
int open(const char *pathname, int flags, mode_t mode);
返回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 **);
};