Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of shm_open() and ftruncate()?

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!!!

like image 493
Muzahir Hussain Avatar asked Mar 17 '16 09:03

Muzahir Hussain


People also ask

What does Shm_open do in C?

shm_open() creates and opens a new, or opens an existing, POSIX shared memory object.

What is the role of Ftruncate () when you create a shared memory object?

When a new shared-memory object is created, the size of the object is set to zero. To set the size, you use ftruncate()—the very same function used to set the size of a file —or shm_ctl().

Does Shm_open create a file?

The shm_open() function establishes a connection between a shared memory object and a file descriptor. It creates an open file description that refers to the shared memory object and a file descriptor that refers to that open file description.

What is mode in Shm_open?

Points to a string naming a shared memory object. oflag. Specifies the flags to be used by the shm_open subroutine. mode. Sets the value of the permission bits of the shared memory object.


1 Answers

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 like open with O_CREAT would create a file of 0 bytes. From Linux manuals:

O_CREAT

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.

(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, use fstat. See also shm_overview(7)