Linux 文件系统、进程和打开文件 Table

Linux File System, Process and Open File Table

我对进程和打开文件有点困惑 tables。

我知道如果 2 个进程尝试打开同一个文件,打开的文件中将有 2 个条目 table。我正在尝试找出原因。

当 2 个不同的进程尝试访问同一个文件时,为什么在打开的文件 table 中创建了 2 个条目?为什么不能用 1 个条目完成?

我不太明白你说的 "file tables" 是什么意思。 Linux 内核中没有通用结构,称为 "file tables"。

/etc/fstab,代表"filesystem table",列出系统启动时自动挂载的文件系统。

您在此问题中包含的 "filetable" Stack Overflow 标记用于 SQL 服务器,与 Linux 没有直接连接。

当您谈论打开的文件时,您指的听起来像是 links。参见 Hard and soft link mechanism。当文件在 Linux 中打开时,内核会维护文件的另一个硬 link。这就是为什么您实际上可以删除一个打开的文件并且系统将继续 运行 正常。只有当应用程序关闭文件时,磁盘上的 space 才会真正标记为空闲。

因此对于文件系统上的每个索引节点(一个索引节点通常就是我们认为的文件),通常有多个 links--一个目录中的每个条目一个,一个目录中的每个条目一个应用程序打开文件的时间。

更新:这是启发这个问题的网页引用:

Each file table entry contains information about the current file. Foremost, is the status of the file, such as the file read or write status and other status information. Additionally, the file table entry maintains an offset which describes how many bytes have been read from (or written to) the file indicating where to read/write from next.

因此,要直接回答问题 "Why there are 2 entries created in the open file table when 2 different processes try to reach the same file?",需要 2 个条目,因为它们可能包含不同的信息。一个进程可能以只读方式打开文件,而另一个以读写方式打开文件。而且每个进程的文件偏移量(文件中的位置)几乎肯定会不同。