Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何将fd传递给另一个进程?_Linux_Sockets_Unix - Fatal编程技术网

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

Linux 如何将fd传递给另一个进程?,linux,sockets,unix,Linux,Sockets,Unix,在过程a中有一个fd处理一个fork并调用exec* 域套接字、fifo、管道 有没有一种简单的方法可以让我在新的进程B中获取并使用这个fd?使用fork时,打开的文件描述符会被继承。没有什么你应该做的 发件人: 对于仍然为真的exec(如果您没有将fd标记为close on exec)。From(所有exec*调用都只是此系统调用的包装): 欢迎来到Stack Overflow-很高兴有你。请阅读并帮助将堆栈溢出内容保持在尽可能高的级别,并增加您获得适当答案的机会。进程B有您可以修改的源代码?

过程a中有一个fd处理一个fork并调用exec*

域套接字、fifo、管道


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

使用fork时,打开的文件描述符会被继承。没有什么你应该做的

发件人:

对于仍然为真的exec(如果您没有将fd标记为close on exec)。From(所有exec*调用都只是此系统调用的包装):


欢迎来到Stack Overflow-很高兴有你。请阅读并帮助将堆栈溢出内容保持在尽可能高的级别,并增加您获得适当答案的机会。进程B有您可以修改的源代码?正确引用的好答案,+1我知道它将被继承。但是,child如何知道在exec*之后这个fd应该是什么变量呢?我的意思是,例如,在进程A中fd是3。在fork和exec*之后,我如何才能将这个3传递给child。@umberlajyl:您最后的评论应该包含在问题帖子中。目前,人们可能只会猜测你的问题在于传递一个值,而不是使用它。你并不真正关心这个问题。如果fd在fork之前为3,则在fork之后将保持3,即变量值将保持不变,打开的文件描述符编号将保持不变。不向子级传递任何内容,如果在fork之前初始化了变量,则子级将具有与父级相同的变量值。(这些变量的地址会发生变化,因为它们是/可能是不同的虚拟地址空间,无论如何,变量的值都会从父级复制到子级地址空间,但这是另一个主题)。
          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)).
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).