When we create a shared-Memory we use shm_open()
and ftruncate()
function. According to my information shm_open()
create a shared-memory region. And then we use ftruncate()
function to configure the size of shared-memory region.
Well how does shm_open()
creates the memory region in the first place when it doesn't yet know the size? And if this is not the case and I am totally wrong, then please tell me the purpose of shm_open()
and ftruncate().
Thanks in Advance!!!
The main point of
shm_open
is that you can open an existing memory area. However in the case that it didn't exist and you'd create it,shm_open
will create a new shared memory object of 0 bytes, just likeopen
withO_CREAT
would create a file of 0 bytes. From Linux manuals:(emphasis mine)
Since
shm_open
does not take the size of newly created area as an argument (it'd complicate the system call / library call to add arguments for all sorts of cases),ftruncate()
must used to change the size of an opened shared memory area from its initial size.Of course you do not have to use
ftruncate
for a shared memory segment that is already properly created and resized elsewhere. Should you want to know its size, usefstat
. See alsoshm_overview(7)