Linux fork()ing 时是否共享文件描述符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4277289/
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
Are file descriptors shared when fork()ing?
提问by
Let's say I open a file with open()
. Then I fork()
my program.
假设我用open()
. 然后fork()
我的程序。
Will father and child now share the same offset for the file descriptor?
父子现在会共享相同的文件描述符偏移量吗?
I mean if I do a write in my father, the offset will be changed in child too?
我的意思是,如果我在我父亲中写入,那么孩子中的偏移量也会改变吗?
Or will the offsets be independent after the fork()
?
还是在fork()
?之后偏移量是独立的?
采纳答案by Ignacio Vazquez-Abrams
From fork(2)
:
来自fork(2)
:
* The child inherits copies of the parent's set of open file descrip- tors. Each file descriptor in the child refers to the same open file description (see open(2)) as the corresponding file descriptor in the parent. This means that the two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).
* The child inherits copies of the parent's set of open file descrip- tors. Each file descriptor in the child refers to the same open file description (see open(2)) as the corresponding file descriptor in the parent. This means that the two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).
回答by Martin v. L?wis
They do share the same offset.
它们确实共享相同的偏移量。