Goktug
Goktug

Reputation: 923

Flag value of shmat() function

This function is used for attaching allocated memory segment to the calling process. It takes three arguments. First argument corresponds to identifier of memory segment. Second argument is pointer to memory segment. For second argument, NULL or 0 value is passed to the function, since when we allocate the shared memory, we know only its identifier not its memory address.

However, I cannot find what the task of third argument is. Some codes that I am encountered by set the flag value to 0. NULL and 0 have same meaning in C language, and I think that additional adjustments are not needed; hence, NULL is passed to the function as third argument.

Is there anyone who can explain the task of flag value in shmat() function ?

Upvotes: 1

Views: 1594

Answers (1)

Johan Myréen
Johan Myréen

Reputation: 226

Four flags are defined:

  1. SHM_RDONLY - the segment is attached for reading; default is Read/Write
  2. SHM_RND - the attach occurrs at the address equal to shmaddr rounded down to the nearest multiple of SHMLBA (usually defined as the page size)
  3. SHM_REMAP - flag may be specified in shmflg to indicate that the mapping of the segment should replace any existing mapping in the range starting at shmaddr and continuing for the size of the segment. This flag is Linux-specific.
  4. SHM_EXEC - allow the contents of the segment to be executed. Linux-specific.

Passing the value 0 means that all flags are unset. I wouldn't use NULL here, since NULL implies the parameter type is a pointer, which it is not.

See the shmat(2) man page.

Upvotes: 2

Related Questions