c++ - Is shared virtual memory used when multiple processes read a file using file pointer in Linux? -
i wrote c++ program read file using file pointer. , need run multiple process @ same time. since size of file can huge (100mb~), reduce memory usage in multiple processes, think need use shared memory. (for example ipc library boost::interprocess::shared_memory_object)
but need? because think if multiple processes read same file, virtual memory of each processes mapped same physical memory of file thru page table.
i read linux doc , said,
shared virtual memory
although virtual memory allows processes have separate (virtual) address spaces, there times when need processes share memory. example there several processes in system running bash command shell. rather have several copies of bash, 1 in each processes virtual address space, better have 1 copy in physical memory , of processes running bash share it. dynamic libraries common example of executing code shared between several processes. shared memory can used inter process communication (ipc) mechanism, 2 or more processes exchanging information via memory common of them. linux supports unix tm system v shared memory ipc.
also, wiki said,
in computer software, shared memory either
- a method of inter-process communication (ipc), i.e. way of exchanging data between programs running @ same time. 1 process create area in ram other processes can access, or
- a method of conserving memory space directing accesses ordinarily copies of piece of data single instance instead, using virtual memory mappings or explicit support of program in question. used shared libraries , xip.
therefore, curious shared virtual memory supported os level or not?
thanks in advance.
regarding first question - if want data accessible multiple processes without duplication you'll need kind of shared storage.
in c++ i'd surely use boost's shared_memory_object. that's valid option share (large) data among processes , has documentation examples (http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/sharedmemorybetweenprocesses.html).
using mmap() more low-level approach used in c. use ipc you'll have make mapped region shared. http://man7.org/linux/man-pages/man2/mmap.2.html:
map_shared
share mapping. updates mapping visible other processes map file, , carried through underlying file. file may not updated until msync(2) or munmap() called.
also on page there's example of mapping file shared memory.
in either case there @ least 2 things remember:
you need synchronization if there multiple processes modify shared data.
you can't use pointers, offsets beginning of mapped region. here's explanation boost docs:
if several processes map same file/shared memory, mapping address surely different in each process. since each process might have used address space in different way (allocation of more or less dynamic memory, example), there no guarantee file/shared memory going mapped in same address.
if 2 processes map same object in different addresses, invalidates use of pointers in memory, since pointer (which absolute address) make sense process wrote it. solution use offsets (distance) between objects instead of pointers: if 2 objects placed in same shared memory segment 1 process, the address of each object different in process distance between them (in bytes) same.
regarding os support - yes, shred memory os specific feature. in linux mmap() implemented in kernel , modules , can used transfer data between user , kernel-space.
windows has it's specifics:
windows shared memory creation bit different portable shared memory creation: size of segment must specified when creating object , can't specified through truncate shared memory object. take in care when last process attached shared memory destroyed shared memory destroyed there no persistency native windows shared memory.
Comments
Post a Comment