Shared Memory helps in passing on data between programs. A memory portion created by a program can be accessed by other processes, if allowed. In other words, Shared memory is an extra portion of memory that is attached to some address spaces, which the owners can use. Here all the processes can access and share the same memory segment. Shared memory is a feature that is supported by UNIX System V, including Linux, SunOS and Solaris systems.
Using the key, one process will explicitly ask for a portion to be shared by other processes. This process will be then called in the server. All other processes / clients who know the shared area can now access the same. The disadvantage of shared memory is that it lacks in security feature as there is no protection to a shared memory and any process that knows it can freely access it. A synchronization protocol should be setup to protect a shared memory from being accessed at the same time by several processes.
A shared memory segment is identified by unique integers, which are known as the shared memory IDs. The shared memory itself is described by a structure of type ‘shmid_ds in header file sys/shm.h’. To use this file, files sys/types.h and sys/ipc.h’ should be included. Therefore, the program should start with the following lines:
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h>
In the Solaris 2.x operating system, the most efficient way to implement shared memory application is to rely on the mmap() function and on the system’s native virtual memories facilities. Solaris 2.x also supports System V shared memories, which is another way to let multiple processes attach the segment of physical memory to their virtual address space. When write access is allowed for more than one processes, an outside protocol or mechanism such as a semaphore can be used to prevent the inconsistencies and collisions. shmctl() is used to alter the permission and other characteristics of a shared memory segment. It is prototyped as follows:
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
The processes must have an effective shmid of owner, creator or super-user to perform this command. “The cmd argument is one of the following control commands: SHM_LOCK — Locks the specified shared memory segments in memory. The process must have the effective ID of super-user to perform this command. SHM_UNLOCK — Unlocks the shared memory segments. The process must have the effective ID of superuser to perform this command. IPC_STAT — Returns the status information contained in the control structure and place it in the buffer pointed to by buffer. The process must have read permissions on the segment to perform these commands. IPC_SET — Set the effective user, group identification and access permissions. The process must have an effective ID of owner, creator or super-user to perform this command. IPC_RMID — Remove the shared memory segment. “
The shared memory systems is relatively easy to program, since all the processors share a single view of data and the communication between processors can be really fast, as memory is always accessed to the same location. The issues with shared memory systems are that CPUs demand fast access to memory and will rely on cache memory, which has two complications:
1. Access time degradations: when several processors try to access the same memory locations, it causes contention.
2. Lack of data coherence: Whenever the cache is updated with information that may be used by other processor, the changes need to be reflected in the other processors as well; otherwise the different processor will be working with the incoherent data.