文档描述符
文档描述符(File descriptor)是计算机科学中的一个术语,是一个用于表述指向文档的引用的抽象化概念。
文档描述符在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文档的记录表。当进程打开一个现有文档或者创建一个新文档时,内核向进程返回一个文档描述符。在进程设计中,一些涉及底层的进程编写往往会围绕着文档描述符展开。但是文档描述符这一概念往往只适用于UNIX、Linux这样的操作系统。
文档描述符的优点主要有两个:
例如,下面的代码就示范了如何基于文档描述符来读取当前目录下的一个指定文档,并把文档内容打印至Console中。
![File descriptors for a single process, file table and inode table. Note that multiple file descriptors can refer to the same file table entry (e.g., as a result of the dup system call)[3]:104 and that multiple file table entries can in turn refer to the same inode (if it has been opened multiple times; the table is still simplified because it represents inodes by file names, even though an inode can have multiple names). File descriptor 3 does not refer to anything in the file table, signifying that it has been closed.](/uploads/202501/13/File_table_and_inode_table.svg3721.png)


