Interprocess Communication in C ++

I have a simple C ++ application that generates reports on the back of my web application (simple LAMP setup). The problem is that the back is loading a data file which takes about 1.5 GB in memory. This won't scale very well if multiple users run it at the same time, so my thought is to split it into multiple programs:  

Program A is the main executable that always runs on the server and always loads data and can actually run reports.  

Program B is derived from php and makes a simple query to program A to get the information it needs and returns the data.  

So my questions are: What is a good mechanism for B to ask A to do something? How is this supposed to work when A has nothing to do? I really don't want to be a poll participant for tasks or otherwise spin tires.

+2


source to share


3 answers


Using a named mutex / event, basically what it does is let one thread (process A in your case) sit there waiting to wait. Then process B comes in, something needs to be done and signals a mutex / event that this wakes up process A and you continue.

If you're at Microsoft:

Mutex , Event



Ipc on linux works differently, but has the same capability:

Linux stuff

Or alternatively, for the C ++ part, you can use one of the IPC boosting libraries that are multi-platform. I'm not sure what PHP has, but it certainly has something equivalent.

+4


source


Use TCP sockets running on localhost

.

  • Make C ++ application a daemon.
  • The PHP interface creates a persistent connection to the daemon. pfsockopen
  • When the request is made, PHP sends the request to the daemon, which then processes and sends it back. PHP C ++ Sockets


EDIT

Added links for reference. I might have very bad C code that uses interprocess sockets somewhere, but nothing convenient.

+2


source


IPC is easy in C ++, just call the POSIX C API.

But what you are asking will be much better served by the queue manager. Let the background daemons wait for messages in the queue, and the PHP frontend will just add the specifications of the task it wants to process. Some queue managers allow you to add a result to a task for one object or define a new queue for finishing messages.

One of the most famous high-performance queue managers RabbitMQ . Another very easy to use is MemcacheQ .

Or you can just add a table to MySQL for tasks, the background process just queries periodically for unfinished ones. This works and can be very reliable (sometimes referred to as ghetto queues) but breaks down into high tasks / second.

+2


source







All Articles