1

I was able to share memory on windows simply using winapi in cpp and mmap.mmap in python. just match "name".

And I was able to set the name of the shared memory using <boost/interprocess/shared_memory_object.hpp> on mac.

But python's mmap.mmap() didn't work. Even in the official documentation the parameters were different. It didn't have a parameter to set a name for.

So I gave up and decided to use <sys/ipc.h>.

It was able to communicate with python using the key, but write() to sysv_ipc in python gives the following error:

ValueError: Attempt to write past end of memory segment

  • python code
shm = sysv_ipc.SharedMemory(777)
if shm:
    offset = 0
    for idx in range(0, 21):
        shm.write(struct.pack(
            'f', hand_landmarks.landmark[idx].x), offset)
        offset += 4
        shm.write(struct.pack(
            'f', hand_landmarks.landmark[idx].y), offset)
        offset += 4
        shm.write(struct.pack(
            'f', hand_landmarks.landmark[idx].z), offset)
        offset += 4
  • c++ code
shmid = shmget(777, 512, IPC_CREAT | 0666)
shared_memory = shmat(shmid, NULL, 0)
fptr = reinterpret_cast<float *>(shared_memory);
for (int i = 0; i < 63; i += 3)
{
    std::cout << fptr[i] << " " << fptr[i + 1] << " " << fptr[i + 2] << "\n";
}

What should I do?

Ko wa
  • 11
  • 3
  • Have you considered using **Redis** - it is very lightweight, very fast, very easy and works across lots of platforms and languages (including the command-line which makes it very simple to inject test data or monitor your app)- you could try it out without installation just using `dockerhub`. – Mark Setchell Jan 10 '22 at 12:17
  • @Mark Setchell Looking at it, it seems like a good to change it! – Ko wa Jan 10 '22 at 12:21
  • You could have a look here... https://stackoverflow.com/a/66456923/2836621 or here https://stackoverflow.com/a/58521903/2836621 – Mark Setchell Jan 10 '22 at 12:26

1 Answers1

0

The issues is that shmget second parameters in in bits, not bytes.

So the correct way to write the code is:

shmid = shmget(777, 512 * 8, IPC_CREAT | 0666)
homer
  • 882
  • 8
  • 23
Ko wa
  • 11
  • 3