Memory-mapped I/O lets us map a
file on disk into a buffer in memory so that, when we fetch bytes from the
buffer, the corresponding bytes of the file are read. Similarly, when we store
data in the buffer, the corresponding bytes are automatically written to the
file. This lets us perform I/O without using read or
write.
To use this feature, we have to tell the kernel to map a given file to a region
in memory. This is done by the mmap function.
#include <sys/mman.h>
void *mmap(void *addr, size_t len, int prot, int
flag, int filedes,
off_t off );
Returns: starting address of mapped region if OK, MAP_FAILED on error
|
The addr argument lets us specify the address of
where we want the mapped region to start. We normally set this to 0 to allow the
system to choose the starting address. The return value of this function is the
starting address of the mapped area.
The filedes argument is the file descriptor
specifying the file that is to be mapped. We have to open this file before we
can map it into the address space. The len
argument is the number of bytes to map, and off
is the starting offset in the file of the bytes to map.The value of off and the value of addr (if MAP_FIXED is specified) are required
to be multiples of the system's virtual memory page size.
The prot argument specifies the
protection of the mapped region.We can specify the protection as either PROT_NONE or
the bitwise OR of any combination of PROT_READ, PROT_WRITE,
and PROT_EXEC. The protection specified for a region can't allow more
access than the open mode of the file. For example, we can't specify
PROT_WRITE if the file was opened read-only.
The flag argument affects various attributes of
the mapped region.
MAP_FIXED The return value must equal addr.
MAP_SHARED This flag specifies that store operations modify the mapped filethat is, a store
operation is equivalent to a write to the file. MAP_PRIVATE This flag says that store operations into the mapped region cause a private copy
of the mapped file to be created. |
Two signals are normally used with mapped regions. SIGSEGV is the
signal normally used to indicate that we have tried to access memory that is not
available to us. This signal can also be generated if we try to store into a
mapped region that we specified to mmap as read-only. The
SIGBUS signal can be generated if we access a portion of the mapped
region that does not make sense at the time of the access. For example, assume
that we map a file using the file's size, but before we reference the mapped
region, the file's size is truncated by some other process. If we then try to
access the memory-mapped region corresponding to the end portion of the file
that was truncated, we'll receive SIGBUS.
A memory-mapped region is inherited by a child across a fork (since
it's part of the parent's address space), but for the same reason, is not
inherited by the new program across an exec.
If the pages in a shared mapping have been modified, we can call msync
to flush the changes to the file that backs the mapping. The msync
function is similar to fsync (), but works on
memory-mapped regions.
#include <sys/mman.h>
int msync(void *addr, size_t len, int flags);
Returns: 0 if OK, 1 on error
|
The flags argument allows us
some control over how the memory is flushed. We can specify the
MS_ASYNC flag to simply schedule the pages to be written. If we want to
wait for the writes to complete before returning, we can use the
MS_SYNC flag. Either MS_ASYNC or MS_SYNC must be
specified.
An optional flag, MS_INVALIDATE, lets us tell the
operating system to discard any pages that are out of sync with the underlying
storage. Some implementations will discard all pages in the specified range when
we use this flag, but this behavior is not required.
A memory-mapped region is automatically unmapped when the process terminates or
by calling munmap directly. Closing the file descriptor filedes does not unmap the region.
#include <sys/mman.h>
int munmap(caddr_t addr, size_t len);
Returns: 0 if OK, 1 on error
|
munmap does not affect the object that was mappedthat is, the call to
munmap does not cause the contents of the mapped region to be written
to the disk file. The updating of the disk file for a
MAP_SHARED region
happens automatically by the kernel's virtual memory algorithm as we store into
the memory-mapped region. Modifications to memory in a
MAP_PRIVATE
region are discarded when the region is unmapped.
阅读(833) | 评论(0) | 转发(0) |