UNIX域套接字与共享内存(映射文件)

时间:2022-11-23 22:30:29

Can anyone tell, how slow are the UNIX domain sockets, compared to Shared Memory (or the alternative memory-mapped file)?

有人能说,与共享内存(或备用内存映射文件)相比,UNIX域套接字有多慢?

Thanks.

谢谢。

4 个解决方案

#1


58  

It's more a question of design, than speed (Shared Memory is faster), domain sockets are definitively more UNIX-style, and do a lot less problems. In terms of choice know beforehand:

这更像是一个设计问题,而不是速度(共享内存更快),域套接字肯定更像UNIX风格,并且做的问题少得多。在事先知道的选择方面:

Domain Sockets advantages

域套接字的优点

  • blocking and non-blocking mode and switching between them
  • 阻塞和非阻塞模式以及它们之间的切换
  • you don't have to free them when tasks are completed
  • 任务完成后,您不必释放它们

Domain sockets disadvantages

域套接字缺点

  • must read and write in a linear fashion
  • 必须以线性方式读写

Shared Memory advantages

共享内存优势

  • non-linear storage
  • 非线性存储
  • will never block
  • 永远不会阻止
  • multiple programs can access it
  • 多个程序可以访问它

Shared Memory disadvantages

共享内存的缺点

  • need locking implementation
  • 需要锁定实现
  • need manual freeing, even if unused by any program
  • 需要手动释放,即使任何程序未使用

That's all I can think of now. However, I'd go with domain sockets any day -- not to mention that it's a lot easier then to reimplement them to do distributed computing. The speed gain of Shared Memory will be lost because of the need of a safe design. However, if you know exactly what you're doing, and use the proper kernel calls, you can achieve greater speed with Shared Memory.

这就是我现在所能想到的。但是,我任何时候都会使用域套接字 - 更不用说重新实现它们来分布式计算要容易得多。由于需要安全设计,共享内存的速度增益将会丢失。但是,如果您确切知道自己在做什么,并使用正确的内核调用,则可以通过共享内存实现更快的速度。

#2


7  

In terms of speed shared memory is definitely the winner. With sockets you will have at least two copies of the data - from sending process to the kernel buffer, then from the kernel to the receiving process. With shared memory the latency will only be bound by the cache consistency algorithm between the cores on the box.

在速度方面,共享内存绝对是赢家。使用套接字,您将拥有至少两个数据副本 - 从发送进程到内核缓冲区,然后从内核到接收进程。对于共享内存,延迟仅受到盒子核心之间的缓存一致性算法的约束。

As Kornel notes though, dealing with shared memory is more involved since you have to come up with your own synchronization/signalling scheme, which might add a delay depending on which route you go. Definitely use semaphores in shared memory (implemented with futex on Linux) to avoid system calls in non-contended case.

正如Kornel所指出的那样,处理共享内存更为复杂,因为您必须提出自己的同步/信令方案,这可能会增加延迟,具体取决于您前往的路由。绝对在共享内存中使用信号量(在Linux上使用futex实现),以避免在非竞争情况下进行系统调用。

#3


2  

Both are inter process communication (IPC) mechanisms. UNIX domain sockets are uses for communication between processes on one host similar as TCP-Sockets are used between different hosts. Shared memory (SHM) is a piece of memory where you can put data and share this between processes. SHM provides you random access by using pointers, Sockets can be written or read but you cannot rewind or do positioning.

两者都是进程间通信(IPC)机制。 UNIX域套接字用于在一个主机上的进程之间进行通信,类似于在不同主机之间使用TCP套接字。共享内存(SHM)是一块内存,您可以在其中放置数据并在进程之间共享。 SHM通过使用指针为您提供随机访问,可以写入或读取套接字,但您无法倒带或进行定位。

#4


0  

In this case - sockets are faster. Writing to shared memory is faster then any IPC but writing to a memory mapped file and writing to shared memory are 2 completely different things.

在这种情况下 - 套接字更快。写入共享内存比任何IPC都快,但写入内存映射文件和写入共享内存是完全不同的两件事。

when writing to a memory mapped file you need to "flush" what was written to the shared memory to an actual binded file (not exactly, the flush is being done for you), so you copy your data first to the shared memory, and then you copy it again (flush) to the actual file and that is super duper expansive - more then anything, even more then writing to socket, you are gaining nothing by doing that.

在写入内存映射文件时,您需要将写入共享内存的内容“刷新”到实际的绑定文件(不完全是,正在为您完成刷新),因此您首先将数据复制到共享内存中,然后你再次复制(刷新)到实际的文件,这是超级duper扩展 - 更多的东西,甚至更多然后写入套接字,你没有做到这一点。

#1


58  

It's more a question of design, than speed (Shared Memory is faster), domain sockets are definitively more UNIX-style, and do a lot less problems. In terms of choice know beforehand:

这更像是一个设计问题,而不是速度(共享内存更快),域套接字肯定更像UNIX风格,并且做的问题少得多。在事先知道的选择方面:

Domain Sockets advantages

域套接字的优点

  • blocking and non-blocking mode and switching between them
  • 阻塞和非阻塞模式以及它们之间的切换
  • you don't have to free them when tasks are completed
  • 任务完成后,您不必释放它们

Domain sockets disadvantages

域套接字缺点

  • must read and write in a linear fashion
  • 必须以线性方式读写

Shared Memory advantages

共享内存优势

  • non-linear storage
  • 非线性存储
  • will never block
  • 永远不会阻止
  • multiple programs can access it
  • 多个程序可以访问它

Shared Memory disadvantages

共享内存的缺点

  • need locking implementation
  • 需要锁定实现
  • need manual freeing, even if unused by any program
  • 需要手动释放,即使任何程序未使用

That's all I can think of now. However, I'd go with domain sockets any day -- not to mention that it's a lot easier then to reimplement them to do distributed computing. The speed gain of Shared Memory will be lost because of the need of a safe design. However, if you know exactly what you're doing, and use the proper kernel calls, you can achieve greater speed with Shared Memory.

这就是我现在所能想到的。但是,我任何时候都会使用域套接字 - 更不用说重新实现它们来分布式计算要容易得多。由于需要安全设计,共享内存的速度增益将会丢失。但是,如果您确切知道自己在做什么,并使用正确的内核调用,则可以通过共享内存实现更快的速度。

#2


7  

In terms of speed shared memory is definitely the winner. With sockets you will have at least two copies of the data - from sending process to the kernel buffer, then from the kernel to the receiving process. With shared memory the latency will only be bound by the cache consistency algorithm between the cores on the box.

在速度方面,共享内存绝对是赢家。使用套接字,您将拥有至少两个数据副本 - 从发送进程到内核缓冲区,然后从内核到接收进程。对于共享内存,延迟仅受到盒子核心之间的缓存一致性算法的约束。

As Kornel notes though, dealing with shared memory is more involved since you have to come up with your own synchronization/signalling scheme, which might add a delay depending on which route you go. Definitely use semaphores in shared memory (implemented with futex on Linux) to avoid system calls in non-contended case.

正如Kornel所指出的那样,处理共享内存更为复杂,因为您必须提出自己的同步/信令方案,这可能会增加延迟,具体取决于您前往的路由。绝对在共享内存中使用信号量(在Linux上使用futex实现),以避免在非竞争情况下进行系统调用。

#3


2  

Both are inter process communication (IPC) mechanisms. UNIX domain sockets are uses for communication between processes on one host similar as TCP-Sockets are used between different hosts. Shared memory (SHM) is a piece of memory where you can put data and share this between processes. SHM provides you random access by using pointers, Sockets can be written or read but you cannot rewind or do positioning.

两者都是进程间通信(IPC)机制。 UNIX域套接字用于在一个主机上的进程之间进行通信,类似于在不同主机之间使用TCP套接字。共享内存(SHM)是一块内存,您可以在其中放置数据并在进程之间共享。 SHM通过使用指针为您提供随机访问,可以写入或读取套接字,但您无法倒带或进行定位。

#4


0  

In this case - sockets are faster. Writing to shared memory is faster then any IPC but writing to a memory mapped file and writing to shared memory are 2 completely different things.

在这种情况下 - 套接字更快。写入共享内存比任何IPC都快,但写入内存映射文件和写入共享内存是完全不同的两件事。

when writing to a memory mapped file you need to "flush" what was written to the shared memory to an actual binded file (not exactly, the flush is being done for you), so you copy your data first to the shared memory, and then you copy it again (flush) to the actual file and that is super duper expansive - more then anything, even more then writing to socket, you are gaining nothing by doing that.

在写入内存映射文件时,您需要将写入共享内存的内容“刷新”到实际的绑定文件(不完全是,正在为您完成刷新),因此您首先将数据复制到共享内存中,然后你再次复制(刷新)到实际的文件,这是超级duper扩展 - 更多的东西,甚至更多然后写入套接字,你没有做到这一点。