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

  • 系统盘上数据的布局



  • 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:

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

used during pathname resolution when a user specifies a relative


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

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

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()


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就是在理解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 in core/memory

a.何时从磁盘读入到内存: 打开的时候需要读入inode;


c.何时可以写入到磁盘: 文件已经关闭,并且没有任何进程打开了inode对应的文件

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

  • 打开文件表





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


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


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