gpt4 book ai didi

sockets - 为什么可以使用unix域套接字将fd从 child 转移到父亲?

转载 作者:行者123 更新时间:2023-12-03 11:57:48 26 4
gpt4 key购买 nike

我知道如果unix进程派生,则子进程可以使用父进程的fd。这是继承:但不是相反。
尽管unix的高级编程 Material 说,如果我们使用unix域套接字,则不仅可以将fd从子级转移到父级,而且还可以在进程之间转移。
这很奇怪:

an fd is only meaningful within one process, in different process, same fd value could mean very different.

Then how could unix domain socket really transfer fds between processes? I mean the integer (4 bytes) can be transferred by any means, but it doesn't seem to make sence that one process can operate another process's fd?


有什么解释吗?

最佳答案

您应该真正尝试一下。就像在unix(7)的手册页中所说的那样:

SCM_RIGHTS
Send or receive a set of open file descriptors from another
process. The data portion contains an integer array of the
file descriptors. The passed file descriptors behave as
though they have been created with dup(2).

这意味着接收到的fds不等于按数字发送的fds,它们就像是新创建的fds,但是从内核的角度来看,它们实际上是同一回事。

关于sockets - 为什么可以使用unix域套接字将fd从 child 转移到父亲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46500936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com