fork() 时文件描述符是否共享?

发布于 2024-10-04 00:59:31 字数 170 浏览 0 评论 0原文

假设我使用 open() 打开一个文件。然后我 fork() 我的程序。

父亲和孩子现在会共享文件描述符的相同偏移量吗?

我的意思是,如果我在父亲中写入,孩子中的偏移量也会改变吗?

或者在 fork() 之后偏移量是独立的吗?

Let's say I open a file with open(). Then I fork() my program.

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()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

标点 2024-10-11 00:59:31

来自fork(2)

 * 子级继承父级打开文件描述集的副本
     托尔。子级中的每个文件描述符都引用同一个打开的文件
     文件描述(参见 open(2))作为相应的文件描述符
     在父级中。这意味着两个描述符共享打开的文件
     状态标志、当前文件偏移量和信号驱动的 I/O 属性
     (参见fcntl(2)中F_SETOWN和F_SETSIG的描述)。

From 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)).
飘过的浮云 2024-10-11 00:59:31

它们确实共享相同的偏移量。

They do share the same offset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文