【问题标题】:File/socket descriptor table文件/套接字描述符表
【发布时间】:2015-10-19 03:53:02
【问题描述】:

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

提前致谢!

【问题讨论】:

    标签: linux-kernel file-descriptor


    【解决方案1】:

    进程打开的文件由struct files_struct管理,该struct files_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;
    

    当一个进程试图打开一个文件时,它会发出一个 open 系统调用。这将调用 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
    

    进程获取打开文件的文件描述符表的索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-02
      • 2014-04-25
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多