- get shared memory segment identifier
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> int shmget(key_t key, size_t size, int shmflg);
The shmget() function returns the shared memory identifier associated with key.
A shared memory identifier and associated data structure and shared memory segment of at least size bytes (see Intro(2)) are created for key if one of the following are true:
The key argument is equal to IPC_PRIVATE.
The key argument does not already have a shared memory identifier associated with it, and (shmflg&IPC_CREAT) is true.
Upon creation, the data structure associated with the new shared memory identifier is initialized as follows:
The values of shm_perm.cuid, shm_perm.uid, shm_perm.cgid, and shm_perm.gid are set equal to the effective user ID and effective group ID, respectively, of the calling process.
The access permission bits of shm_perm.mode are set equal to the access permission bits of shmflg. shm_segsz is set equal to the value of size.
The values of shm_lpid, shm_nattch shm_atime, and shm_dtime are set equal to 0.
The shm_ctime is set equal to the current time.
Shared memory segments must be explicitly removed after the last reference to them has been removed.
Upon successful completion, a non-negative integer representing a shared memory identifier is returned. Otherwise, -1 is returned and errno is set to indicate the error.
The shmget() function will fail if:
A shared memory identifier exists for key but operation permission (see Intro(2)) as specified by the low-order 9 bits of shmflg would not be granted.
A shared memory identifier exists for key but both (shmflg&IPC_CREAT) and (shmflg&IPC_EXCL) are true.
The size argument is less than the system-imposed minimum or greater than the system-imposed maximum. See NOTES.
A shared memory identifier exists for key but the size of the segment associated with it is less than size and size is not equal to 0.
A shared memory identifier does not exist for key and (shmflg&IPC_CREAT) is false.
A shared memory identifier and associated shared memory segment are to be created but the amount of available memory is not sufficient to fill the request.
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. See NOTES.
See attributes(5) for descriptions of the following attributes:
The project.max-shm-memory resource control restricts the total amount of shared memory a project can allocate. The system-imposed maximum on the size of a shared memory segment is therefore a function of the sizes of any other shared memory segments the calling project might have allocated that are still in use. For accounting purposes, segment sizes are rounded up to the nearest multiple of the system page size. See getpagesize(3C).
The system-imposed limit on the number of shared memory identifiers is maintained on a per-project basis using the project.max-shm-ids resource control.