进程可以打开或关闭文件,文件属于系统资源,Linux内核要对进程使用文件的情况进行记录。task_struct结构中有两个数据结构用于描述进程与文件相关的信息。其中,fs_struct中描述了两个VFS索引节点(VFS inode),这两个索引节点叫做root和pwd,分别指向进程的可执行映象所对应的根目录(home directory)和当前目录或工作目录。file_struct结构用来记录了进程打开的文件的描述符(descriptor)
1.数据结构图解
2.struct fs_struct详解
fs_struct主要描述文件的位置信息
struct fs_struct {
int users;
rwlock_t lock;
int umask;
int in_exec;
struct path root, pwd; //分别描述了进程最常用到的两个目录的信息
};
struct path {
struct vfsmount *mnt;
struct dentry *dentry;
};
lock:用于表中字段的读/写自旋锁
umask:当打开文件设置文件权限时所使用的位掩码
root:根目录所安装的文件系统对象,根目录的目录项
pwd:当前工作目录安装的文件系统对象,当前工作目录的目录项
3.struct files_struct
/*
* Open file table structure
*/
struct files_struct {
/*
* read mostly part
*/
atomic_t count; //共享该表的进程数
struct fdtable *fdt;
struct fdtable fdtab;
/*
* written part on a separate cache line in SMP
*/
spinlock_t file_lock ____cacheline_aligned_in_smp; /* 用于表中字段的读/写自旋锁 */
int next_fd;
struct embedded_fd_set close_on_exec_init; /* 执行exec() 时需要关闭的文件描述符的集合 */
struct embedded_fd_set open_fds_init;
struct file * fd_array[NR_OPEN_DEFAULT]; /* 文件对象指针的初始化数组 */
};
struct fdtable {
unsigned int max_fds;
struct file ** fd; /* current fd array */
fd_set *close_on_exec;
fd_set *open_fds;
struct rcu_head rcu;
struct fdtable *next;
};
fd字段指向文件对象指针数组。该数组的长度存放在max_fds中。通常,fd字段指向files_struct的fd_array字段,该字段包含32个文件对象指针。如果进程打开的文件数目多于32个,内核就分配一个新的、更大的文件指针数组,并将其地址放在fd中,内核也同时更新max_fds字段的值。
对于在fd数组中有元素的每个文件来说,数组的索引就是文件描述符。Unix进程将文件描述符作为主文件标识符。两个文件描述符可以指向同一个打开的文件。 通常, 数组的第一个元素( 索引为 0) 是进程的标准输入文件, 数组的第二个元素( 索引为 1) 是进程的标准输出文件, 数组的第三个元素( 索引为 2) 是进程的标准错误文件( 参见图 8.3)。 请注意, 借助于 dup( )、 dup2( )和 fcntl( ) 系统调用, 两个文件描述符就可以指向同一个打开的文件, 也就是说, 数组的两个元素可能指向同一个文件对象。
4.struct file
struct file {
/*
* fu_list becomes invalid after file_free is called and queued via
* fu_rcuhead for RCU freeing
*/
union {
struct list_head fu_list; /*所有打开的文件结构形成一个链表*/
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path; /*指向相关目录项和安装的文件系统对象*/
#define f_dentry f_path.dentry
#define f_vfsmnt f_path.mnt
const struct file_operations *f_op; /*指向文件操作表*/
spinlock_t f_lock; /* f_ep_links, f_flags, no IRQ */
atomic_long_t f_count; /*使用该结构的进程数*/
unsigned int f_flags;
fmode_t f_mode; /*文件打开模式*/
loff_t f_pos; /*文件位置*/
struct fown_struct f_owner;
const struct cred *f_cred;
struct file_ra_state f_ra;
u64 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;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};