C ++中的进程间通信 [英] Interprocess Communication in C++
问题描述
我有一个简单的c ++应用程序,可以在我的Web应用程序的后端生成报告(简单的LAMP设置).问题在于后端加载的数据文件大约需要1.5GB的内存.如果多个用户同时运行它,伸缩性将不会很好,所以我想将其拆分为几个程序:
程序A是始终在服务器上运行的主要可执行文件,始终加载数据,并且可以实际运行报告.
程序B是从php产生的,它向程序A发出一个简单的请求以获取所需的信息,并返回数据.
所以我的问题是:
B要求A做某事的良好机制是什么?
当A无关时应如何工作?我真的不想轮询任务或以其他方式旋转轮胎.
I have a simple c++ application that generates reports on the back end of my web app (simple LAMP setup). The problem is the back end loads a data file that takes about 1.5GB in memory. This won't scale very well if multiple users are running it simultaneously, so my thought is to split into several programs :
Program A is the main executable that is always running on the server, and always has the data loaded, and can actually run reports.
Program B is spawned from php, and makes a simple request to program A to get the info it needs, and returns the data.
So my questions are these:
What is a good mechanism for B to ask A to do something?
How should it work when A has nothing to do? I don't really want to be polling for tasks or otherwise spinning my tires.
推荐答案
使用命名的互斥锁/事件,基本上,此操作允许一个线程(在您的情况下为进程A)坐在那里挂出等待.然后,过程B来了,需要完成一些工作,并向互斥体/事件发出信号,这会唤醒过程A,然后继续.
Use a named mutex/event, basically what this does is allows one thread (process A in your case) to sit there hanging out waiting. Then process B comes along, needing something done, and signals the mutex/event this wakes up process A, and you proceed.
如果您使用的是Microsoft:
If you are on Microsoft :
Linux上的IPC的工作方式不同,但功能相同:
Ipc on linux works differently, but has the same capability:
或者,对于c ++部分,您可以使用Boost IPC库之一,它是多平台的.我不确定有什么可用的PHP,但是毫无疑问它将具有等效的功能.
Or alternatively, for the c++ portion you can use one of the boost IPC libraries, which are multi-platform. I'm not sure what PHP has available, but it will no doubt have something equivalent.
这篇关于C ++中的进程间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!