C++/C++ inter-program communication
Hello. For reason's not important to this question, i need to have one c++ program, access a variable that is declared in another program.
I tried taking ProgramA, declaring an integer. Then saving the address of that integer in a text file. Then while programA was still running, i ran ProgramB which read the text file, took the value and stored it into a pointer, and then read the value at that address.
The values didn't match. How to i make two program's share memory addresses?
As far as C++ is concerned the short answer is that you do not.
The longer answer is that it depends on the available facilities of the operating system you are running the programs on (assuming you are using a system that has an operating system at all). I will overview some methods; however I will leave it up to you to look up the details of the method(s) that interest you as you have not given any indication of the kind of system you are using. You can obtain further information from several places:
- In built system help, UNIX/Linux systems have the man command and manual pages for commands and functions (note: they are usually found in sections 2 or 3 of the manual). MS systems do not have developer help built in, however if you subscribe to the MS Developer Network (MSDN) or use their more recent developer tools then you may well have copies of the MSDN library installed on your system.
- From the Internet. There is a lot of software development information on the Internet. If you use Microsoft systems and do not have the MSDN library available locally then it can be found on the MSDN site (http://msdn.microsoft.com
) at http://msdn.microsoft.com/library/
. Most UNIX/Linux man pages seem to be available in various places. Use a search engine such as Google (http://www.google.com/
) to enter a query for what you wish to know about and see what turns up!
- From books. There are many, many book on developing software so the system(s) you are using and the facilities you want information about will almost certainly be covered by some book(s) somewhere. The ACCU site (http://accu.org/
) has many book reviews (http://accu.org/index.php/book_reviews
), which may help you determine those more likely to be of use.
If you are using a desktop or server operating systems such as MS Windows XP, UNIX or Linux then such systems run each process in its own protected virtual memory space. For the purposes of this question each of the programs you run will run in its own process space. Hence the addresses used by one program running in one process have meaning only in that process. This is made possible by the virtual memory management facilities of the processors. In fact if you run two instances of the same program (each in its own process) then you can expect the addresses of objects to have the same memory address in each process. That is each process has a virtual memory address range the same as any other process. Behind the scenes the virtual memory system will map these virtual addresses to the required physical memory addresses.
Run time libraries can be shared by several (or all) processes. Such libraries are called shared object libraries on UNIX/Linux like systems and Dynamic Link Libraries (DLLs) on MS Windows (and some other) systems. In such cases the required code (and data) is mapped into each process' address space.
OK so that is the background on why your plan did not work as expected. What can you do to fix it? The general term for handling this sort of problem is called Inter Process Communication or IPC for short.
The most obvious method would be to ask for a shared region of memory between program processes. This is called shared memory or a memory mapping. UNIX/Linux systems have two API sets for handling such a facility: The older System V shared memory API, with functions like shmget, shmat, shmdt, and the POSIX API functions mmap, munmap msync. MS Win32 systems have the functions CreateFileMapping, OpenFileMapping, MapViewOfFile, MapViewOfFileEx, FlushViewOfFile.
Note that sharing data between processes can cause synchronisation problems if more than one process wishes to update/access the data at a time.
There are other methods. For example you could use a pipe to transfer the data from one process to another. Pipes connect the output of one process to the input of another. See the UNIX/Linux (POSIX) pipe and mkfifo API functions and the Win32 CreatePipe, CreateNamedPipe, ConnectNamedPipe etc. API functions. In your case named pipes (POSIX calls named pipes FIFOs) would be most useful as you can share the name (typically similar to or the same as a file name) between programs. Similarly you could use sockets (as in TCP/IP socket) to transfer the information, see API functions socket, bind, accept, listen etc. (note Win32 has the winsock2 API functions that are similar to the UNIX/Linux/POSIX/Berkley style socket API in this case).
You could also use middleware systems such as COM/DCOM and CORBA. These, like named pipes and sockets, have the capability to allow your programs to be running on different machines altogether.
Note that there are libraries that can wrap some/all of these raw operating system facilities. One such is the ACE library (http://www.cs.wustl.edu/~schmidt/ACE.html
I hope I have given you enough information that you can look up further information on the specific parts that interest you.