1

I have a 64 bit application that creates 2 subprocesses (32 bit) via a popen2 implementation. Everything is written in C++.

I need the 2 subprocesses to access the same object in memory and I don't have a good idea about how to do this.

If I understand correctly, each subprocess will have a different memory map and therefore I can't just pass a memory address between the two.

Additional information: The target platform is Mac but I'm looking for an answer that is as platform independent as possible Mac specific answers are fine, I probably won't use this approach on other platforms. I simply don't know enough about using threads; I came down this route because the subprocesses must be 32 bit.

koan
  • 3,596
  • 2
  • 25
  • 35
  • 1
    The simplest way by far is to create threads instead of sub-processes. – Marcelo Cantos Jan 19 '13 at 12:07
  • what OS are you using? [And why can't you use threads?] – Mats Petersson Jan 19 '13 at 12:07
  • Assuming your target platform is Linux, take a look at [this question / answers][1] [1]: http://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c – Component 10 Jan 19 '13 at 12:07
  • I added some info to the question: I don't know about starting 32 bit threads from a 64 bit application. I'm on a Mac but I'm looking for a platform independent answer. – koan Jan 19 '13 at 12:38
  • @MarceloCantos,@MatsPetersson My application is 64 bit and starts 2 x 32 bit processes (each subprocess links to a closed source library that is only available as 32 bit). Is it possible to use threads in this case ? – koan Jan 19 '13 at 21:57
  • @koan: I'm afraid not. Yours is one of the few cases where creating subprocesses is the only real choice. – Marcelo Cantos Jan 19 '13 at 22:05

2 Answers2

2

You can use shared memory concept. It means, that you allocate (using OS services) a memory, that will be visible by both subprocesses.

As wiki recoomends, you can use boost.interprocess to use shared memory on platform-independent level.

Lol4t0
  • 12,444
  • 4
  • 29
  • 65
1

It's a difficult problem.

You are correct that each process has its own address space. Objects created by one process can't be accessed by another process.

It is possible to use shared memory, and place the objects there. One complication is that in general the shared memory segment will be mapped at different addresses into each process's address space. This means that you can't use pointers inside those object. This can be alleviated by working with indices instead of pointers.

Furthermore, if process A is 32-bit and process B is 64-bit, primitive types such as long can have different width. Thus when sharing data in such a scenario you need to use fixed-width types such as int32_t.

One final complication is synchronization: if a process can modify an object while another process is reading or modifying it, you'll need to introduces inter-process synchronization.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I'll be sharing one object between the two 32 bit processes so type sizes will be the same. I think I can use the public API for the object then I won't have to mess around with translating pointers. So it's just a question of getting access to the object by both processes, isn't it ? (And the synchronization issue). – koan Jan 19 '13 at 12:44
  • @koan: Do you know for a fact that the object doesn't use pointers internally? If it does, that would be problematic. – NPE Jan 19 '13 at 12:54