我很想知道每进程文件/套接字描述符表是如何在Linux中实现的。具体来说,是使用哪种数据结构和算法来实现它并保持它的效率。
提前感谢!
进程打开的文件由struct files_struct管理,该struct位于进程的struct task_struct
struct task_struct {
...
/* open file information */
struct files_struct *files;
每个进程的文件描述符表(fdt)在struct files_struct
struct files_struct {
...
struct fdtable __rcu *fdt;
当一个进程试图打开一个文件时,它发出一个打开的系统调用。它将调用sys_open。这基本上是代码流:
sys_open(filename, …)
// 1) copy filename from user space
getname(filename)
strncpy_from_user()
// 2) get first unused file descriptor (will be returned to process)
int fd = get_unused_fd()
struct files_struct *files = current->files
// 3) get file from filesystem
struct file *f = file_open(filename)
open_namei
// lookup operation for filesystem
dentry = cached_lookup or real_lookup
// initializes file struct
dentry_open
// 4) install file returned by filesystem into file descriptor table for the process
fd_install
current->files->fd[fd] = file
进程得到打开文件的文件描述符表的索引。