Linux 两个文件描述符指向同一个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5284062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:14:16  来源:igfitidea点击:

two file descriptors to same file

linuxfileposixfile-descriptor

提问by Michael Xu

Using the posix read() write() linux calls, is it guaranteed that if I write through one file descriptor and read through another file descriptor, in a serial fashion such that the two actions are mutually exclusive of each other... that my read file descriptor will always see what was written last by the write file descriptor?

使用 posix read() write() linux 调用,是否可以保证如果我通过一个文件描述符写入并读取另一个文件描述符,以串行方式使这两个操作相互排斥...读取文件描述符将始终看到写入文件描述符最后写入的内容?

i believe this is the case, but I want to make sure and the man page isn't very helpful on this

我相信是这种情况,但我想确保并且手册页对此不是很有帮助

采纳答案by Chris Dodd

It depends on where you got the two file descriptors. If they come from a dup(2) call, then they share file offset and status, so doing a write(2) on one will affect the position on the other. If, on the other hand, they come from two separate open(2) calls, each will have their own file offset and status.

这取决于您从何处获得两个文件描述符。如果它们来自 dup(2) 调用,则它们共享文件偏移量和状态,因此对一个执行 write(2) 会影响另一个的位置。另一方面,如果它们来自两个单独的 open(2) 调用,则每个调用都有自己的文件偏移量和状态。

A file descriptor is mostly just a reference to a kernel file structure, and it is that kernel structure that contains most of the state. When you open(2) a file, you get a new kernel file structure and a new file descriptor that refers to it. When you dup(2) a file descriptor (or pass a file descriptor through sendmsg), you get a new reference to the same kernel file struct.

文件描述符大多只是对内核文件结构的引用,并且正是该内核结构包含了大部分状态。当您打开(2) 一个文件时,您将获得一个新的内核文件结构和一个引用它的新文件描述符。当您 dup(2) 文件描述符(或通过 sendmsg 传递文件描述符)时,您将获得对同一内核文件结构的新引用。

回答by Erik

This is guaranteed if they both refer to the same file description, aka you got them from "dup" or "dup2" (or inherited via fork()).

如果它们都引用相同的文件描述,即您从“dup”或“dup2”(或通过 继承fork())获得它们,则可以保证这一点。

After a successful return from one of these system calls, the old and new file descriptors may be used interchangeably. They refer to the same open file description (see open(2)) and thus share file offset and file status flags; for example, if the file offset is modified by using lseek(2) on one of the descriptors, the offset is also changed for the other.

从这些系统调用之一成功返回后,旧文件描述符和新文件描述符可以互换使用。它们引用相同的打开文件描述(请参阅 open(2)),因此共享文件偏移量和文件状态标志;例如,如果在其中一个描述符上使用 lseek(2) 修改了文件偏移量,则另一个描述符的偏移量也会更改。

回答by user2742399

when you use dup()or dup2()or fork(), the file table is shared by both of the file descriptors. so if you writesomething from one file descriptor , and again writesomething through other file descriptor , then it is appended not overwritten.

当您使用dup()dup2()或者fork(),文件表是由两个文件描述符共享。所以如果你write从一个文件描述符中得到write一些东西,然后又通过另一个文件描述符得到一些东西,那么它会被追加而不是被覆盖。

but if two independent process open one file , then the data written by both processes may get mixed.

但是如果两个独立的进程打开一个文件,那么两个进程写入的数据可能会混在一起。