how two c++ programs can communicate?

Sorry for this "newbie" question... I have not c++ experience whatsoever (yet). I know only some BASIC dialects and little Python.

But I'd like to know how programs written with c++ can communicate, or exchange real-time information. If two separate programs run at the same time, how can they "send signals" to each other so that they can co-operate and know what other program is doing? (Kind of simple "parallel programming".)

(I'm not expecting a detailed analysis, maybe just some links or simple source code example.)
This is called IPC (inter-process communication) and is far from simple.

There are a lot of different types of IPC
http://en.wikipedia.org/wiki/Inter-process_communication
These are also operating system dependent but there are lots of similarities so you stand a good chance of finding a cross platform library.

One type of IPC is signals. There are a fixed set if signals. One program can send a signal to another, the receiving program is then interrupted and jumps straight into some code to handle the signal (then back to where it left off)
If you run a program from a terminal, then type cntrl-C to kill it. The terminal program is sending the signal 'INT' to the other program which then stops (you could write a program to which catches this signal and does something else)

Other IPC methods tend to come in two types
- A message queue or pipe where one process puts data into the queue and another process consumes the data
- Shared memory where part of the memory is accessible by more than one process

There are things to consider. For a message queue or pipe does the receiving program wait until it gets a message before doing anything or does it carry on running, looking at the queue occasionally?

For shared memory you need to prevent different processes from writing or reading the same bit of memory at the same time, which means you may need to use semaphores.

Another way to share data between processes is to just use a file. You can lock the file one one process is modifying it so that the another process doesn't access it at this time.

There are IPC methods available in python.


Topic archived. No new replies allowed.