如何将fd传递给另一个进程?

时间:2018-04-09 08:39:03

标签: linux sockets unix

进程A 中有 fd 处理A 分叉并调用 exec *。

域套接字,fifo,pipe。

是否有一种简单的方法可以让我在新的 进程B 中使用 fd

1 个答案:

答案 0 :(得分:1)

使用fork时会继承打开的文件描述符。你什么都不应该。

来自fork manpage

          The child inherits copies of the parent's set of open file
          descriptors.  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 file
          descriptors share open file status flags, file offset, and signal-
          driven I/O attributes (see the description of F_SETOWN and
          F_SETSIG in fcntl(2)).

至于仍然适用的执行官(如果你没有将fd标记为接近执行)。从execve man page开始(所有exec *调用只是这个系统调用的包装器):

By default, file descriptors remain open across an execve(). File descriptors that are marked close-on-exec are closed; see the description of FD_CLOEXEC in fcntl(2).