| Article Index |
|---|
| Shared Memory Basics |
| Page 2 |
| Page 3 |
| Page 4 |
| All Pages |
Shared Memory Basics
Basic Concepts
Shared memory can best be described as the mapping of an area (segment) of memory that will be mapped and shared by more than one process. This is by far the fastest form of IPC, because there is no intermediation (i.e. a pipe, a message queue, etc). Instead, information is mapped directly from a memory segment, and into the addressing space of the calling process. A segment can be created by one process, and subsequently written to and read from by any number of processes.
Internal and User Data Structures
Let's briefly look at data structures maintained by the kernel for shared memory segments.
Kernel shmid_ds structure
As with message queues and semaphore sets, the kernel maintains a special internal data structure for each shared memory segment which exists within its addressing space. This structure is of type shmid_ds, and is defined in linux/shm.h as follows:
/* One shmid data structure for each shared memory segment in the system. */
struct shmid_ds {
struct ipc_perm shm_perm; /* operation perms */
int shm_segsz; /* size of segment (bytes) */
time_t shm_atime; /* last attach time */
time_t shm_dtime; /* last detach time */
time_t shm_ctime; /* last change time */
unsigned short shm_cpid; /* pid of creator */
unsigned short shm_lpid; /* pid of last operator */
short shm_nattch; /* no. of current attaches */
/* the following are private */
unsigned short shm_npages; /* size of segment (pages) */
unsigned long *shm_pages; /* array of ptrs to frames -> SHMMAX */
struct vm_area_struct *attaches; /* descriptors for attaches */
};
Operations on this structure are performed by a special system call, and should not be tinkered with directly. Here are descriptions of the more pertinent fields:
shm_perm
This is an instance of the ipc_perm structure, which is defined for us in linux/ipc.h. This holds the permission information for the segment, including the access permissions, and information about the creator of the segment (uid, etc).
shm_segsz
Size of the segment (measured in bytes).
shm_atime
Time the last process attached the segment.
shm_dtime
Time the last process detached the segment.
shm_ctime
Time of the last change to this structure (mode change, etc).
shm_cpid
The PID of the creating process.
shm_lpid
The PID of the last process to operate on the segment.
shm_nattch
Number of processes currently attached to
the segment.




