Windows管道之间进行进程间通信的主要缺点是什么? [英] What are the main disadvantages of Windows pipes for inter-process communication?

查看:503
本文介绍了Windows管道之间进行进程间通信的主要缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说您正在将一个完整的Windows程序拆分为可重用的组件,这些组件可以使用某种

Say you are splitting a monolithic Windows program into reusable components which could communicate with each other using some sort of inter-process communication (IPC), and, for whatever the reason, you choose anonymous pipes (e.g. starting a number of Process-es and connecting Console.Out to Console.In of the next process, or just using "|" in the command prompt).

Dobbs博士的文章,带有示例带有命名管道:

A Dr. Dobbs article with an example with named pipes:

与同一台计算机上的其他IPC方法相比,性能损失是多少?当扩大进程数量或传输数据量时,首先要解决的问题是什么?

What would be the performance penalty compared to other IPC methods within the same machine? And what would be the first problem to hit when scaling up in the number of processes or the amount of data being transmitted?

例如如果不是使用管道,而是使用文件,则会遇到锁定问题和磁盘速度.

E.g. If instead of pipes we were using files we would hit locking issues and disk speed.

请注意,由于必须对要传输的数据结构进行序列化,因此已经存在开销,这是我降低效率的基准.

Note that by having to serialise the data structures being transmitted there is already an overhead, which is my baseline for loss of efficiency.

PS.对管道的兴趣是因为它们易于在每个进程(Console.Readline,Console.WriteLine)中实现,易于为它们编写MSMQ和异步HTTP接口,以及对Unix和命令行的热爱.

PS. The interest in pipes is because they are easy to implement in each process (Console.Readline, Console.WriteLine), it's easy to write MSMQ and async HTTP interfaces to them, and for the love of Unix and the command line.

推荐答案

您可以使用CreatePipe()Win32 API调用创建双向匿名管道,因此管道输入/输出不是唯一的方法.您只需获得一个可以交给其他过程的新文件句柄即可.

You can create two-way anonymous pipes using CreatePipe() Win32 API call, so piping input/output is not the only way. You simply get a new file handle you can give to the other process.

匿名管道基于共享内存,但不支持异步操作(通过ReadFileEx,ReadFileWrite).因此,性能问题(缺点)是1)同步操作,2)内存复制,3)进程间同步.通常,原始"共享内存(没有实际备份文件的内存映射文件)和命名管道将更快,套接字和Window消息将比匿名管道更慢.

Anonymous pipes are based on shared memory, but do not support asynchronous operations (through ReadFileEx, ReadFileWrite). Performance issues (disadvantages) are thus 1) synchronous operation, 2) memory copying, 3) interprocess synchronization. Generally, "raw" shared memory (memory mapped file without an actual backing file) and named pipes will be faster, and sockets and Window messages will be slower (than anonymous pipes).

您不能将I/O完成端口(IOCP)与匿名管道一起使用,而是必须轮询"管道,从而导致额外的上下文切换.除了序列化数据外,还必须在内存中复制序列化的数据,因为您无法直接写入共享内存.一个进程还必须等待另一个进程,这意味着另一个进程必须发信号通知进程间同步原语.性能在很大程度上取决于消息的大小(读/写调用与发送的数据量之比),因为对于每次读/写,该过程都必须进行上下文切换,并且可能旋转/休眠.

You cannot use I/O Completion Ports (IOCP) with anonymous pipes, instead you'll have to "poll" the pipe, incurring extra context switches. In addition to serializing the data, the serialized data has to be copied around in memory, as you cannot write directly to the shared memory. One process also has to wait for another, implying that the other process has to signal an interprocess synchronization primitive. Performance depends heavily on the size of the messages (ratio of read/write calls to amount of data sent), because for each read/write the process has to make context switches and possibly spin/sleep.

除原始"共享内存外,所有方法均需要内存复制和某种进程间信令(同步),因此匿名管道的主要缺点是同步操作.当CPU花费大部分时间进行上下文切换时,您将在传输大量消息时达到顶峰.

All methods except "raw" shared memory require memory copying and some sort of interprocess signaling (synchronization), so the main disadvantage of anonymous pipes is synchronous operation. You'll hit the ceiling when transmitting large number of messages, when the CPU spends most of its time doing context switches.

在性能方面,命名管道更好,因为您可以让工作线程使用IOCP处理异步通知,甚至可以通过一次调用接收多个消息,从而减少了API开销.如果您自己制造组件,那么给管道起个名字所带来的额外麻烦是值得的(您甚至可以跨网络连接). Windows的更高版本为本地套接字实现了特殊的路由,该路由也支持IOCP.

Performance-wise, named pipes are better because you can have worker thread(s) processing async notifications using IOCP, and can even receive multiple messages with one call, thus reducing the API overhead. If making your own components, the extra trouble from giving a name to the pipe is well worth the trouble (and you can even connect across networks). Later Windows versions implement special routing for local sockets, which also do support IOCP.

最快的方法是直接使用共享内存,但随后您将不得不自己进行进程间同步.可以自己实现无锁管道,但是如果您不经常传输数据,则仍然必须使用同步原语来通知/唤醒侦听过程.

The fastest method would be to use shared memory directly, but then you will have to take care of interprocess synchronization yourself. It's possible to implement lock-free pipes yourself, but in case you're not constantly transmitting data, you'll still have to use synchronization primitives to notify/wake the listening process up.

还请注意,使用文件并不意味着您会受到磁盘速度的限制,因为您可以使用内存映射文件,即使是普通文件也可以使用缓存管理器来进行读取/写入.其他方法(RPC,剪贴板等)基于这些标准"方法,这意味着它们将仅添加一些额外的协议层,并且表示更容易/更有用,或更适合于某些编程环境(但不要更快).

Also note that using files does not imply that you would be limited by disk speed, as you can use memory mapped files and even with normal files, the cache manager handles the reading/writing. The other methods (RPC, clipboard, etc) are based on these "standard" methods, which means they'll just add some extra layer of protocol and are meant to be easier/more helpful, or more suitable for some programming environment (but not to be faster).

这篇关于Windows管道之间进行进程间通信的主要缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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