One, what is the shared memory area
The shared memory area is the fastest available form of IPC. It allows multiple unrelated processes to access the same part of the logical memory. Shared memory is a highly efficient solution if you need to transfer data between two running processes. Once such a memory area is mapped to the address space of the process that shares it, the transfer of these inter-process data is no longer involved in the kernel. This can reduce system call time and improve program efficiency.
Shared memory is a special address range created by the IPC for a process that will appear in the process's address space. Other processes can "connect" the same segment of shared memory to their own address space. All processes have access to the addresses in shared memory. If a process writes data to this shared memory, the changes are immediately visible to other processes that have access to the same section of shared memory.
Note that the shared memory itself does not provide any synchronization functionality. That is, before the first process finishes writing on shared memory, there is no automatic function to prevent the second process from starting to read it. The problem of access synchronization for shared memory must be the responsibility of the programmer. Optional synchronization methods include mutual exclusion lock, conditional variable, read-write lock, record lock, Semaphore.
Second, mmap
We'll introduce the following functions before sharing the memory.
The MMAP function maps a file or a POSIX shared memory area object to the address space of the calling process. There are three purposes to use this function:
1. Use normal files to provide memory mapped I/O
2. Use special files to provide anonymous memory mappings.
3. Use Shm_open to provide POSIX shared memory areas between unrelated processes.
1.
Name:: |
Mmap |
Function: |
Mapping I/O files to a storage area |
Header file: |
#include <sys/mman.h> |
Function prototype: |
void *mmap (void *addr,size_t len,int prot,int flag,int off); |
Parameters: |
Addr point to the starting address of the mapping store Len-Mapped bytes Prot protection requirements for mapped storage areas Flag Flag Sign Bit Filedes the descriptor of the file to be mapped Off to map the starting offset of a byte in a file |
return value: |
If successful, return the starting address of the mapping area and return map_failed if an error occurs |
The addr parameter is used to specify the starting address of the mapping store. It is usually set to NULL, which means that the system selects the starting address of the mapping area.
Filedes refers to the descriptor of the file to be mapped. Before mapping the file to an address space, open the file first. Len is the number of bytes mapped.
Off is the starting offset to map the byte in the file. It is usually set to 0.
The prot parameter describes the protection requirements for the mapped store. You can specify the prot parameter as Prot_none, or prot_read (the mapping area is readable), prot_write (the mapping area is writable), prot_exec (the mapping area can perform) any combination of bitwise OR, or prot_none (the map area is inaccessible). Protection requirements for the specified mapping store cannot exceed file open mode access.
The flag parameter affects multiple properties of the mapping area:
Map_fixed return value must be equal to addr. This flag is discouraged because it is not conducive to portability.
Map_shared This flag describes the configuration of the storage operations performed by this process on the mapping area. This flag specifies that the storage operation modifies the mapping file.
Map_private This flag causes a private copy of the mapping file to be established for the mapping area. All subsequent references to the map area refer to the copy instead of the original file.
Note that you must specify one of the map_fixed or map_private flags, specifying that the former is an operation on the storage mapping file itself, and that the latter is operating on its copy.
The FD parameter can be closed after the MMAP is successfully returned. This operation has no effect on the mapping relationship established by Mmap. To delete a mapping relationship from the address space of a process, we call Munmap.
2.
Name:: |
Munmap |
Function: |
Releasing Storage mappings |
Header file: |
#include <sys/mman.h> |
Function prototype: |
int Munmap (caddr_t addr,size_t len); |
Parameters: |
Addr point to the starting address of the mapping store Len-Mapped bytes |
return value: |
If successful, return 0, if error, return-1 |
Where the addr parameter is the address returned by Mmap, Len is the size of the map area. Access to these addresses again causes a SIGSEGV signal to be generated to the calling process.
If the mapped area is mapped using the MAP_PRIVATE flag, the changes made to it by the calling process are discarded.
The kernel's virtual storage algorithm keeps the memory-mapped file (typically on the hard disk) synchronized with the memory-mapped area (in memory) (provided it is a map_shared memory area). This means that if we modify the contents of a location in the memory mapped to a file, the kernel will update the file accordingly at a later time. However, sometimes we want to make sure that the contents of the file on the hard disk match the contents of the file in the memory-mapped area, and call Msync to perform this synchronization.
3.
Name:: |
Msync |
Function: |
Synchronizing files to Storage |
Header file: |
#include <sys/mman.h> |