本文介绍了对于本地IPC,POSIX消息队列(队列)或Unix域(本地)套接字,哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用POSIX消息队列或Unix域套接字进行本地IPC通信更好吗?

Is it better to use POSIX message queues or Unix domain sockets for local IPC communication?

我曾经在机器(不是域)之间使用Unix套接字,并且我记得建立和断开连接会导致套接字在最终消失之前停留一会儿.此外,如果您想进行可靠的"交换,则必须使用TCP或设计应用程序以返回ACK.我不确定这是否也适用于Unix域套接字.

I have worked with Unix sockets between machines (not domain) and I remember that making and breaking the connection would cause sockets to linger awhile before they finally went away. Moreover, if you wanted a "reliable" exchange you either had to use TCP or design the application to return an ACK. I'm not certain if this also applies to Unix domain sockets though.

在我当前的项目中,我们需要本地IPC.我的第一个反应是使用POSIX MQueues,因为我之前已将它们用于本地消息传递.但是,一位同事建议改用Unix域套接字.

In my current project we need local IPC. My first reaction was to use POSIX MQueues, since I've used them before for local messaging. However, a co-worker is suggesting Unix domain sockets instead.

一个人比另一个人好吗,还是编程上的熟悉程度?还是取决于所创建的应用程序?

Is one better than the other, or is it a matter of programming familiarity? Or perhaps its depends upon the application being created?

总的来说,我们正在处理的应用程序遵循客户端/服务器模型.客户端将消息发送到服务器以执行某些操作".但是,客户端不必等待完成"响应-尽管他们确实想知道是否已接收到他们的请求.

At a big picture the application we are working on follows a client/server model. The clients send messages to the server to "do something". However, the client doesn't wait for an "its done" response -- although they do want to know if their request has been received or not.

发送方的基本逻辑是:

connect to server
send request
note if the send worked or not
disconnect from server

一台服务器可以有数百个客户端.

There can be hundreds of clients to the one server.

我们正在运行Linux操作系统的SMP系统(4-8个内核)上执行.

We're are executing on an SMP system (4-8 cores) running the Linux OS.

提前谢谢.

推荐答案

UNIX域套接字不必徘徊"在类似于TIME_WAIT的状态,因为使用等待时间是为了防止来自服务器的杂散数据包.连接仍然在Internet上徘徊.这种担忧不适用于本地.

UNIX domain sockets do not have to "linger" in a TIME_WAIT-like status, since that wait time is used in case there are stray packets from the connection still wandering around the Internet. The concern doesn't apply locally.

UNIX域套接字可以是SOCK_STREAM(例如TCP)或SOCK_DGRAM(例如UDP),并额外保证UNIX域数据报套接字是可靠的并且不会对数据报进行重新排序.

UNIX domain sockets can be either SOCK_STREAM (like TCP) or SOCK_DGRAM (like UDP), with the added guarantee that UNIX domain datagram sockets are reliable and don't re-order datagrams.

如果您想确定您的其他应用程序已经阅读了您发送的消息,您仍然需要某种ACK(甚至使用TCP都需要).毕竟,即使send()成功了,它也有可能在有机会处理该消息之前崩溃了. (这也适用于消息队列-为完全确保消息不会丢失,接收应用程序必须将请求写入日志,将其刷新到磁盘,然后发送回确认.)

You will still need some kind of ACK (you do even with TCP) if you want to be certain that your other application has read the message you sent; after all, even if the send() succeeded it may have crashed before it had a chance to process the message. (This applies to message queues too - to be totally sure that a message won't be lost, the recieving application must write the request to a journal, flush that to disk and then send back an acknowledgement).

我同意,选择本质上是与编程熟悉有关的问题.

I agree that the choice is essentially a matter of programming familiarity.

这篇关于对于本地IPC,POSIX消息队列(队列)或Unix域(本地)套接字,哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 08:53