struct files_struct

内核利用文件描述符(file descriptor)来访问文件。文件描述符是非负整数。它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。

文件描述符的操作(如: open)返回的是一个文件描述符,内核会在每个进程空间中维护一个文件描述符表, 所有打开的文件都将通过此表中的文件描述符来引用; 
struct files_struct
  对于每个进程,包含一个files_struct结构,用来记录文件描述符的使用情况,定义在include/linux/file.h中
  struct files_struct
  {
  atomic_t count; 使用该表的进程数
  struct fdtable *fdt;
  struct fdtable fdtab;
  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]; 默认打开的fd队列
  };
  struct fdtable {
  unsigned int max_fds;
  struct file ** fd; 指向打开的文件描述符列表的指针,开始的时候指向fd_array,
  当超过max_fds时,重新分配地址
  fd_set *close_on_exec; 执行exec需要关闭的文件描述符位图(fork,exec即不被子进程继承的文件描述符)
  fd_set *open_fds; 打开的文件描述符位图
  struct rcu_head rcu;
  struct fdtable *next;
  };

原文地址:https://www.cnblogs.com/Blog-day/p/My_Blog_Days_13.html