Linux allows any process to image a disk file into memory. There are two main benefits of using a memory image file-accelerating file I/O operations, different I/O calls such as read or fputs reading or writing data through the kernel buffer. Although Linux has a fast and advanced disk buffering algorithm, the fastest disk access is always slower than the slowest memory access. – Data is shared, and if multiple processes are accessing the same data, the data can be stored in a memory image file that can be accessed by all processes.
mmap function
#include <unistd.h>
#include <sys/mman.h>
void *mmap (void *start, size_t length, int prot, int flags, int fd, off_t offset)
In the file descriptor FD Specifies the open file, starting at offset from the beginning of the file image file to where the memory start begins. Offset is typically 0, which means that the entire file is imaged into memory. length specifies the size of the file to be imaged. Execution successfully returns a pointer to the memory area, failure returns-1, and sets the errno variable
parameter Prot---protection mode– Its value can be logical "or"
Value |
Description |
Prot_none |
Do not allow access |
Prot_read |
Memory readable |
Prot_write |
Memory writable |
Prot_exec |
Memory executable |
Parameter Flags---the properties of the image – its value can be logical "or"
Value |
Description |
Map_fixed |
Fails if start is invalid or is in use |
Map_private |
The write operation to the image memory area is private to the process |
Map_shared |
Write operations to the image memory area are also copied to the file |
The image memory area must be marked private with map_private or map_shared labeled as shareable.
Munmap function
int Munmap (void *start, size_t length)
When a memory image file is used, it is necessary to call Munmap to unlock the memory image and return the memory release to the operating system. The parameter start points to the starting position of the memory area where you want to remove the image. The length of the parameter specifies the size of the memory area to be freed. Execution successfully returns a pointer to the memory area, failure returns-1, and sets the errno variable. When a piece of memory is de-imaged, then attempting to access start causes a segment error when a process terminates, all memory images are released
examples of mmap and MUNMAP functions
int Main () { int fdin = open ("aaa.txt", o_rdonly); struct stat statbuf; &statbuf); = statbuf.st_size; char *s = mmap (00); printf (s); Munmap (S, Len); Close (Fdin); return 0 ;}
Msync Function
int Msync (const void *start, size_t length, int flags)
The Msync function writes the file being imaged to disk. The parameter flags can make one or more of the following values "or". –ms_async-returns after a write operation is invoked. –ms_sync-writes the data before Msync returns. –ms_invalidate-the images to the same file are not valid for the new data to update them. Msync Function Example
intMain () {intFdin = open ("Aaa.txt", O_RDWR); structstat statbuf; Fstat (Fdin,&statbuf); off_t Len=statbuf.st_size; Char*s = Mmap (0, Len, Prot_write | Prot_read, map_shared, Fdin,0); printf (s); strcpy (s),"Hello World"); Msync (S, Len, Ms_sync); Munmap (S, Len); Close (Fdin); return 0;}
Lock Memory
int Mlock (const void *start, size_t len);
int Munlock (void *start, size_t len);
For Linux systems, if memory is not accessed over a period of time, memory data may be temporarily written to the swap partition (a special area of the disk). Locking memory is the setting of a flag that prevents the system from writing memory data to the swap partition. Only the root user has permission to lock the memory area.
int Mlock (const void *start, size_t len);
int Munlock (void *start, size_t len);
For Linux systems, if memory is not accessed over a period of time, memory data may be temporarily written to the swap partition (a special area of the disk). Locking memory is the setting of a flag that prevents the system from writing memory data to the swap partition. Only the root user has permission to lock the memory area.Copy to Google TranslateTranslation Results
Memory image File