The shared memory type of IPC allows two or more processes (executing programs) to share memory and, consequently, the data contained there. This is done by allowing processes to set up access to a common virtual memory address space. This sharing occurs on a segment basis, which is memory management hardware-dependent.
A process initially creates a shared memory segment facility using the shmget system call. Upon creation, this process sets the overall operation permissions for the shared memory segment facility, sets its size in bytes, and can specify that the shared memory segment is for reference only (read-only) upon attachment.
If the memory segment is not specified to be for reference only, all other processes with appropriate operation permissions can read from or write to the memory segment.
shmat (shared memory attach) and shmdt (shared memory detach) can be performed on a shared memory segment.
shmat allows processes to associate themselves with the shared memory segment if they have permission. They can then read or write as allowed.
shmdt allows processes to disassociate themselves from a shared memory segment. Therefore, they lose the ability to read from or write to the shared memory segment.
The original owner/creator of a shared memory
segment can relinquish ownership to another process using the
However, the creating process remains the creator until the facility is removed
or the system is reinitialized.
Other processes with permission can perform
other functions on the shared memory segment
System calls make these shared memory capabilities available to processes. The calling process passes arguments to a system call, and the system call either successfully or unsuccessfully performs its function. If the system call is successful, it performs its function and returns the appropriate information. Otherwise, a known error code (-1) is returned to the process, and the external variable errno is set accordingly.