2015-10-19 106 views
1

我很想知道如何在Linux中实现每进程文件/套接字描述符表。具体而言,使用哪种数据结构和算法来实现并保持其效率。文件/套接字描述符表

在此先感谢!

回答

1

由进程打开文件是由结构files_struct,这是在这个过程中的结构的task_struct

struct task_struct { 
    ... 
    /* open file information */ 
    struct files_struct *files; 

每进程文件描述符表(FDT)管理是在结构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 

的过程中得到回报的索引文件的描述符表为打开的文件。

相关问题