0

I'm on Linux, and I using mmap for sharing information between python code and C code. When I used this instruction:

buf = mmap.mmap(fd.fileno(), 4096, access=mmap.ACCESS_DEFAULT)
with buf:
    buf.write(b'HELLO WORLD')

This is C instruction for mmap:

  int *shared = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);

I'm expecting my changes are still in the memory, but it is in a file on the disk already.

I would like that Python code write all information on the memory and C code write all information in the memory to file. Is it possible to do this?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
giupardeb
  • 791
  • 1
  • 5
  • 13
  • 3
    When you map a file, the memory and the file are automatically synchronized. – Barmar Sep 11 '19 at 22:52
  • is not here any solution to avoid the sync? with many changes it's like having a physical file without a map. Maybe is there an alternative to mmap? – giupardeb Sep 11 '19 at 22:56
  • What are you really trying to achieve? If you don't want the file, you can use shared memory instead of `mmap`. See https://stackoverflow.com/questions/49103709/how-to-use-shared-memory-in-python-and-c-c – Barmar Sep 11 '19 at 23:06
  • The whole point of the [`mmap`](https://docs.python.org/3/library/mmap.html#module-mmap) module is to support memory-mapped _files_ — sounds like what you want is memory shared between processes... – martineau Sep 12 '19 at 00:16
  • @Barmar Yes my goal is memory shared between processes. I want that Python write on the memory shared and C read from the memoty shared and finally, before the C process ends it must write the memory shared to a file. – giupardeb Sep 12 '19 at 06:35
  • It looks like they're adding a [`multiprocessing.shared_memory.SharedMemory`](https://docs.python.org/3.9/library/multiprocessing.shared_memory.html) class in Python 3.8. – martineau Sep 12 '19 at 08:46

0 Answers0