Shared Memory Interfaces
Shared memory interfaces are only available with the NuttX kernel build
(CONFIG_BUILD_KERNEL=y). These interfaces support user memory
regions that can be shared between multiple user processes. Shared
memory interfaces:
Functions
- 
int shmget(key_t key, size_t size, int shmflg)
- Returns the shared memory identifier associated with - key.- A shared memory identifier, associated data structure, and shared memory segment of at least size bytes is created for - keyif one of the following is true:- The argument - keyis equal to- IPC_PRIVATE.
- The argument - keydoes not already have a shared memory identifier associated with it and- (shmflg & IPC_CREAT)is non-zero.
 - Upon creation, the data structure associated with the new shared memory identifier will be initialized as follows: - The low-order nine bits of - shm_perm.modeare set equal to the low-order nine bits of- shmflg.
- The value of - shm_segszis set equal to the value of size.
- The values of - shm_lpid,- shm_nattch,- shm_atime, and- shm_dtimeare set equal to 0.
- The value of - shm_ctimeis set equal to the current time.
 - When the shared memory segment is created, it will be initialized with all zero values. - Parameters:
- key – The key that is used to access the unique shared memory identifier. 
- size – The shared memory region that is created will be at least this size in bytes. 
- shmflg – See - IPC_*definitions in- sys/ipc.h. Only the values- IPC_PRIVATEor- IPC_CREATare supported.
 
- Returns:
- Upon successful completion, - shmget()will return a non-negative integer, namely a shared memory identifier; otherwise, it will return -1 and set- errnoto indicate the error.- EACCES. A shared memory identifier exists for key but operation permission as specified by the low-order nine bits of- shmflgwould not be granted.
- EEXIST. A shared memory identifier exists for the argument key but- (shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)are non-zero.
- EINVAL. A shared memory segment is to be created and the value of size is less than the system-imposed minimum or greater than the system-imposed maximum.
- EINVAL. No shared memory segment is to be created and a shared memory segment exists for key but the size of the segment associated with it is less than size and size is not 0.
- ENOENT. A shared memory identifier does not exist for the argument key and- (shmflg & IPC_CREAT)is 0.
- ENOMEM. A shared memory identifier and associated shared memory segment will be created, but the amount of available physical memory is not sufficient to fill the request.
- ENOSPC. A shared memory identifier is to be created, but the system-imposed limit on the maximum number of allowed shared memory identifiers system-wide would be exceeded.
 
 - POSIX Deviations - The values of - shm_perm.cuid,- shm_perm.uid,- shm_perm.cgid, and- shm_perm.gidshould be set equal to the effective user ID and effective group ID, respectively, of the calling process. The NuttX- ipc_permstructure, however, does not support these fields because user and group IDs are not yet supported by NuttX.
 
- 
void *shmat(int shmid, FAR const void *shmaddr, int shmflg)
- Attaches the shared memory segment associated with the shared memory identifier specified by - shmidto the address space of the calling process. The segment is attached at the address specified by one of the following criteria:- If - shmaddris a null pointer, the segment is attached at the first available address as selected by the system.
- If - shmaddris not a null pointer and- (shmflg & SHM_RND)is non-zero, the segment is attached at the address given by- (shmaddr - ((uintptr_t)shmaddr % SHMLBA)).
- If - shmaddris not a null pointer and- (shmflg & SHM_RND)is 0, the segment is attached at the address given by- shmaddr.
- The segment is attached for reading if - (shmflg & SHM_RDONLY)is non-zero and the calling process has read permission; otherwise, if it is 0 and the calling process has read and write permission, the segment is attached for reading and writing.
 - Parameters:
- shmid – Shared memory identifier 
- smaddr – Determines mapping of the shared memory region 
- shmflg – See - SHM_*definitions in- include/sys/shm.h. Only- SHM_RDONLYand- SHM_RNDare supported.
 
- Returns:
- Upon successful completion, - shmat()will increment the value of- shm_nattchin the data structure associated with the shared memory ID of the attached shared memory segment and return the segment’s start address. Otherwise, the shared memory segment will not be attached,- shmat()will return -1, and- errnowill be set to indicate the error.- EACCES. Operation permission is denied to the calling process
- EINVAL. The value of- shmidis not a valid shared memory identifier, the- shmaddris not a null pointer, and the value of- (shmaddr -((uintptr_t)shmaddr % SHMLBA))is an illegal address for attaching shared memory; or the- shmaddris not a null pointer,- (shmflg & SHM_RND)is 0, and the value of- shmaddris an illegal address for attaching shared memory.
- EMFILE. The number of shared memory segments attached to the calling process would exceed the system-imposed limit.
- ENOMEM. The available data space is not large enough to accommodate the shared memory segment.
 
 
- 
int shmctl(int shmid, int cmd, FAR struct shmid_ds *buf)
- Provides a variety of shared memory control operations as specified by - cmd. The following values for- cmdare available:- IPC_STAT. Place the current value of each member of the- shmid_dsdata structure associated with- shmidinto the structure pointed to by- buf.
- IPC_SET. Set the value of the- shm_perm.modemember of the- shmid_dsdata structure associated with- shmidto the corresponding value found in the structure pointed to by- buf.
- IPC_RMID. Remove the shared memory identifier specified by- shmidfrom the system and destroy the shared memory segment and- shmid_dsdata structure associated with it.
 - Parameters:
- shmid – Shared memory identifier 
- cmd – - shmctl()command
- buf – Data associated with the - shmctl()command
 
- Returns:
- Upon successful completion, - shmctl()will return 0; otherwise, it will return -1 and set- errnoto indicate the error.- EACCES. The argument- cmdis equal to- IPC_STATand the calling process does not have read permission.
- EINVAL. The value of- shmidis not a valid shared memory identifier, or the value of- cmdis not a valid command.
- EPERM. The argument- cmdis equal to- IPC_RMIDor- IPC_SETand the effective user ID of the calling process is not equal to that of a process with appropriate privileges and it is not equal to the value of- shm_perm.cuidor- shm_perm.uidin the data structure associated with- shmid.
- EOVERFLOW. The- cmdargument is- IPC_STATand the- gidor- uidvalue is too large to be stored in the structure pointed to by the- bufargument.
 
 - POSIX Deviations - IPC_SET. Does not set the- shm_perm.uidor- shm_perm.gidmembers of the- shmid_dsdata structure associated with- shmidbecause user and group IDs are not yet supported by NuttX
- IPC_SET. Does not restrict the operation to processes with appropriate privileges or matching user IDs in- shmid_dsdata structure associated with- shmid. Again because user IDs and user/group privileges are are not yet supported by NuttX
- IPC_RMID. Does not restrict the operation to processes with appropriate privileges or matching user IDs in- shmid_dsdata structure associated with- shmid. Again because user IDs and user/group privileges are are not yet supported by NuttX
 
- 
int shmdt(FAR const void *shmaddr)
- Detaches the shared memory segment located at the address specified by - shmaddrfrom the address space of the calling process.- Parameters:
- shmid – Shared memory identifier 
 
- Returns:
- Upon successful completion, - shmdt()will decrement the value of- shm_nattchin the data structure associated with the shared memory ID of the attached shared memory segment and return 0.- Otherwise, the shared memory segment will not be detached, - shmdt()will return -1, and- errnowill be set to indicate the error.- EINVAL. The value of- shmaddris not the data segment start address of a shared memory segment.