zoukankan      html  css  js  c++  java
  • [文件系统]文件系统学习笔记(二)---task_struct

    一,task_struct和文件系统相关的一些信息

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. <sched.h>  
    2. struct task_struct {  
    3.     ...  
    4.     /* file system info */  
    5.     int link_count, total_link_count;  
    6.     ...  
    7.     /* filesystem information */  
    8.     struct fs_struct *fs;  
    9.     /* open file information */  
    10.     struct files_struct *files;  
    11.     /* namespaces */  
    12.     struct nsproxy *nsproxy;  
    13.     ...  
    14. }  

    进程的文件系统相关的数据保存在fs中,这些数据包含当前的工作目录

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. fs_struct主要用于管理特定进程本身的一些信息,  
    2. struct fs_struct {  
    3.     atomic_t count;  
    4.     int umask;  //表示标准的掩码,用于设置新文件的权限  
    5.     struct dentry * root, * pwd, * altroot; //root表示该进程所在的根目录,pwd表示当前进程所在的目录,shell的cd命令使用时会改变  
    6.     struct vfsmount * rootmnt, * pwdmnt, * altrootmnt; //rootmnt表示根目录所在的文件系统,  
    7. };  
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. struct files_struct {  
    2.     atomic_t count; //共享该文件的进程的数目  
    3.     struct fdtable *fdt;  
    4.     struct fdtable fdtab;  
    5.     int next_fd;  //该进程下一次打开新文件的时候使用的文件描述符id  
    6.     struct embedded_fd_set close_on_exec_init;  
    7.     struct embedded_fd_set open_fds_init;  
    8.     struct file * fd_array[NR_OPEN_DEFAULT]; 每个成员都是一个指针,指向每个打开文件struct file实例  
    9. };  


    默认情况下内核运行每个进程打开NR_OPEN_DEFAULT个文件,默认值是BITS_PER_LONG,32位系统上该值为32

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. struct fdtable {  
    2.     unsigned int max_fds; //该进程当前可以处理的文件对象和文件描述符的最大数目  
    3.     struct file ** fd; /* current fd array */  
    4.     fd_set *close_on_exec;  
    5.     fd_set *open_fds;  
    6.     struct rcu_head rcu;  
    7.     struct files_struct *free_files;  
    8.     struct fdtable *next;  
    9. };  


    struct file指向了真正文件的信息,

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. struct file {  
    2.     struct list_head fu_list;  
    3.     struct path f_path;  //指定了文件名和inode之间的关联,文件所在文件系统相关信息  
    4.     #define f_dentry f_path.dentry  
    5.     #define f_vfsmnt f_path.mnt  
    6.     const struct file_operations *f_op;  //文件操作所调用到的各个函数  
    7.     atomic_t f_count;  
    8.     unsigned int f_flags;  
    9.     mode_t f_mode;  
    10.     loff_t f_pos; //表示进程对文件操作的位置  
    11.     struct fown_struct f_owner;  //处理该文件的进程有关信息  
    12.     unsigned int f_uid, f_gid;  //用户的UID GID  
    13.     struct file_ra_state f_ra;  //预读取特征,指定在实际读取数据之前是否预读取  
    14.     unsigned long f_version;  
    15.     ...  
    16.     struct address_space *f_mapping;  //指向属于文件相关的inode实例的地址空间映射  
    17.     ...  
    18. };  
    19.   
    20.   
    21. struct path {  
    22.     struct vfsmount *mnt;  
    23.     struct dentry *dentry;  
    24. };  

    在硬盘上并不存在一个文件结构,进程打开一个文件,内核就动态创建一个文件对象,同一个文件在不同的进程中有不同的文件对象

    附录:

    进程打开一个文件的过程:
    1,用户层的open()函数最终调用了内核里面的sys_open()函数(fs/open.c),sys_open()函数主要借助do_flip_open()函数来完成查找文件的inode,do_flip_open()函数首先调用open_namei()函数,open_namei()函数调用path_lookup()函数查找文件的inode并执行额外几个检查动作,如果是创建新的文件系统项,该函数还需要应用存储在进程umask(current->fs->umask)中的权限位的默认设置。do_flip_open()函数然后调用nameidata_to_flip()函数初始化预读结构,将新创建的file实例放置到超级快的s_files链上并调用底层文件系统的file_operations结构中的open()函数。
    最终控制权转回用户进程,返回文件描述符之前,fd_install必须将file实例放置到进程task_struct的files->fd数组中

  • 相关阅读:
    javascript实现限制上传文件的大小
    js事件
    表格展开伸缩以及ztree异步加载
    二分法
    php操作beanstalkd
    ubuntu安装操作HttpSQS高速队列
    mysql中安全函数
    php操作httpsqs
    jquery操作表单
    ajax长轮询实例
  • 原文地址:https://www.cnblogs.com/zhiliao112/p/4067834.html
Copyright © 2011-2022 走看看