在不同进程之间传递文件描述符的可移植方式 [英] Portable way to pass file descriptor between different processes

查看:123
本文介绍了在不同进程之间传递文件描述符的可移植方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数UNIX系统上,可以通过fork()为子/父进程轻松地在进程之间传递打开文件.但是我需要在孩子已经被分叉之后分享" fd.

On most UNIX systems passing an open file between processes can be easily done for child/parent processes by fork(); however I need to share a fd "after" the child was already forked.

我找到了一些网页告诉我sendmsg()可能适用于任意进程;但这似乎非常依赖于操作系统且非常复杂. portlisten 似乎是我能找到的最好的例子,但是我更喜欢像libevent这样的好包装器库,它可以隐藏kqueue,pool,...的所有魔力.

I've found some webpages telling me that sendmsg() may work for arbitary processes; but that seems very OS dependent and complex. The portlisten seems like the best example I can find, but I'd prefer a good wrapper library like libevent that hides all the magic of kqueue, pool, ....

有人知道是否有一些库(和可移植的方式)来做到这一点?

Does anyone know if there's some library (and portable way) to do this?

推荐答案

您最好的选择是尝试通过Unix域套接字发送文件描述符.斯蒂芬斯(Stephens)以及网络上的一些地方对此都有描述,但是如果您问的不错的话,我可以为您挖掘代码.

Your best bet is to try sending the file descriptor over a Unix domain socket. This is described in Stephens, and in a few places on the web, but I can dig up code for you if you ask nicely.

这几天将非常便于携带;现在,很多东西被认为是不可携带的"(例如mmap!),现在已经非常普遍了.如果您需要比当今大多数系统"更具可移植性,那么您面临许多有趣的问题,但是,如果您能告诉我们更多有关您正在做的事情以及正在使用的平台的信息,也许是非Unix POSIX平台?)我们也许可以提供帮助.

This will be pretty portable these days; a lot of the things considered "non-portable" way back when (such as mmap!) are extremely common now. If you need to be more portable than "most systems these days," you've got a lot of interesting issues ahead of you, but possibly if you tell us more about what you're doing and what platforms you're working on (perhaps non-Unix POSIX platforms?) we might be able to help out.

这篇关于在不同进程之间传递文件描述符的可移植方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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