手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Socket的实现(Linux内核部分)

[日期:2010-08-23] 来源:Linux社区  作者:Linux [字体:大 中 小]

vfs中有4种主要的数据结构:

1 超级块对象,代表一个已安装的文件系统.super_block

2 索引节点对象,代表一个文件.inode

3 目录项对象,代表一个目录项.dentry

4 文件对象,表示一个被进程打开的文件.file

其中每种对象都包含一个操作对象.依次为 super_operations,inode_operations,dentry_operations以及file_operations.各自操作不同的层次.然后我们的文件系统只需要实现这些方法,然后注册到内核就可以了.

接下来我们来看和vfs相应的结构:

第一个就是file_system_type结构,这个结构表示了一个文件系统:

# struct file_system_type { 
#     const char *name; 
#     int fs_flags; 
# ///最关键的函数,得到文件系统的超级块. 
#     int (*get_sb) (struct file_system_type *, int, 
#                const char *, void *, struct vfsmount *); 
#     void (*kill_sb) (struct super_block *); 
# ........................................... 
# };

然后是vfsmount结构,它表示了一个安装点,换句话说也就是一个文件系统实例.

第三个是files_struct结构,它主要是为每个进程来维护它所打开的句柄.这里只需要注意一个就是fd_array和fstable中的 fd的区别.当进程数比较少也就是小于NR_OPEN_DEFAULT(32)时,句柄就会存放在fd_array中,而当句柄数超过32则就会重新分配数组,然后将fd指针指向它(然后我们通过fd就可以取得相应的file结构).

而且files_struct是每个进程只有一个的.

1. struct files_struct { 
   2.   /*
   3.    * read mostly part
   4.    */ 
   5.     atomic_t count; 
   6.     struct fdtable *fdt; 
   7.     struct fdtable fdtab; 
   8.   /*
   9.    * written part on a separate cache line in SMP
  10.    */ 
  11.     spinlock_t file_lock ____cacheline_aligned_in_smp; 
  12.     int next_fd; 
  13.     struct embedded_fd_set close_on_exec_init; 
  14.     struct embedded_fd_set open_fds_init; 
  15. ///所打开的所有文件 
  16.     struct file * fd_array[NR_OPEN_DEFAULT]; 
  17. }; 
  18.  
  19.  
  20. struct fdtable { 
  21.     unsigned int max_fds; 
  22.     struct file ** fd;      /* current fd array */ 
  23.     fd_set *close_on_exec; 
  24.     fd_set *open_fds; 
  25.     struct rcu_head rcu; 
  26.     struct fdtable *next; 
  27. }; 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款