A process allocates a shared memory segment using shmget (SHared Memory GET ).
- First parameter is an integer key that specifies which segment to create. Unrelated processes can access the same shared segment by specifying the same key value. Unfortunately, other processes may have also chosen the same fixed key, which could lead to conflict. Using the special constant IPC_PRIVATE as the key value guarantees that a brand new memory segment is created.
- Second parameter specifies the number of bytes in the segment. Because segments are allocated using pages, the number of actually allocated bytes is rounded up to an integral multiple of the page size.
- Third parameter is the bitwise or flag values that specify options to shmget. The flag values include these:
- IPC_CREAT: This flag indicates that a new segment should be created.This permits creating a new segment while specifying a key value.
- IPC_EXCL: This flag, which is always used with
IPC_CREAT, causes shmget to fail if a segment key is specified that already exists. If this flag is not given and the key of an existing segment is used, shmget returns the existing segment instead of creating a new one.
- Mode flags; S_IRUSR and S_IWUSR specify read and write permissions for the owner of the shared memory segment, and S_IROTH and S_IWOTH specify read and write permissions for others.
int segment_id=shmget(shm_key,getpagesize(),
IPC_CREAT | S_IRUSR | S_IWUSER);