Lecture
Shared memory is the fastest way to exchange data between processes. [1]
In other means of interprocess communication (IPC), the exchange of information between processes passes through the core, which leads to a context switch between the process and the core, i.e. loss of productivity. [2]
The shared memory technique allows the exchange of information through a common memory segment for processes without the use of kernel system calls. The shared memory segment is connected to the free part of the process’s virtual address space [3]. Thus, two different processes may have different addresses of the same cell of the connected shared memory.
Visual representation of shared memory
After creating a shared memory segment, any of the user processes can connect it to its own virtual space and work with it as with a normal memory segment. The disadvantage of such information exchange is the absence of any synchronization means, however, to overcome this disadvantage, you can use the semaphore technique.
In a data exchange scheme between two processes - (client and server) using shared memory - a group of two semaphores must function. The first semaphore serves to block access to shared memory, its enabling signal is 1, and the inhibiting one to 0. The second semaphore serves to signal the server that the client has started, while access to the shared memory is blocked and the client reads data from the memory. Now when the server calls the operation, its operation will be suspended until the client releases the memory.
In software, shared memory is called:
Since both processes can access the shared memory region as normal memory, this is a very fast communication method (unlike other IPC mechanisms, such as named pipes, UNIX sockets, or CORBA). On the other hand, such a method is less flexible, for example, exchanging processes should be run on the same machine (of the IPC methods listed, only network sockets, not to be confused with UNIX domain sockets, can exchange data through the network), and you must be careful to avoid problems when using shared memory on different processor cores and hardware architecture without a coherent cache.
Data exchange via shared memory is used, for example, to transfer images between an application and an X server on Unix systems, or inside an IStream object returned by CoMarshalInterThreadInterfaceInStream in a COM library under Windows.
Dynamic libraries are usually loaded into memory once and mapped to several processes, and only pages that are specific to an individual process (since some identifiers differ) are duplicated, usually with the help of a mechanism known as copy-on-write, which when attempting to write to shared memory, it is unnoticeable for the recording process to copy the pages of memory, and then write data to this copy.
On UNIX-like operating systems
POSIX provides a standardized API for working with shared memory - POSIX Shared Memory. One of the key features of the UNIX family of operating systems is the process copying mechanism (the fork () system call), which allows you to create anonymous parts of shared memory before copying a process and inherit them from descendant processes. After copying the process, the shared memory will be available to both the parent and child processes. [3] [4]
There are two different approaches to connecting and using shared memory:
UNIX System V style shared memory
UNIX System V provides a set of C language functions that allow you to work with shared memory [7]:
Named shared memory implies an association with each memory section of a unique numeric key within the operating system, through which you can later connect shared memory in another process. [8]
POSIX shared memory
POSIX allows you to associate a file descriptor with a shared memory object, which is a more unified mechanism than the UNIX System V mechanism. The following C language functions can be used to work with memory:
On Windows operating systems
In the Windows operating system, CreateSharedMemory [13] from the Win32-SDK is used to create shared memory. On the other hand, it is possible to use the CreateFileMapping and MapViewOfFile [14] functions from MSDN.
Support in programming languages
Some C ++ libraries offer cross-platform access to working with shared memory. For example, the Boost library provides the class boost :: interprocess :: shared_memory_object [15] for POSIX-compatible operating systems, and the Qt library provides the class QSharedMemory that unifies access to shared memory for different operating systems with some restrictions [16].
In Java 7 under the GNU / Linux operating system, shared memory can be implemented by mapping a file from the / dev / shm / directory (or / run / shm /, depending on the distribution) to memory [17] using the map method of the java.nio class. MappedByteBuffer [18].
Support for shared memory implemented in many other programming languages. For example, PHP provides an API [19] for creating shared memory, whose functions are similar to POSIX functions.
Comments
To leave a comment
Operating Systems and System Programming
Terms: Operating Systems and System Programming