- 论坛徽章:
- 0
|
看了关于task_struct、file、inode等结构,总结了一下很多地方不明白,下面是我的一些理解,不知道对不对,请各位指点一下:
task_struct这个数据结构与文件相关的一个成员有两个
- /* filesystem information */
- struct fs_struct *fs;
- /* open file information */
- struct files_struct *files;
复制代码
其中:
struct fs_struct主要是作为文件系统的一些内容,包括根目录、当前工作目录、mount的文件系统信息、初始化umask等。
struct files_struct存储了当前进程所有打开的文件信息:
- /*
- * Open file table structure
- */
- struct files_struct {
- atomic_t count;
- spinlock_t file_lock; /* Protects all the below members. Nests inside tsk->alloc_lock */
- int max_fds;
- int max_fdset;
- int next_fd;
- struct file ** fd; /* current fd array */
- fd_set *close_on_exec;
- fd_set *open_fds;
- fd_set close_on_exec_init;
- fd_set open_fds_init;
- struct file * fd_array[NR_OPEN_DEFAULT];
- };
复制代码
其中包括了指向文件对象数组的指针struct file ** fd,文件对象file是直接和文件内容相关联的
- struct file {
- struct list_head f_list;
- struct dentry *f_dentry;
- struct vfsmount *f_vfsmnt;
- struct file_operations *f_op;
- atomic_t f_count;
- unsigned int f_flags;
- mode_t f_mode;
- int f_error;
- loff_t f_pos;
- struct fown_struct f_owner;
- unsigned int f_uid, f_gid;
- struct file_ra_state f_ra;
-
- unsigned long f_version;
- void *f_security;
-
- /* 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;
- };
复制代码
其中最重要的是struct file_operations *f_op,描述了对文件对象的操作,也就是读写文件的一些注册处理函数
- 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 (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
- ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
- ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, 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);
- int (*mmap) (struct file *, struct vm_area_struct *);
- int (*open) (struct inode *, struct file *);
- int (*flush) (struct file *);
- 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 (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
- ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
- ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
- 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 (*dir_notify)(struct file *filp, unsigned long arg);
- int (*flock) (struct file *, int, struct file_lock *);
- };
复制代码
再有一个重要的结构就是inode,文件系统管理一个文件的所有信息都存储在inode结构中,是一个静态的东西,即只要文件存在其相应的inode信息就存在,文件的名字可以改变,但是其inode号是不会变的。而上面的file结构是为了和打开文件进程交互的,只有当文件被某一个进程打开的时候才创建一个file结构,如果同一个文件被不同的进程分别打开,那么内存中会维护两个不同的file结构,file结构中的f_op初始化的时候来自inode的i_fop,进程这个时候才可以对文件进行基本的读写操作。这个时候可以修改f_op中的注册函数地址为我们自己的函数,就可以修改文件的操作函数集。
上面是我自己这几天看书的一些理解,不知道对不对! |
|