Having learnt a bit about the subject, can anyone tell, what is the real difference between POSIX shared memory (shm_open) and POSIX mapped files (mmap)?
Both seems to use the /dev/tmpfs subsystem, rather then older IPC mechanism.
So is there any advantage of using mmap file over shared memory?
Thanks.
The POSIX shared memory API allows processes to communicate information by sharing a region of memory. The interfaces employed in the API are: shm_open(3) Create and open a new object, or open an existing object. This is analogous to open(2).
The main difference between System V shared memory (shmem) and memory mapped I/O (mmap) is that System V shared memory is persistent: unless explicitly removed by a process, it is kept in memory and remains available until the system is shut down.
Faster File AccessAccessing files via memory map is faster than using I/O functions such as fread and fwrite . Data are read and written using the virtual memory capabilities that are built in to the operating system rather than having to allocate, copy into, and then deallocate data buffers owned by the process.
In short, mmap() is great if you're doing a large amount of IO in terms of total bytes transferred; this is because it reduces the number of copies needed, and can significantly reduce the number of kernel entries needed for reading cached data.
The distinction is not always clear. Shared memory can be implemented via memory mapped files. An excellent write on this can be found here (as applied to C/C++ programming).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With