This allocates a shared memory segment and returns the shared memory identifier that's associated with the key. Here is its syntax:
int shmget(key_t key, size_t size, int shmflg);
Here, we need to address the following:
- key: This is (usually) the value that is returned by invoking the ftok function. You can also set the value of the key as IPC_PRIVATE if you don't want the shared memory to be accessed by other processes.
- size: Represents the size of the desired shared memory segment.
- shmflg: This can be any of the following constants:
- IPC_CREAT: This creates a new segment if no shared memory identifier exists for the specified key. If this flag is not used, the function returns the shared memory segment associated with the key.
- IPC_EXCL: This makes the shmget function fail if the segment already exists with the specified key.
On successful execution, the function returns the shared memory identifier in the form of a non-negative integer, otherwise it returns -1.