fork()ing 时是否共享文件描述符? [英] Are file descriptors shared when fork()ing?

查看:34
本文介绍了fork()ing 时是否共享文件描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

解决方案

来自 fork(2):

<块引用>

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

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

解决方案

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)).

这篇关于fork()ing 时是否共享文件描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆