c - Memory Management for Mapped Data in Shared Memory Segments -
i'm working on project in c uses shared memory ipc on linux system. however, i'm little bit confused memory management in these segments. i'm using posix api project.
i understand how create shared segments, , these persist until reboot if fail remove them shm_unlink()
. additionally, understand how mapping & unmapping mmap
, munmap
respectively. however, usage of these operations , how affects stored data in these shared segments confusing me.
here i'm trying understand:
lets create segment using shm_open()
o_creat
flag. gives me file descriptor i've named msfd
in below example. have struct map address space following:
mystruct* ms = (mystruct*)mmap(null, sizeof(mystruct), prot_read | prot_write, map_shared, msfd, 0); //set elements of struct here using ms->element = x usual
part 1) here's confusion beings. lets process done accessing location since setting data process read. still call munmap()
?
i want other process still have access of data current process has set. normally, wouldn't call free()
on malloc
'ed pointer until use no longer needed permanently. however, understand when process exits unmapping happens automatically anyway. data persisted inside segment, or segment reserved it's allotted size , name?
part 2) we're in process of other application needs access , read shared segment. understand open segment shm_open()
, perform same mapping operation mmap()
. have access structure in segment. when call munmap()
process (not 1 created data) "unlinks" pointer, data still accessible. assume process 1 (the creator) has not called munmap()
?
is data persisted inside segment,
yes.
does segment reserved it's allotted size , name?
also yes.
does assume process 1 (the creator) has not called munmap()?
no.
the shared memory gets created via shm_create()
(as being taken available os memory) , moment on carries whichever content had been written until given os via shm_unlink()
.
shm_create()
, shm_open()
act system oriented, in terms of (shared) memory being system (not process) specific resource.
mmap()
, unmap()
act process oriented, map , unmap system resource shared memory into/out-of process' address space.
Comments
Post a Comment