Function: Mmap ()
Function: Creates a memory map area, maps the file data on disk to memory, and the user can modify the disk file through memory.
Advantages: High Efficiency
Cons: Do not block, trouble (for efficiency is not very high demand advice with piping)
Function Prototypes:
#include <sys/mman.h>void *mmap (voidint int int FD, off_t offset); int munmap (void *addr, size_t length);
Mmap parameter Description:
- Addr: The first address of the memory-mapped area, passed null
- Length: Map area size, can not be 0, the general length of the file to specify how large (can be obtained through Lseek)
- Prot: Map Area permissions (Prot_read, prot_write) must have Read permissions
- Flags: Set sharing or Private (map_shared, map_private)
- FD: File descriptor, open file requires Read permission
- Offset: Mapping file offsets, when mapping the file pointer offset (must be an integer multiple of 4k) general fill 0
Return value: Successfully returned the first address of the file memory map area, failed to return map_failed = = (void *) (-1)
Munmap parameter Description:
- Addr:mmap the first address of the memory-mapped area returned
- Length: Map area size
Return value: Successfully returned 0, failed to return-1
Inter-process communication:
Blood-related interprocess communication: parent-child process shared memory map area (can create anonymous memory map area)
Unrelated interprocess communication: cannot communicate through anonymous memory mapping, only with disk file communication
The mmap is non-blocking.
If you create an anonymous memory map area:
- Length needs to specify memory map area size
- The flags parameter needs to add Map_anon macros
- FD parameter should be changed to-1
Problems:
- If the return value (PTR) is the + + operation (ptr++), will munmap succeed?
No, release the memory without changing it.
- If Open is O_rdonly, Mmap Pro? Parameter Designation Prot_read | What will happen to Prot_write?
Mmap call failed, open is permission must be greater than or equal to mmap when the memory-mapped area
- What happens if the file offset is 1000?
The call fails, and the offset must be an integer multiple of 4k
- Mmap What happens when the call fails?
Question 2, 3. And then there is length = = 0 O'Clock
- Can I o_creat a new file to create a map area when open?
Yes, but the size can not be 0, need to do file expansion
- What happens if a file is written over the memory-mapped area beyond the size of the memory map?
The program does not go wrong, but the portion of the memory-mapped area is not written to the file
Search
Copy
interprocess communication (Shared memory map area)