apue中:其中进程表项内部的数组又称为 进程打开文件表
另外一个角度:
从linux内核角度开:
task_struct是进程描述符对应上面的进程表项,在task_struct描述符中有struct file_struct*类型的变量file,指向struct file_struct结构。
1.file_struct:该结构体有进程描述符中的files域指向,如下:
struct files_struct { atomic_t count; /* structure‘s usage count */ spinlock_t file_lock; /* lock protecting this structure */ int max_fds; /* maximum number of file objects */ int max_fdset; /* maximum number of file descriptors */ int next_fd; /* next file descriptor number */ struct file **fd; /* array of all file objects */ fd_set *close_on_exec; /* file descriptors to close on exec() */ fd_set *open_fds; /* pointer to open file descriptors */ fd_set close_on_exec_init; /* initial files to close on exec() */ fd_set open_fds_init; /* initial set of file descriptors */ struct file *fd_array[NR_OPEN_DEFAULT]; /* default array of file objects */ };
该结构中有一个struct
file *fd_array[NR_OPEN_DEFAULT]的数组,对应上面的进程打开文件表,其数组的元素指向
struct
file
结构,此结构对应apue中的文件表
2、文件对象:文件对象表示进程以打开的文件。文件对象仅仅在进程观点上代表已打开文件,它反过来指向目录项对象(反过来指向索引节点),其实只有目录项对象才表示已打开的实际文件。虽然一个文件对应的文件对象不是唯一的,但对应的索引节点和目录项对象无疑是唯一的。文件对象由file结构表示,定义在文件linux/fs.h中,如下:
struct file { struct list_head f_list; /* list of file objects */ struct dentry *f_dentry; /* associated dentry object */ struct vfsmount *f_vfsmnt; /* associated mounted fs */ struct file_operations *f_op; /* file operations table */ atomic_t f_count; /* file object‘s usage count */ unsigned int f_flags; /* flags specified on open */ mode_t f_mode; /* file access mode */ loff_t f_pos; /* file offset (file pointer) */ struct fown_struct f_owner; /* owner data for signals */ unsigned int f_uid; /* user‘s UID */ unsigned int f_gid; /* user‘s GID */ int f_error; /* error code */ struct file_ra_state f_ra; /* read-ahead state */ unsigned long f_version; /* version number */ void *f_security; /* security module */ void *private_data; /* tty driver hook */ struct list_head f_ep_links; /* list of eventpoll links */ spinlock_t f_ep_lock; /* eventpoll lock */ struct address_space *f_mapping; /* page cache mapping */ };
文件表项中包括了文件描述符的状态标志,文件的偏移量(或者说文件的位置),和v节点指针(unix独有)
struct
file 包括我们通常说的文件描述符引用计数,文件偏移量,文件模式等等以系列核心功能,同时又指向文件file_operation结构的指针
struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char *, size_t, loff_t *); ssize_t (*aio_read) (struct kiocb *, char *, size_t, loff_t); ssize_t (*write) (struct file *, const char *, size_t, loff_t *); ssize_t (*aio_write) (struct kiocb *, const char *, 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); int (*aio_fsync) (struct kiocb *, int); 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 flags); int (*dir_notify) (struct file *filp, unsigned long arg); int (*flock) (struct file *filp, int cmd, struct file_lock *fl); };
struct file包括指向struct
dentry 的指针
struct dentry { atomic_t d_count; /* usage count */ unsigned long d_vfs_flags; /* dentry cache flags */ spinlock_t d_lock; /* per-dentry lock */ struct inode *d_inode; /* associated inode */ struct list_head d_lru; /* unused list */ struct list_head d_child; /* list of dentries within */ struct list_head d_subdirs; /* subdirectories */ struct list_head d_alias; /* list of alias inodes */ unsigned long d_time; /* revalidate time */ struct dentry_operations *d_op; /* dentry operations table */ struct super_block *d_sb; /* superblock of file */ unsigned int d_flags; /* dentry flags */ int d_mounted; /* is this a mount point? */ void *d_fsdata; /* filesystem-specific data */ struct rcu_head d_rcu; /* RCU locking */ struct dcookie_struct *d_cookie; /* cookie */ struct dentry *d_parent; /* dentry object of parent */ struct qstr d_name; /* dentry name */ struct hlist_node d_hash; /* list of hash table entries */ struct hlist_head *d_bucket; /* hash bucket */ unsigned char d_iname[DNAME_INLINE_LEN_MIN]; /* short name */ };
由于目录项并非真正保存在磁盘上,所有目录项没有对应的磁盘数据结构,VFS根据字符串形式的路径名现场创建它,目录项结构体也没有是否被修改的标志。目录项对象有三种状态:被使用,未被使用和负状态。一个被使用的目录项对应一个有效的索引节点(即d_inode指向相应的索引节点)并且该对象存在一个或多个使用者(即d_count为正值)。一个未被使用的目录项对应一个有效的索引节点(d_inode指向一个索引节点),但是VFS当前并未使用它(d_count为0)。该目录项对象仍然指向一个有效对象,而且被保留在内存中以便需要时再使用它。显然这样要比重新创建要效率高些。一个负状态的目录项没有对应的有效索引节点(d_inode为NULL).因为索引节点已被删除了,或路径不再正确了,但是目录项仍然保留,以便快速解析以后的路径查询。虽然负的状态目录项有些用处,但如果需要的话话,还是可以删除的,可以销毁它。
只是路径对应的innode节点