[深入理解文件系统之二] 文件描述符、inode和打开文件表
The procstructuredoes not record information related to file access. However the userstructurecontains a number of imp
Unix文件系统学习笔记之二:文件描述符、inode和打开文件表 The procstructuredoes not record information related to file access. However the userstructurecontains a number of important file-access-related fields, namely: u_cdir.The inode of the current working directory is stored here. This is usedduring pathname resolution when a user specifies a relative pathname. u_uid/u_gid.The process user ID and group ID used for permissions checkingfor file-access-based system calls. Similarly, u_euidand u_egidholdthe effective user and group IDs. u_ofile.This array holds the process file descriptors. This is described in moredetail later. u_arg.An array of system call arguments set up during the transition fromuser to kernel mode when invoking a system call. u_base.This field holds the address of a user space buffer in which to read datafrom 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 decrementedduring the I/O operation and the result can be passed back tothe user. u_offset.This field records the offset within the file for the current read orwrite operation. u_error.When processing a system call, this field is set if an error is encountered.The value of u_erroristhen passed back to the user whenthe system call returns. 深刻理解inode就是在理解meta-data,inode是文件系统中最重要的metadata, 它的主要数据结构如下 (注意unix文件系统,它的原始数据存储在非易失性的器件上,文件系统启动之后,访问到的或者经常访问的inode被读到内存里面,这一部分被称为inodein-core。) Eachfile in the filesystem was represented by a unique inode thatcontained fields such as: i_mode.This field specifies whether the file is a directory (IFDIR), a block specialfile (IFBLK), or a character special file (IFCHR). Note that if one ofthe above modes was not set, the file was assumed to be a regularfile. Thiswould later be replaced by an explicit flag, IFREG. i_nlink.This field recorded the number of hard links to the file. When thisfield 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 datablocks are held. i_mtime.The time the file was last modified. 关于inode的操作,需要考虑以下方面: Inodein 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用来记录一个打开文件的信息: 上述数据结构file_structure(内存中的就用来在unix中记录进程打开的一个文件的,而如果程序打开了多 个文件,就需要一个file_structure数组。而这恰恰是恰恰是理解文件描述符的关键,文件描述符就是前进程打开的文件列表的索引(index)。 下面这张图前面展示了文件描述符、打开文件列表和inode的关系: (编辑:海南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |