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

查看:26
本文介绍了我可以打开一个套接字并将其传递给 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() 之后).子进程可以使用任何未关闭的文件句柄(包括套接字).所以子进程继承了父进程的sockets.

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

向下滚动到 void process().

Scroll down to void process().

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

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