我可以打开一个套接字并将其传递给Linux中的另一个进程吗 [英] Can I open a socket and pass it to another process in Linux

查看:276
本文介绍了我可以打开一个套接字并将其传递给Linux中的另一个进程吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中,我可以打开一个套接字并将该套接字传递给另一个进程吗? 如果是,您能告诉我在哪里可以找到示例吗?

In Linux, is it possible for me to open a socket and pass the socket to another process? If yes, can you please tell me where I can find an example?

谢谢.

推荐答案

是的,可以将sendmsg()与SCM_RIGHTS一起使用,从一个进程到另一个进程:

Yes you can, using sendmsg() with SCM_RIGHTS from one process to another:

SCM_RIGHTS-发送或接收一组 从另一个打开文件描述符 过程.数据部分包含一个 文件描述符的整数数组. 传递的文件描述符的行为与 尽管它们是用 dup(2).

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()之后)继承套接字.未关闭的所有文件句柄(包括套接字)将对子进程可用.因此,子进程继承了父进程的套接字.

That is not the typical usage though. More common is when a process inherits sockets from its parent (after a fork()). Any file handles (including sockets) not closed will be available to the child process. So the child process inherits the parent's sockets.

用于侦听连接的服务器进程称为守护程序.通常,这会分叉到每个新的连接上,从而产生一个进程来处理每个新的请求.典型守护程序的示例在这里:

A server process that listens for connections is called a daemon. This usually forks on each new connection, spawning a process to handle each new request. An example of the typical daemon is here:

http://www.steve.org.uk/Reference/Unix/faq_8.html#SEC88

向下滚动到空白process().

这篇关于我可以打开一个套接字并将其传递给Linux中的另一个进程吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆