[深刻理解文件系统之二] 文件描述符、inode和打开文件表

Unix文件系统学习笔记之二: 文件描述符、inode和打开文件表node



  • 系统盘上数据的布局数组



    文件系统无非是关于数据在磁盘上的组织以及存储空间管理的,为此,首先须要知道磁盘上数据的整体布局方式。以Unix为例,最重要的一张表以下:缓存

wKiom1ith2uiNQk2AACQA04dGbg056.png



  • Unix 进程管理中和用户文件、io 最相关的数据结构:usr 数据结构
    数据结构


The procstructure does not record information related to file access.  However the userstructure contains a number of important  file-access-related fields, namely:ide

u_cdir. The inode of the current working directory is stored here. This is布局

used during pathname resolution when a user specifies a relative学习

pathname.ui

u_uid/u_gid. The process user ID and group ID used for permissionsthis

checking for file-access-based system calls. Similarly, u_euidandspa

u_egidhold the effective user and group IDs.

u_ofile. This array holds the process file descriptors. This is described in

more detail later.

u_arg. An array of system call arguments set up during the transition

from user to kernel mode when invoking a system call.

u_base. This field holds the address of a user space buffer in which to read

data from or write data to when processing a system call such as read()

orwrite().

u_count. The number of bytes to read or write is held here. It is

decremented during the I/O operation and the result can be passed back

to the user.

u_offset. This field records the offset within the file for the current read

or write operation.

u_error. When processing a system call, this field is set if an error is

encountered. The value of u_erroris then passed back to the user

when the system call returns.


  • INODE

    为了深刻理解文件inode、打开文件表、描述符三者之间的关系,首先就须要了解indoe。

深入理解inode就是在理解meta-data,inode是文件系统中最重要的meta data, 它的主要数据结构以下

(注意,它的原始数据存储在非易失性的器件上,文件系统启动以后,访问到的或者常常访问的inode被读到内存里面,这一部分被称为inode in-core。)


Each file in the filesystem was represented by a unique inode that contained fields such as:

i_mode. This field specifies whether the file is a directory (IFDIR), a block

special file (IFBLK), or a character special file (IFCHR). Note that if one

of the above modes was not set, the file was assumed to be a regular file.

This would later be replaced by an explicit flag, IFREG.

i_nlink. This field recorded the number of hard links to the file. When

this field reaches zero, the inode is freed.

i_uid. The file’s user ID.

i_gid. The file’s group ID.

i_size. The file size in bytes.

i_addr. This field holds block addresses on disk where the file’s data blocks are held.

i_mtime. The time the file was last modified.


关于inode的操做,须要考虑如下方面:


Inode in core/memory

a.什么时候从磁盘读入到内存: 打开的时候须要读入inode;

b.什么时候从内存写入到磁盘:若是对inode有任何更新,好比新申请了块、释放了块

c.什么时候能够写入到磁盘: 文件已经关闭,而且没有任何进程打开了inode对应的文件

d.哪些inode须要缓存:DNLC (directory name lookup cache for vnode)


  • 打开文件表

由此天然引出一个问题,如何表示进程打开的一个文件,或者说操做系统如何记录一个打开的文件呢?若是你去设计,你会怎么作?

a.它必然包含inode的部分信息(或者所有)

b.它必然包含运行时的信息(读写指针的偏移,读写buffer的位置,以及它所对应的inode指针?

固然还必须包含引用计数、打开的方式flag)


在unix系统中,正是这样实现的, 下面就是unix用来记录一个打开文件的信息:

wKiom1itiFOSPF_vAAEtqbDTNC0294.png

上述数据结构file_structure (内存中的就用来在unix中记录进程打开的一个文件的,而若是程序打开了多

个文件,就须要一个file_structure数组。而这偏偏是偏偏是理解文件描述符的关键,文件描述符就是前进程打开的文件列表的索引(index)。


下面这张图前面展现了 文件描述符、打开文件列表和inode的关系:


wKiom1itiBOwys4GAACo-w3gKpM515.png

相关文章
相关标签/搜索