12_shared_memory.rst 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. Shared Memory Interfaces
  2. ************************
  3. Shared memory interfaces are only available with the NuttX kernel build
  4. (``CONFIG_BUILD_KERNEL=y``). These interfaces support user memory
  5. regions that can be shared between multiple user processes. Shared
  6. memory interfaces:
  7. - :c:func:`shmget`
  8. - :c:func:`shmat`
  9. - :c:func:`shmctl`
  10. - :c:func:`shmdt`
  11. Functions
  12. ---------
  13. .. c:function:: int shmget(key_t key, size_t size, int shmflg)
  14. Returns the shared memory identifier associated with ``key``.
  15. A shared memory identifier, associated data structure, and shared memory
  16. segment of at least size bytes is created for ``key`` if one of the
  17. following is true:
  18. - The argument ``key`` is equal to ``IPC_PRIVATE``.
  19. - The argument ``key`` does not already have a shared memory identifier
  20. associated with it and ``(shmflg & IPC_CREAT)`` is non-zero.
  21. Upon creation, the data structure associated with the new shared memory
  22. identifier will be initialized as follows:
  23. - The low-order nine bits of ``shm_perm.mode`` are set equal to the
  24. low-order nine bits of ``shmflg``.
  25. - The value of ``shm_segsz`` is set equal to the value of size.
  26. - The values of ``shm_lpid``, ``shm_nattch``, ``shm_atime``, and
  27. ``shm_dtime`` are set equal to 0.
  28. - The value of ``shm_ctime`` is set equal to the current time.
  29. When the shared memory segment is created, it will be initialized with
  30. all zero values.
  31. :param key: The key that is used to access the unique shared memory
  32. identifier.
  33. :param size: The shared memory region that is created will be at least
  34. this size in bytes.
  35. :param shmflg: See ``IPC_*`` definitions in ``sys/ipc.h``. Only the
  36. values ``IPC_PRIVATE`` or ``IPC_CREAT`` are supported.
  37. :return: Upon successful completion, ``shmget()`` will return
  38. a non-negative integer, namely a shared memory identifier; otherwise, it
  39. will return -1 and set ``errno`` to indicate the error.
  40. - ``EACCES``. A shared memory identifier exists for key but operation
  41. permission as specified by the low-order nine bits of ``shmflg``
  42. would not be granted.
  43. - ``EEXIST``. A shared memory identifier exists for the argument key
  44. but ``(shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)`` are non-zero.
  45. - ``EINVAL``. A shared memory segment is to be created and the value of
  46. size is less than the system-imposed minimum or greater than the
  47. system-imposed maximum.
  48. - ``EINVAL``. No shared memory segment is to be created and a shared
  49. memory segment exists for key but the size of the segment associated
  50. with it is less than size and size is not 0.
  51. - ``ENOENT``. A shared memory identifier does not exist for the
  52. argument key and ``(shmflg & IPC_CREAT)`` is 0.
  53. - ``ENOMEM``. A shared memory identifier and associated shared memory
  54. segment will be created, but the amount of available physical memory
  55. is not sufficient to fill the request.
  56. - ``ENOSPC``. A shared memory identifier is to be created, but the
  57. system-imposed limit on the maximum number of allowed shared memory
  58. identifiers system-wide would be exceeded.
  59. **POSIX Deviations**
  60. - The values of ``shm_perm.cuid``, ``shm_perm.uid``, ``shm_perm.cgid``,
  61. and ``shm_perm.gid`` should be set equal to the effective user ID and
  62. effective group ID, respectively, of the calling process. The NuttX
  63. ``ipc_perm`` structure, however, does not support these fields
  64. because user and group IDs are not yet supported by NuttX.
  65. .. c:function:: void *shmat(int shmid, FAR const void *shmaddr, int shmflg)
  66. Attaches the shared memory
  67. segment associated with the shared memory identifier specified by
  68. ``shmid`` to the address space of the calling process. The segment is
  69. attached at the address specified by one of the following criteria:
  70. - If ``shmaddr`` is a null pointer, the segment is attached at the
  71. first available address as selected by the system.
  72. - If ``shmaddr`` is not a null pointer and ``(shmflg & SHM_RND)`` is
  73. non-zero, the segment is attached at the address given by
  74. ``(shmaddr - ((uintptr_t)shmaddr % SHMLBA))``.
  75. - If ``shmaddr`` is not a null pointer and ``(shmflg & SHM_RND)`` is 0,
  76. the segment is attached at the address given by ``shmaddr``.
  77. - The segment is attached for reading if ``(shmflg & SHM_RDONLY)`` is
  78. non-zero and the calling process has read permission; otherwise, if
  79. it is 0 and the calling process has read and write permission, the
  80. segment is attached for reading and writing.
  81. :param shmid: Shared memory identifier
  82. :param smaddr: Determines mapping of the shared memory region
  83. :param shmflg: See ``SHM_*`` definitions in ``include/sys/shm.h``. Only
  84. ``SHM_RDONLY`` and ``SHM_RND`` are supported.
  85. :return: Upon successful completion, ``shmat()`` will
  86. increment the value of ``shm_nattch`` in the data structure associated
  87. with the shared memory ID of the attached shared memory segment and
  88. return the segment's start address. Otherwise, the shared memory segment
  89. will not be attached, ``shmat()`` will return -1, and ``errno`` will be
  90. set to indicate the error.
  91. - ``EACCES``. Operation permission is denied to the calling process
  92. - ``EINVAL``. The value of ``shmid`` is not a valid shared memory
  93. identifier, the ``shmaddr`` is not a null pointer, and the value of
  94. ``(shmaddr -((uintptr_t)shmaddr % SHMLBA))`` is an illegal address
  95. for attaching shared memory; or the ``shmaddr`` is not a null
  96. pointer, ``(shmflg & SHM_RND)`` is 0, and the value of ``shmaddr`` is
  97. an illegal address for attaching shared memory.
  98. - ``EMFILE``. The number of shared memory segments attached to the
  99. calling process would exceed the system-imposed limit.
  100. - ``ENOMEM``. The available data space is not large enough to
  101. accommodate the shared memory segment.
  102. .. c:function:: int shmctl(int shmid, int cmd, FAR struct shmid_ds *buf)
  103. Provides a variety of shared
  104. memory control operations as specified by ``cmd``. The following values
  105. for ``cmd`` are available:
  106. - ``IPC_STAT``. Place the current value of each member of the
  107. ``shmid_ds`` data structure associated with ``shmid`` into the
  108. structure pointed to by ``buf``.
  109. - ``IPC_SET``. Set the value of the ``shm_perm.mode`` member of the
  110. ``shmid_ds`` data structure associated with ``shmid`` to the
  111. corresponding value found in the structure pointed to by ``buf``.
  112. - ``IPC_RMID``. Remove the shared memory identifier specified by
  113. ``shmid`` from the system and destroy the shared memory segment and
  114. ``shmid_ds`` data structure associated with it.
  115. :param shmid: Shared memory identifier
  116. :param cmd: ``shmctl()`` command
  117. :param buf: Data associated with the ``shmctl()`` command
  118. :return: Upon successful completion, ``shmctl()`` will return
  119. 0; otherwise, it will return -1 and set ``errno`` to indicate the error.
  120. - ``EACCES``. The argument ``cmd`` is equal to ``IPC_STAT`` and the
  121. calling process does not have read permission.
  122. - ``EINVAL``. The value of ``shmid`` is not a valid shared memory
  123. identifier, or the value of ``cmd``\ is not a valid command.
  124. - ``EPERM``. The argument ``cmd`` is equal to ``IPC_RMID`` or
  125. ``IPC_SET`` and the effective user ID of the calling process is not
  126. equal to that of a process with appropriate privileges and it is not
  127. equal to the value of ``shm_perm.cuid`` or ``shm_perm.uid`` in the
  128. data structure associated with ``shmid``.
  129. - ``EOVERFLOW``. The ``cmd`` argument is ``IPC_STAT`` and the ``gid``
  130. or ``uid`` value is too large to be stored in the structure pointed
  131. to by the ``buf`` argument.
  132. **POSIX Deviations**
  133. - ``IPC_SET``. Does not set the ``shm_perm.uid`` or
  134. ``shm_perm.gid``\ members of the ``shmid_ds`` data structure
  135. associated with ``shmid`` because user and group IDs are not yet
  136. supported by NuttX
  137. - ``IPC_SET``. Does not restrict the operation to processes with
  138. appropriate privileges or matching user IDs in ``shmid_ds`` data
  139. structure associated with ``shmid``. Again because user IDs and
  140. user/group privileges are are not yet supported by NuttX
  141. - ``IPC_RMID``. Does not restrict the operation to processes with
  142. appropriate privileges or matching user IDs in ``shmid_ds`` data
  143. structure associated with ``shmid``. Again because user IDs and
  144. user/group privileges are are not yet supported by NuttX
  145. .. c:function:: int shmdt(FAR const void *shmaddr)
  146. Detaches the shared memory
  147. segment located at the address specified by ``shmaddr`` from the address
  148. space of the calling process.
  149. :param shmid: Shared memory identifier
  150. :return: Upon successful completion, ``shmdt()`` will
  151. decrement the value of ``shm_nattch`` in the data structure associated
  152. with the shared memory ID of the attached shared memory segment and
  153. return 0.
  154. Otherwise, the shared memory segment will not be detached, ``shmdt()``
  155. will return -1, and ``errno`` will be set to indicate the error.
  156. - ``EINVAL``. The value of ``shmaddr`` is not the data segment start
  157. address of a shared memory segment.