0

I am attempting to do a relatively simple task using boost interprocess semaphore and shared memory. I want to fixed buffer of data shared between two processes, where the first process is a producer and the second process is a consumer. The buffer will consist of 3 parts. The first part will be a boost::interprocess::semaphore, used to coordinate the producer/consumer. The second part will just be an integer, so the consumer knows how many items are on the buffer. The third part will be the actual array of items. I have a very basic implementation started, but the processes hang when attempting to access open the shared memory, and i'm not certain why. I am doing this on 64-bit Centos 6.5 with gcc/g++ 4.8.2. I should note also that the machine has two CPUs, and using process affinity I am ensuring that the producer and the consumer both run on separate CPUs.

The code is at http://pastie.org/9693362. I am experiencing the following issues; with the code as is, the consumer and producer both hang at line 3 (fixed_managed_shared_memory shm(open_only, "SharedMem" );). If i comment that line out, then both end up terminating (no error is caught however) at line 26 (the post/wait on the semaphore). This makes me think that somehow the memory isnt being shared, b/c when i print out the addresses, they seem to be properly formed (as in the offsets seem to be correct), and they are properly passed between processes. Is there something I'm missing on how to properly set this up?

jekelija
  • 257
  • 4
  • 16
  • What's not to love? C-style casts doing `reinterpret_cast`, stringly typed void* passing (`std::string semaphoreAddr`, yes sir), `system()` calls, out-of-synch comments, explicit destructor calling, a big-wall-of-prose "question", external code link with incomplete code in non-compilable order? [_Oh I forget raw pointer arithmetics on 40% of the lines and 2 missing variables._] – sehe Nov 03 '14 at 15:36
  • What are you using boost for, if you're going to treat everything as C anyways? `mmap` seems a much more natural fit. – sehe Nov 03 '14 at 15:38
  • I was using boost just b/c i'm familiar with it; i was not aware of mmap. I'm sorry if some of the code is incomplete or some variables are missing, i was trying to strip out some of the extra fluff. I was hoping there was something simple i was missing in regards to the fixed_managed_shared_memory, something i was forgetting to initialize that would cause it to hang. If you'd like, i can strip out all the rest of the code so that it focuses in on that problem? – jekelija Nov 03 '14 at 16:04
  • We always like that on SO: http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions. In general I feel as if you actually used boost (fully) your code would be 4x smaller and 10x less error prone. – sehe Nov 03 '14 at 16:11
  • I want to; i just couldnt figure out how to get it to share the exact same memory using just boost, so i resorted to pointer arithmetic and ridiculous program arguments... if you know of how to share identical memory space using boost, please share. – jekelija Nov 03 '14 at 16:43

0 Answers0