Description
Shared memory is the fastest available IPC format. It allows multiple unrelated (unrelated) processes to access the same part of the Logical Memory.
To transmit data between two processes, shared memory is an extremely efficient solution. Once such a memory zone is mapped to the address space of the process that shares it, data transmission between these processes will no longer involve the kernel. In this way, the system call time can be reduced and the program efficiency can be improved.
Shared Memory is a special address range created by IPC for a process. It will appear in the address space of the process. Other processes can "Connect" the same shared memory segment to their own address space. All processes can access the addresses in the shared memory. If a process writes data to the shared memory, the changes will be immediately seen by other processes that access the same shared memory.
Note that the shared memory itself does not provide any synchronization function. That is to say, there is no automatic function to prevent the second process from reading the shared memory before the write operation of the first process ends. The programmer is responsible for the synchronization of shared memory access. Optional Synchronization Methods include mutex lock, condition variable, read/write lock, record lock, and traffic signal.
In fact, during Memory sharing between processes, the ing is not always removed after a small amount of data is read and written. When there is new communication, the shared memory area is re-established. Instead, keep the shared area until the communication is complete.
POSIX shared memory API
The following APIs are required to use POSIX shared memory:
#include <sys/types.h>#include <sys/stat.h> /* For mode constants */#include <sys/mman.h>#include <fcntl.h> /* For O_* constants */#include <unistd.h>int shm_open(const char *name, int oflag, mode_t mode);int shm_unlink(const char *name);int ftruncate(int fildes, off_t length);void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);int munmap(void *addr, size_t len);int close(int fildes);int fstat(int fildes, struct stat *buf);int fchown(int fildes, uid_t owner, gid_t group);int fchmod(int fildes, mode_t mode);
Shm_open
The usage of the plug-in and open a new shared memory object or open an existing shared memory object is similar to that of the function open. the function return value is a file descriptor and will be used by the following API.
Ftruncate
Set the size of the shared memory object. The size of the newly created shared memory object is 0.
MMAP
Maps shared memory objects to the virtual address space of the calling process.
Munmap
Cancels the ing from the shared memory object to the virtual address space of the calling process.
Shm_unlink
Deletes a Shared Memory Object Name.
Close
When the file descriptor returned by the shm_open function is no longer used, use the close function to close it.
Fstat
The stat struct that obtains the attributes of shared memory objects. The struct contains the size (st_size), permission (st_mode), owner (st_uid), and owner group (st_gid) of shared memory objects ).
Fchown
Changes the ownership of a shared memory object.
Fchmod
Change the permission of a shared memory object.
Persistence)
POSIX shared memory continues with the kernel: Shared Memory objects will exist until the system is closed or a process cancels the memory ing (unmapped) and calls shm_unlink to delete shared memory objects.
Linking)
Must be linked to the RT (librt) library, that is, the real-time library.
-lrt
Accessing shared memory objects via the File System
In Linux, shared memory objects are created in the Virtual File System (tmpfs) and are usually mounted under the/dev/SHM directory..
Linux supports POSIX shared memory from kernel 2.4 and glibc 2.2.
Starting from Linux kernel 2.6.19, Linux supports using the access control list (ACLs) to control the permissions of objects in the virtual file system.
Inherit
To be continued...
Conforming)
POSIX.1-2001.
Notes
Multi-process must use the synchronous mechanism to access Shared Memory objects, such as using POSIX semaphores.
The shared memory of System V is outdated. POSIX shared memory provides APIs that are simpler to use and more reasonable to design.