A memory image is a way to improve file read speed by mapping files to memory.
You can use the pointer to quickly access the data in the file
1.
Functions that are used
To map externally stored files to memory, you need to use the
#include <sys/types.h>#include<sys/mman.h>void *mmap (void *start.size_t length,int prot,int flag,int fd,off_t offset);
At this point, if start is not NULL, the file will be mapped to this location, but it is not guaranteed to succeed;
Length indicates the size of the memory image occupied, byte-written;
The prot represents the security properties of the memory image, and the options are:
Prot_exec // mapped memory may be executable prot_none // mapped memory inaccessible prot_read // memory that is mapped is readable Prot_write // mapped memory writable
Flag indicates the memory image's flags
Map_fixed // If an image cannot be created at start, an error is returned map_reivate // changes to memory are not reflected in the external memory file map_shared // Save changes in memory to the external memory file
off_t represents the offset of the content of the image from the file header
Call failed return-1, otherwise the image memory start address is returned
Note You need to open a file in a process to map it * * *
2.
Modify the protection value of the memory image prot
int Protect (constvoid *addr,size_t length,int prot);
Success returns 0, error returns-1, and errno is placed.
3.
Write memory image to external memory
int msync (constvoid *start,size_t length,int flag); flag parameter options are: Ms_async // dispatches a write operation and returns Ms_invalidate // image to the same file is not valid for update to new data Ms_sync // write function returned after completion
4.
Modify Image Memory Size
void *mremap (voidlong flag)
The parameter flag indicates whether the location of the image is moved when needed
The call returns the new address of the image successfully, otherwise the error value-1
Memory image Io-linux C Programming Guide