linux - 我可以打开一个套接字并将其传递给 Linux 中的另一个进程吗

标签 linux sockets

在 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 with dup(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/

相关文章:

winapi - 是否可以在Reactor风格的操作中使用IOCP(或其他API)?

linux - Qt5 designer 是否可用于 linux?

linux - 在 Linux 中上下文切换需要多长时间?

web-services - Web 浏览器如何知道请求的 url 是 https 还是 http

c# - 无法分配新套接字,抛出无效参数异常

c - 无法设置套接字选项 IPV6 多播 : No such device error

python - 处理 Python 套接字中的超时错误

linux - GDB 核心转储 : Find executable by build-id

php - 如何授予我的 Laravel 应用程序在目录中创建子文件夹的权限 (Ubuntu)

linux - 启动后自动运行 flask 应用程序无法正常工作