This is used to attach a shared memory segment to the given address space. That is, the shared memory identifier that's received by invoking the shmgt function needs to be associated with the address space of a process. Here is its syntax:
void *shmat(int shidtfr, const void *addr, int flag);
Here, we need to address the following:
- shidtfr: Represents the memory identifier of the shared memory segment.
- addr: Represents the address space where the segment needs to be attached. If shmaddr is a null pointer, the segment is attached at the first available address or selected by the system.
- flag: This is attached as a read-only memory if the flag is SHM_RDONLY; otherwise, it is readable and writable.
If successfully executed, the function attaches the shared memory segment and returns the segment's start address, otherwise it returns -1.