linux vfs 解析 之 file - 进程相关的信息

来源:互联网 发布:南方舆情数据研究院 编辑:程序博客网 时间:2024/06/19 23:49

http://blog.sina.com.cn/s/blog_5219094a01009a4h.html


进程相关的信息

 

和进程相关的信息, 涉及到四个重要的数据结构:
file, fs_struct, files_struct 和 namespace


相关的数据结构为:


struct file {
 struct list_head f_list;
 struct dentry  *f_dentry;
 struct vfsmount         *f_vfsmnt;
 struct file_operations *f_op;
 atomic_t  f_count;
 unsigned int   f_flags;
 mode_t   f_mode;
 int   f_error;
 loff_t   f_pos;
 struct fown_struct f_owner;
 unsigned int  f_uid, f_gid;
 struct file_ra_state f_ra;

 size_t   f_maxcount;
 unsigned long  f_version;
 void   *f_security;

 
 void   *private_data;

#ifdef CONFIG_EPOLL
 
 struct list_head f_ep_links;
 spinlock_t  f_ep_lock;
#endif
 struct address_space *f_mapping;
};

-------------------------------------------------------------------------------

struct fs_struct {
 atomic_t count;
 rwlock_t lock;
 int umask;
 struct dentry * root, * pwd, * altroot;
 struct vfsmount * rootmnt, * pwdmnt, * altrootmnt;
};

-------------------------------------------------------------------------------

struct files_struct {
        atomic_t count;
        spinlock_t file_lock;    
        int max_fds;
        int max_fdset;
        int next_fd;
        struct file ** fd;     
        fd_set *close_on_exec;
        fd_set *open_fds;
        fd_set close_on_exec_init;
        fd_set open_fds_init;
        struct file * fd_array[NR_OPEN_DEFAULT];
};

-------------------------------------------------------------------------------

struct namespace {
 atomic_t  count;
 struct vfsmount * root;
 struct list_head list;
 struct rw_semaphore sem;
};


每个进程都有自己的namespace.

fs_struct用于表示进程与文件系统之间的结构关系,比如当前的工作目录,进程的根目录等等.

files_struct 用于表示当前进程打开的文件.

而对于每一个打开的文件,由file对象来表示.

                                                                         
                                                                         
         task_struct                                                     
       +-------------+                                                   
       |    fs       |                                                   
       +-------------+                                                   
  /----|    files    |                                                   
  |    +-------------+                                                   
  |    |  namespace  |----> namespace                                    
  |    +-------------+     +---------+                                   
  |    |             |     |  count  |                +-------------+    
  |    +-------------+     +---------+   /----------->| struct file |    
  |    |             |     |  root   |   |            +-------------+    
  |    +-------------+     +---------+   |                               
  |                        |  list   |   |                               
  |                        +---------+   |                               
  |                        |  sem    |   |                +-------------+
  \---> files_struct       +---------+   |   /----------->| struct file |
      +----------------+                 |   |            +-------------+
      |   fd           |-------------\   |   |                           
      +----------------+             |   |   |                           
      |                |             v   |   |                           
      +----------------+       +---+---+---+---+---+---+                 
      |   fd_array     |------>| 0 | 1 | 2 | 3 | ....  |                 
      +----------------+       ----+---+---+---+---+---.                 
      |   max_fds      |---\    `.                  ,-`                  
      +----------------+   |      `',            ,-`                     
 /----|   open_fds     |   |         ``'-...--'``                        
    +----------------+   \--------------/                              
    |   max_fdset    |-----\                                           
    +----------------+     |                                           
                   __,,,,,,\,,,_                                       
           _,.-''``             ```'-.,,                               
         -`----+--+--+--+--+--+---+---+``'-                            
 \-------->|  |  |  |  |  |  |bit map   |   |                            
           +--+--+--+--+--+--+--+---+---+---+                            
                           

 

Linux中,常常用文件描述符(file descriptor)来表示一个打开的文件,这个描述符的值往往是一个大于或等于0的整数.
而这个整数,其实就是在files_struct中file数组fd的下标.
对于所有打开的文件, 这些文件描述符会存储在open_fds的位图中.

 


原创粉丝点击