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

标签 linux sockets unix

进程A中有一个fdprocess A fork 并调用 exec *。

域套接字、fifo、管道。

有没有一种简单的方法可以让我在新的进程 B 中获取和使用这个 fd

最佳答案

打开的文件描述符在使用 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)).

至于 exec,它仍然适用(如果您没有将 fd 标记为 close-on-exec)。来自 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).

关于linux - 如何将 fd 传递给另一个进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49728757/

相关文章:

java - 关于 ServerSocket,需要澄清

sockets - gethostbyname() 只返回本地主机在 linux 上的地址

multithreading - Windows 和 Solaris 10 上的 std::async 性能

linux - Git:如何在不充斥无关紧要的提交的情况下共享存储库?

linux - 什么是文件系统UUID?

linux - 我无法在 centos 7 中打开 robo3t 1.1.1

在 C 中转换转义序列

php - 允许 PHP 更改文件和目录的所有权和权限

python - 无法关闭 KeyboardInterrupt 上的套接字

linux - 脚本不读取文件的最后一行