Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posix shared memory vs mapped files

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.

like image 461
SyBer Avatar asked Feb 14 '10 21:02

SyBer


People also ask

What is POSIX shared memory?

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).

What is the difference between shared memory and mmap?

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.

Are memory-mapped files faster?

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.

Why is mmap better than read?

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.


1 Answers

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).

like image 117
ennuikiller Avatar answered Sep 17 '22 13:09

ennuikiller