在 Linux 中,我可以打开一个套接字并将该套接字传递给另一个进程吗? 如果是,您能告诉我在哪里可以找到示例吗?
谢谢。
最佳答案
是的,您可以使用 sendmsg()
和 SCM_RIGHTS 从一个进程到另一个进程:
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 withdup(2)
.
http://linux.die.net/man/7/unix
但这不是典型的用法。更常见的是当进程从其父进程继承套接字时(在 fork()
之后)。子进程可以使用任何未关闭的文件句柄(包括套接字)。所以子进程继承父进程的套接字。
监听连接的服务器进程称为守护进程。这通常会在每个新连接上 fork ,产生一个处理每个新请求的进程。典型守护进程的示例如下:
http://www.steve.org.uk/Reference/Unix/faq_8.html#SEC88
向下滚动到 void process()
。
关于linux - 我可以打开一个套接字并将其传递给 Linux 中的另一个进程吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1997622/