内容简介
#include <sys/mman.h> void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) int munmap(void *start, size_t length) |
描述
The%uA0mmap() function asks to map%uA0length%uA0bytes starting at offset%uA0offset%uA0from the file (or other object) specified by the file descriptor%uA0fd%uA0into memory, preferably at address%uA0start. This latter address is a hint only, and is usually specified as 0. The actual place where the object is mapped is returned by%uA0mmap().The%uA0prot%uA0argument describes the desired memory protection (and must not conflict with the open mode of the file). It is either%uA0PROT_NONE%uA0or is the bitwise OR of one or more of the other PROT_* flags.
标签 | 描述 |
---|---|
PROT_EXEC | Pages may be executed. |
PROT_READ | Pages may be read. |
PROT_WRITE | Pages may be written. |
PROT_NONE | Pages may not be accessed. |
标签 | 描述 |
---|---|
MAP_FIXED | Do not select a different address than the one specified. If the memory region specified by%uA0start%uA0and%uA0len%uA0overlaps pages of any existing mapping(s), then the overlapped part of the existing mapping(s) will be discarded. If the specified address cannot be used,%uA0mmap() will fail. If%uA0MAP_FIXED%uA0is specified,%uA0start%uA0must be a multiple of the page size. Use of this option is discouraged. |
MAP_SHARED | Share this mapping with all other processes that map this object. Storing to the region is equivalent to writing to the file. The file may not actually be updated until%uA0msync(2) ormunmap(2) are called. |
MAP_PRIVATE | Create a private copy-on-write mapping. Stores to the region do not affect the original file. It is unspecified whether changes made to the file after the%uA0mmap() call are visible in the mapped region. |
The above three flags are described in POSIX.1-2001. Linux also knows about the following non-standard flags:
标签 | 描述 |
---|---|
MAP_DENYWRITE | |
%uA0 | This flag is ignored. (Long ago, it signalled that attempts to write to the underlying file should fail with%uA0ETXTBUSY. But this was a source of denial-of-service attacks.) |
MAP_EXECUTABLE | |
%uA0 | This flag is ignored. |
MAP_NORESERVE | |
%uA0 | Do not reserve swap space for this mapping. When swap space is reserved, one has the guarantee that it is possible to modify the mapping. When swap space is not reserved one might get SIGSEGV upon a write if no physical memory is available. See also the discussion of the file%uA0/proc/sys/vm/overcommit_memoryin%uA0proc(5). In kernels before 2.6, this flag only had effect for private writable mappings. |
MAP_LOCKED%uA0(since Linux 2.5.37) | |
%uA0 | Lock the pages of the mapped region into memory in the manner of%uA0mlock().%uA0This flag is ignored in older kernels. |
MAP_GROWSDOWN | |
%uA0 | Used for stacks. Indicates to the kernel VM system that the mapping should extend downwards in memory. |
MAP_ANONYMOUS | |
%uA0 | The mapping is not backed by any file the%uA0fd%uA0and%uA0offsetarguments are ignored. The use of this flag in conjunction withMAP_SHARED%uA0is only supported on Linux since kernel 2.4. |
MAP_ANON | |
%uA0 | Alias for%uA0MAP_ANONYMOUS. Deprecated. |
MAP_FILE | |
%uA0 | Compatibility flag. Ignored. |
MAP_32BIT | |
%uA0 | Put the mapping into the first 2GB of the process address space. Ignored when%uA0MAP_FIXED%uA0is set. This flag is currently only supported on x86-64 for 64bit programs. |
MAP_POPULATE%uA0(since Linux 2.5.46) | |
%uA0 | Populate (prefault) page tables for a file mapping, by performing read-ahead on the file. Later accesses to the mapping will not be bocked by page faults. |
MAP_NONBLOCK%uA0(since Linux 2.5.46) | |
%uA0 | Only meaningful in conjunction with%uA0MAP_POPULATE. Don’t perform read-ahead: only create page tables entries for pages that are already present in RAM. |
fd%uA0should be a valid file descriptor, unless%uA0MAP_ANONYMOUS%uA0is set. IfMAP_ANONYMOUS%uA0is set, then%uA0fd%uA0is ignored on Linux. However, some implementations require%uA0fd%uA0to be -1 if%uA0MAP_ANONYMOUS%uA0(or%uA0MAP_ANON) is specified, and portable applications should ensure this.
offset%uA0should be a multiple of the page size as returned by%uA0getpagesize(2).
Memory mapped by%uA0mmap() is preserved across%uA0fork(2), with the same attributes.
A file is mapped in multiples of the page size. For a file that is not a multiple of the page size, the remaining memory is zeroed when mapped, and writes to that region are not written out to the file. The effect of changing the size of the underlying file of a mapping on the pages that correspond to added or removed regions of the file is unspecified.
The%uA0munmap() system call deletes the mappings for the specified address range, and causes further references to addresses within the range to generate invalid memory references. The region is also automatically unmapped when the process is terminated. On the other hand, closing the file descriptor does not unmap the region.
The address%uA0start%uA0must be a multiple of the page size. All pages containing a part of the indicated range are unmapped, and subsequent references to these pages will generate SIGSEGV. It is not an error if the indicated range does not contain any mapped pages.
For file-backed mappings, the%uA0st_atime%uA0field for the mapped file may be updated at any time between the%uA0mmap() and the corresponding unmapping the first reference to a mapped page will update the field if it has not been already.
The%uA0st_ctime%uA0and%uA0st_mtime%uA0field for a file mapped with%uA0PROT_WRITE%uA0andMAP_SHARED%uA0will be updated after a write to the mapped region, and before a subsequent%uA0msync() with the%uA0MS_SYNC%uA0or%uA0MS_ASYNC%uA0flag, if one occurs.
返回值
On success,%uA0mmap() returns a pointer to the mapped area. On error, the valueMAP_FAILED%uA0(that is, (void *) -1) is returned, and%uA0errno%uA0is set appropriately. On success,%uA0munmap() returns 0, on failure -1, and%uA0errno%uA0is set (probably to%uA0EINVAL).注意
It is architecture dependent whether%uA0PROT_READ%uA0includes%uA0PROT_EXEC%uA0or not. Portable programs should always set%uA0PROT_EXEC%uA0if they intend to execute code in the new mapping.错误
标签 | 描述 |
---|---|
EACCES | A file descriptor refers to a non-regular file. Or%uA0MAP_PRIVATEwas requested, but%uA0fd%uA0is not open for reading. Or%uA0MAP_SHAREDwas requested and%uA0PROT_WRITE%uA0is set, but%uA0fd%uA0is not open in read/write (O_RDWR) mode. Or%uA0PROT_WRITE%uA0is set, but the file is append-only. |
EAGAIN | The file has been locked, or too much memory has been locked (see%uA0setrlimit(2)). |
EBADF | fd%uA0is not a valid file descriptor (and%uA0MAP_ANONYMOUS%uA0was not set). |
EINVAL | We don’t like%uA0start%uA0or%uA0length%uA0or%uA0offset. (E.g., they are too large, or not aligned on a page boundary.) |
ENFILE | The system limit on the total number of open files has been reached. |
ENODEV | The underlying filesystem of the specified file does not support memory mapping. |
ENOMEM | No memory is available, or the process’s maximum number of mappings would have been exceeded. |
EPERM | The%uA0prot%uA0argument asks for%uA0PROT_EXEC%uA0but the mapped area belongs to a file on a filesystem that was mounted no-exec. |
ETXTBSY | |
%uA0 | MAP_DENYWRITE%uA0was set but the object specified by%uA0fd%uA0is open for writing. |
Use of a mapped region can result in these signals: | |
SIGSEGV | |
%uA0 | Attempted write into a region mapped as read-only. |
SIGBUS | Attempted access to a portion of the buffer that does not correspond to the file (for example, beyond the end of the file, including the case where another process has truncated the file). |
可用性
On POSIX systems on which%uA0mmap(),%uA0msync() and%uA0munmap() are available,_POSIX_MAPPED_FILES%uA0is defined in <unistd.h> to a value greater than 0. (See alsosysconf(3).)遵循于
SVr4, 4.4BSD, POSIX.1-2001.BUGS
On Linux there are no guarantees like those suggested above underMAP_NORESERVE. By default, any process can be killed at any moment when the system runs out of memory.In kernels before 2.6.7, the%uA0MAP_POPULATE%uA0flag only has effect if%uA0prot%uA0is specified asPROT_NONE.