When we create a shared-Memory we use
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 behaves like
open when creating a file; the newly created memory area has size of 0. From Linux manuals:
Create the shared memory object if it does not exist. The user and group ownership of the object are taken from the corresponding effective IDs of the calling process, and the object's permission bits are set according to the low-order 9 bits of mode, except that those bits set in the process file mode creation mask (see umask(2)) are cleared for the new object. A set of macro constants which can be used to define mode is listed in open(2). (Symbolic definitions of these constants can be obtained by including .)
A new shared memory object initially has zero length--the size of the object can be set using
ftruncate(2).The newly allocated bytes of a shared memory object are automatically initialized to 0.
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, use
fstat. See also