|
MMAP (Create memory ing) |
Related functions |
Munmap, open
|
Header file |
# Include <unistd. h> # Include <sys/Mman. h>
|
Define functions |
Void * MMAP (void * Start, size_t length, int Prot, int flags, int FD, off_t offsize );
|
Function Description |
MMAP () is used to map the content of a file to the memory. Access to the memory area is to directly read and write the file content. The start parameter points to the memory start address, which is usually set to null, indicating that the system will automatically select the address. After the address is successfully matched, it will return. The length parameter indicates that the large part of the file is mapped to the memory.
|
Parameters |
Prot indicates that the protection methods of the ing region are combined as follows: The prot_exec ing area can be executed. The prot_read ing area can be read. The prot_write ing area can be written. The prot_none ing area cannot be accessed.
|
Parameters |
Flags will affect the features of the ing area. Map_fixed if the address specified by the start parameter cannot be mapped successfully, the ing is abandoned and the address is not corrected. This flag is generally not encouraged. Map_shared copies data written to the ing area back to the file and allows other processes mapped to the file to share the data. Map_private write operations on the ing area will generate a copy of The ing file, that is, private copy on write) no modifications made to this region will be written back to the original file content. Map_anonymous creates an anonymous ing. The FD parameter is ignored, and files are not involved, and the ing area cannot be shared with other processes. Map_denywrite only allows write operations on the ing area. Other operations that directly write data to the file will be rejected. Map_locked locks the ing region, which means the region will not be replaced (SWAp ). You must specify map_shared or map_private when calling MMAP. The FD parameter is the description of the file returned by open (), indicating the file to be mapped to the memory. The offset parameter is the offset of the file ing. It is usually set to 0, which indicates the correspondence starting from the beginning of the file. The offset must be an integer multiple of the page size.
|
Return Value |
If the ing is successful, the start address of memory in the ing area is returned. Otherwise, map_failed (-1) is returned. The error cause is stored in errno.
|
Error Code |
The ebadf parameter FD is not a valid file description. The eacces access permission is incorrect. If map_private is used, the file must be readable. If map_shared is used, prot_write is required and the file must be writable. Either start, length, or offset of the einval parameter is invalid. The eagain file is locked or too much memory is locked. Insufficient enomem memory.
|
Example |
/* Use MMAP () to read/etc/passwd file content */ # Include <sys/types. h> # Include <sys/STAT. h> # Include <fcntl. h> # Include <unistd. h> # Include <sys/Mman. h> Main () { Int FD; Void * start; Struct stat Sb; FD = open ("/etc/passwd", o_rdonly);/* Open/etc/passwd */ Fstat (FD, & SB);/* Get file size */ Start = MMAP (null, SB. st_size, prot_read, map_private, FD, 0 ); If (START = map_failed)/* determine whether the ing is successful */ Return; Printf ("% s", start ); Munma (START, SB. st_size);/* unmap */ Closed (FD ); }
|
Run |
Root: X: 0: Root:/root:/bin/bash Bin: X: 1: 1: Bin:/bin: Daemon: X: 2: 2: daemon:/sbin ADM: X: 3: 4: ADM:/var/adm: LP: X: 4: 7: LP:/var/spool/lpd: Sync: X: 5: 0: Sync:/sbin: Bin/Sync: Shutdown: X: 6: 0: shutdown:/sbin/Shutdown Halt: X: 7: 0: Halt:/sbin/halt Mail: X: 8: 12: Mail:/var/spool/mail: News: X: 9: 13: News:/var/spool/news: Uucp: X: 10: 14: uucp:/var/spool/uucp: Operator: X: 11: 0: Operator:/root: Games: X: 12: 100: games:/usr/games: Gopher: X: 13: 30: gopher:/usr/lib/gopher-data: FTP: X: 14: 50: FTP user:/home/ftp: Nobody: X: 99: 99: Nobody :/: XFS: 100: 101: X Font Server:/etc/Xll/Fs:/bin/false TPD: X: 42: 42:/home/TPD:/bin/bash KIDS: X: 500: 500:/home/kids:/bin/bash
|