mmap 认知

来源:互联网 发布:js控制button点击 编辑:程序博客网 时间:2024/05/21 08:59
回到顶部

mmap基础概念

mmap是一种内存映射文件的方法,即将一个文件或者其它对象映射到进程的地址空间,实现文件磁盘地址和进程虚拟地址空间中一段虚拟地址的一一对映关系。实现这样的映射关系后,进程就可以采用指针的方式读写操作这一段内存,而系统会自动回写脏页面到对应的文件磁盘上,即完成了对文件的操作而不必再调用read,write等系统调用函数。相反,内核空间对这段区域的修改也直接反映用户空间,从而可以实现不同进程间的文件共享。如下图所示:

          

由上图可以看出,进程的虚拟地址空间,由多个虚拟内存区域构成。虚拟内存区域是进程的虚拟地址空间中的一个同质区间,即具有同样特性的连续地址范围。上图中所示的text数据段(代码段)、初始数据段、BSS数据段、堆、栈和内存映射,都是一个独立的虚拟内存区域。而为内存映射服务的地址空间处在堆栈之间的空余部分。

linux内核使用vm_area_struct结构来表示一个独立的虚拟内存区域,由于每个不同质的虚拟内存区域功能和内部机制都不同,因此一个进程使用多个vm_area_struct结构来分别表示不同类型的虚拟内存区域。各个vm_area_struct结构使用链表或者树形结构链接,方便进程快速访问,如下图所示:

         

vm_area_struct结构中包含区域起始和终止地址以及其他相关信息,同时也包含一个vm_ops指针,其内部可引出所有针对这个区域可以使用的系统调用函数。这样,进程对某一虚拟内存区域的任何操作需要用要的信息,都可以从vm_area_struct中获得。mmap函数就是要创建一个新的vm_area_struct结构,并将其与文件的物理磁盘地址相连。具体步骤请看下一节。

 

回到顶部

mmap内存映射原理

mmap内存映射的实现过程,总的来说可以分为三个阶段:

(一)进程启动映射过程,并在虚拟地址空间中为映射创建虚拟映射区域

1、进程在用户空间调用库函数mmap,原型:void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);

2、在当前进程的虚拟地址空间中,寻找一段空闲的满足要求的连续的虚拟地址

3、为此虚拟区分配一个vm_area_struct结构,接着对这个结构的各个域进行了初始化

4、将新建的虚拟区结构(vm_area_struct)插入进程的虚拟地址区域链表或树中

 

(二)调用内核空间的系统调用函数mmap(不同于用户空间函数),实现文件物理地址和进程虚拟地址的一一映射关系

5、为映射分配了新的虚拟地址区域后,通过待映射的文件指针,在文件描述符表中找到对应的文件描述符,通过文件描述符,链接到内核“已打开文件集”中该文件的文件结构体(struct file),每个文件结构体维护着和这个已打开文件相关各项信息。

6、通过该文件的文件结构体,链接到file_operations模块,调用内核函数mmap,其原型为:int mmap(struct file *filp, struct vm_area_struct *vma),不同于用户空间库函数。

7、内核mmap函数通过虚拟文件系统inode模块定位到文件磁盘物理地址。

8、通过remap_pfn_range函数建立页表,即实现了文件地址和虚拟地址区域的映射关系。此时,这片虚拟地址并没有任何数据关联到主存中。

 

(三)进程发起对这片映射空间的访问,引发缺页异常,实现文件内容到物理内存(主存)的拷贝

注:前两个阶段仅在于创建虚拟区间并完成地址映射,但是并没有将任何文件数据的拷贝至主存。真正的文件读取是当进程发起读或写操作时。

9、进程的读或写操作访问虚拟地址空间这一段映射地址,通过查询页表,发现这一段地址并不在物理页面上。因为目前只建立了地址映射,真正的硬盘数据还没有拷贝到内存中,因此引发缺页异常。

10、缺页异常进行一系列判断,确定无非法操作后,内核发起请求调页过程。

11、调页过程先在交换缓存空间(swap cache)中寻找需要访问的内存页,如果没有则调用nopage函数把所缺的页从磁盘装入到主存中。

12、之后进程即可对这片主存进行读或者写的操作,如果写操作改变了其内容,一定时间后系统会自动回写脏页面到对应磁盘地址,也即完成了写入到文件的过程。

注:修改过的脏页面并不会立即更新回文件中,而是有一段时间的延迟,可以调用msync()来强制同步, 这样所写的内容就能立即保存到文件里了。

 

回到顶部

mmap和常规文件操作的区别

对linux文件系统不了解的朋友,请参阅我之前写的博文《从内核文件系统看文件读写过程》,我们首先简单的回顾一下常规文件系统操作(调用read/fread等类函数)中,函数的调用过程:

1、进程发起读文件请求。

2、内核通过查找进程文件符表,定位到内核已打开文件集上的文件信息,从而找到此文件的inode。

3、inode在address_space上查找要请求的文件页是否已经缓存在页缓存中。如果存在,则直接返回这片文件页的内容。

4、如果不存在,则通过inode定位到文件磁盘地址,将数据从磁盘复制到页缓存。之后再次发起读页面过程,进而将页缓存中的数据发给用户进程。

总结来说,常规文件操作为了提高读写效率和保护磁盘,使用了页缓存机制。这样造成读文件时需要先将文件页从磁盘拷贝到页缓存中,由于页缓存处在内核空间,不能被用户进程直接寻址,所以还需要将页缓存中数据页再次拷贝到内存对应的用户空间中。这样,通过了两次数据拷贝过程,才能完成进程对文件内容的获取任务。写操作也是一样,待写入的buffer在内核空间不能直接访问,必须要先拷贝至内核空间对应的主存,再写回磁盘中(延迟写回),也是需要两次数据拷贝。

而使用mmap操作文件中,创建新的虚拟内存区域和建立文件磁盘地址和虚拟内存区域映射这两步,没有任何文件拷贝操作。而之后访问数据时发现内存中并无数据而发起的缺页异常过程,可以通过已经建立好的映射关系,只使用一次数据拷贝,就从磁盘中将数据传入内存的用户空间中,供进程使用。

总而言之,常规文件操作需要从磁盘到页缓存再到用户主存的两次数据拷贝。而mmap操控文件,只需要从磁盘到用户主存的一次数据拷贝过程。说白了,mmap的关键点是实现了用户空间和内核空间的数据直接交互而省去了空间不同数据不通的繁琐过程。因此mmap效率更高。

 

回到顶部

mmap优点总结

由上文讨论可知,mmap优点共有一下几点:

1、对文件的读取操作跨过了页缓存,减少了数据的拷贝次数,用内存读写取代I/O读写,提高了文件读取效率。

2、实现了用户空间和内核空间的高效交互方式。两空间的各自修改操作可以直接反映在映射的区域内,从而被对方空间及时捕捉。

3、提供进程间共享内存及相互通信的方式。不管是父子进程还是无亲缘关系的进程,都可以将自身用户空间映射到同一个文件或匿名映射到同一片区域。从而通过各自对映射区域的改动,达到进程间通信和进程间共享的目的。

     同时,如果进程A和进程B都映射了区域C,当A第一次读取C时通过缺页从磁盘复制文件页到内存中;但当B再读C的相同页面时,虽然也会产生缺页异常,但是不再需要从磁盘中复制文件过来,而可直接使用已经保存在内存中的文件数据。

4、可用于实现高效的大规模数据传输。内存空间不足,是制约大数据操作的一个方面,解决方案往往是借助硬盘空间协助操作,补充内存的不足。但是进一步会造成大量的文件I/O操作,极大影响效率。这个问题可以通过mmap映射很好的解决。换句话说,但凡是需要用磁盘空间代替内存的时候,mmap都可以发挥其功效。

 

回到顶部

mmap相关函数

函数原型

void *mmap(void *start, size_t length, int prot, int flags,int fd, off_t offset);

返回说明

成功执行时,mmap()返回被映射区的指针。失败时,mmap()返回MAP_FAILED[其值为(void *)-1],error被设为以下的某个值:

返回错误类型

参数

start:映射区的开始地址

length:映射区的长度

prot:期望的内存保护标志,不能与文件的打开模式冲突。是以下的某个值,可以通过or运算合理地组合在一起

1 PROT_EXEC :页内容可以被执行2 PROT_READ :页内容可以被读取3 PROT_WRITE :页可以被写入4 PROT_NONE :页不可访问

flags:指定映射对象的类型,映射选项和映射页是否可以共享。它的值可以是一个或者多个以下位的组合体

复制代码
 1 MAP_FIXED //使用指定的映射起始地址,如果由start和len参数指定的内存区重叠于现存的映射空间,重叠部分将会被丢弃。如果指定的起始地址不可用,操作将会失败。并且起始地址必须落在页的边界上。 2 MAP_SHARED //与其它所有映射这个对象的进程共享映射空间。对共享区的写入,相当于输出到文件。直到msync()或者munmap()被调用,文件实际上不会被更新。 3 MAP_PRIVATE //建立一个写入时拷贝的私有映射。内存区域的写入不会影响到原文件。这个标志和以上标志是互斥的,只能使用其中一个。 4 MAP_DENYWRITE //这个标志被忽略。 5 MAP_EXECUTABLE //同上 6 MAP_NORESERVE //不要为这个映射保留交换空间。当交换空间被保留,对映射区修改的可能会得到保证。当交换空间不被保留,同时内存不足,对映射区的修改会引起段违例信号。 7 MAP_LOCKED //锁定映射区的页面,从而防止页面被交换出内存。 8 MAP_GROWSDOWN //用于堆栈,告诉内核VM系统,映射区可以向下扩展。 9 MAP_ANONYMOUS //匿名映射,映射区不与任何文件关联。10 MAP_ANON //MAP_ANONYMOUS的别称,不再被使用。11 MAP_FILE //兼容标志,被忽略。12 MAP_32BIT //将映射区放在进程地址空间的低2GB,MAP_FIXED指定时会被忽略。当前这个标志只在x86-64平台上得到支持。13 MAP_POPULATE //为文件映射通过预读的方式准备好页表。随后对映射区的访问不会被页违例阻塞。14 MAP_NONBLOCK //仅和MAP_POPULATE一起使用时才有意义。不执行预读,只为已存在于内存中的页面建立页表入口。
复制代码

fd:有效的文件描述词。如果MAP_ANONYMOUS被设定,为了兼容问题,其值应为-1

offset:被映射对象内容的起点

相关函数

int munmap( void * addr, size_t len ) 

成功执行时,munmap()返回0。失败时,munmap返回-1,error返回标志和mmap一致;

该调用在进程地址空间中解除一个映射关系,addr是调用mmap()时返回的地址,len是映射区的大小;

当映射关系解除后,对原来映射地址的访问将导致段错误发生。 

 

int msync( void *addr, size_t len, int flags )

一般说来,进程在映射空间的对共享内容的改变并不直接写回到磁盘文件中,往往在调用munmap()后才执行该操作。

可以通过调用msync()实现磁盘上文件内容与共享内存区的内容一致。

 

回到顶部

mmap使用细节

1、使用mmap需要注意的一个关键点是,mmap映射区域大小必须是物理页大小(page_size)的整倍数(32位系统中通常是4k字节)。原因是,内存的最小粒度是页,而进程虚拟地址空间和内存的映射也是以页为单位。为了匹配内存的操作,mmap从磁盘到虚拟地址空间的映射也必须是页。

2、内核可以跟踪被内存映射的底层对象(文件)的大小,进程可以合法的访问在当前文件大小以内又在内存映射区以内的那些字节。也就是说,如果文件的大小一直在扩张,只要在映射区域范围内的数据,进程都可以合法得到,这和映射建立时文件的大小无关。具体情形参见“情形三”。

3、映射建立之后,即使文件关闭,映射依然存在。因为映射的是磁盘的地址,不是文件本身,和文件句柄无关。同时可用于进程间通信的有效地址空间不完全受限于被映射文件的大小,因为是按页映射。

 

在上面的知识前提下,我们下面看看如果大小不是页的整倍数的具体情况:

情形一:一个文件的大小是5000字节,mmap函数从一个文件的起始位置开始,映射5000字节到虚拟内存中。

分析:因为单位物理页面的大小是4096字节,虽然被映射的文件只有5000字节,但是对应到进程虚拟地址区域的大小需要满足整页大小,因此mmap函数执行后,实际映射到虚拟内存区域8192个 字节,5000~8191的字节部分用零填充。映射后的对应关系如下图所示:

               

此时:

(1)读/写前5000个字节(0~4999),会返回操作文件内容。

(2)读字节5000~8191时,结果全为0。写5000~8191时,进程不会报错,但是所写的内容不会写入原文件中 。

(3)读/写8192以外的磁盘部分,会返回一个SIGSECV错误。

 

情形二:一个文件的大小是5000字节,mmap函数从一个文件的起始位置开始,映射15000字节到虚拟内存中,即映射大小超过了原始文件的大小。

分析:由于文件的大小是5000字节,和情形一一样,其对应的两个物理页。那么这两个物理页都是合法可以读写的,只是超出5000的部分不会体现在原文件中。由于程序要求映射15000字节,而文件只占两个物理页,因此8192字节~15000字节都不能读写,操作时会返回异常。如下图所示:

                 

此时:

(1)进程可以正常读/写被映射的前5000字节(0~4999),写操作的改动会在一定时间后反映在原文件中。

(2)对于5000~8191字节,进程可以进行读写过程,不会报错。但是内容在写入前均为0,另外,写入后不会反映在文件中。

(3)对于8192~14999字节,进程不能对其进行读写,会报SIGBUS错误。

(4)对于15000以外的字节,进程不能对其读写,会引发SIGSEGV错误。

 

情形三:一个文件初始大小为0,使用mmap操作映射了1000*4K的大小,即1000个物理页大约4M字节空间,mmap返回指针ptr。

分析:如果在映射建立之初,就对文件进行读写操作,由于文件大小为0,并没有合法的物理页对应,如同情形二一样,会返回SIGBUS错误。

但是如果,每次操作ptr读写前,先增加文件的大小,那么ptr在文件大小内部的操作就是合法的。例如,文件扩充4096字节,ptr就能操作ptr ~ [ (char)ptr + 4095]的空间。只要文件扩充的范围在1000个物理页(映射范围)内,ptr都可以对应操作相同的大小。

这样,方便随时扩充文件空间,随时写入文件,不造成空间浪费。


MMAP(2)                       Linux Programmer's Manual                       MMAP(2)

NAME         top

       mmap, munmap - map or unmap files or devices into memory

SYNOPSIS         top

       #include <sys/mman.h>       void *mmap(void *addr, size_t length, int prot, int flags,                  int fd, off_t offset);       int munmap(void *addr, size_t length);

DESCRIPTION         top

       mmap() creates a new mapping in the virtual address space of the calling       process.  The starting address for the new mapping is specified in addr.  The       length argument specifies the length of the mapping.       If addr is NULL, then the kernel chooses the address at which to create the       mapping; this is the most portable method of creating a new mapping.  If addr       is not NULL, then the kernel takes it as a hint about where to place the       mapping; on Linux, the mapping will be created at a nearby page boundary.  The       address of the new mapping is returned as the result of the call.       The contents of a file mapping (as opposed to an anonymous mapping; see       MAP_ANONYMOUS below), are initialized using length bytes starting at offset       offset in the file (or other object) referred to by the file descriptor fd.       offset must be a multiple of the page size as returned by       sysconf(_SC_PAGE_SIZE).       The prot argument describes the desired memory protection of the mapping (and       must not conflict with the open mode of the file).  It is either PROT_NONE or       the bitwise OR of one or more of the following 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.       The flags argument determines whether updates to the mapping are visible to       other processes mapping the same region, and whether updates are carried       through to the underlying file.  This behavior is determined by including       exactly one of the following values in flags:       MAP_SHARED Share this mapping.  Updates to the mapping are visible to other                  processes that map this file, and are carried through to the                  underlying file.  The file may not actually be updated until                  msync(2) or munmap() is called.       MAP_PRIVATE                  Create a private copy-on-write mapping.  Updates to the mapping are                  not visible to other processes mapping the same file, and are not                  carried through to the underlying file.  It is unspecified whether                  changes made to the file after the mmap() call are visible in the                  mapped region.       Both of these flags are described in POSIX.1-2001.       In addition, zero or more of the following values can be ORed in flags:       MAP_32BIT (since Linux 2.4.20, 2.6)              Put the mapping into the first 2 Gigabytes of the process address              space.  This flag is only supported on x86-64, for 64-bit programs.  It              was added to allow thread stacks to be allocated somewhere in the first              2GB of memory, so as to improve context-switch performance on some              early 64-bit processors.  Modern x86-64 processors no longer have this              performance problem, so use of this flag is not required on those              systems.  The MAP_32BIT flag is ignored when MAP_FIXED is set.       MAP_ANON              Synonym for MAP_ANONYMOUS.  Deprecated.       MAP_ANONYMOUS              The mapping is not backed by any file; its contents are initialized to              zero.  The fd and offset arguments are ignored; however, some              implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is              specified, and portable applications should ensure this.  The use of              MAP_ANONYMOUS in conjunction with MAP_SHARED is only supported on Linux              since kernel 2.4.       MAP_DENYWRITE              This flag is ignored.  (Long ago, it signaled that attempts to write to              the underlying file should fail with ETXTBUSY.  But this was a source              of denial-of-service attacks.)       MAP_EXECUTABLE              This flag is ignored.       MAP_FILE              Compatibility flag.  Ignored.       MAP_FIXED              Don't interpret addr as a hint: place the mapping at exactly that              address.  addr must be a multiple of the page size.  If the memory              region specified by addr and len overlaps 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, mmap() will fail.              Because requiring a fixed address for a mapping is less portable, the              use of this option is discouraged.       MAP_GROWSDOWN              Used for stacks.  Indicates to the kernel virtual memory system that              the mapping should extend downward in memory.       MAP_HUGETLB (since Linux 2.6.32)              Allocate the mapping using "huge pages."  See the kernel source file              Documentation/vm/hugetlbpage.txt for further information.       MAP_LOCKED (since Linux 2.5.37)              Lock the pages of the mapped region into memory in the manner of              mlock(2).  This flag is ignored in older kernels.       MAP_NONBLOCK (since Linux 2.5.46)              Only meaningful in conjunction with MAP_POPULATE.  Don't perform read-              ahead: only create page tables entries for pages that are already              present in RAM.  Since Linux 2.6.23, this flag causes MAP_POPULATE to              do nothing.  One day the combination of MAP_POPULATE and MAP_NONBLOCK              may be reimplemented.       MAP_NORESERVE              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 /proc/sys/vm/overcommit_memory in proc(5).  In kernels before              2.6, this flag only had effect for private writable mappings.       MAP_POPULATE (since Linux 2.5.46)              Populate (prefault) page tables for a mapping.  For a file mapping,              this causes read-ahead on the file.  Later accesses to the mapping will              not be blocked by page faults.  MAP_POPULATE is only supported for              private mappings since Linux 2.6.23.       MAP_STACK (since Linux 2.6.27)              Allocate the mapping at an address suitable for a process or thread              stack.  This flag is currently a no-op, but is used in the glibc              threading implementation so that if some architectures require special              treatment for stack allocations, support can later be transparently              implemented for glibc.       MAP_UNINITIALIZED (since Linux 2.6.33)              Don't clear anonymous pages.  This flag is intended to improve              performance on embedded devices.  This flag is only honored if the              kernel was configured with the CONFIG_MMAP_ALLOW_UNINITIALIZED option.              Because of the security implications, that option is normally enabled              only on embedded devices (i.e., devices where one has complete control              of the contents of user memory).       Of the above flags, only MAP_FIXED is specified in POSIX.1-2001.  However,       most systems also support MAP_ANONYMOUS (or its synonym MAP_ANON).       Some systems document the additional flags MAP_AUTOGROW, MAP_AUTORESRV,       MAP_COPY, and MAP_LOCAL.       Memory mapped by mmap() is preserved across fork(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.

munmap()

       The munmap() 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 addr must 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.

Timestamps changes for file-backed mappings

       For file-backed mappings, the st_atime field for the mapped file may be       updated at any time between the mmap() and the corresponding unmapping; the       first reference to a mapped page will update the field if it has not been       already.       The st_ctime and st_mtime field for a file mapped with PROT_WRITE and       MAP_SHARED will be updated after a write to the mapped region, and before a       subsequent msync(2) with the MS_SYNC or MS_ASYNC flag, if one occurs.

RETURN VALUE         top

       On success, mmap() returns a pointer to the mapped area.  On error, the value       MAP_FAILED (that is, (void *) -1) is returned, and errno is set appropriately.       On success, munmap() returns 0, on failure -1, and errno is set (probably to       EINVAL).

ERRORS         top

       EACCES A file descriptor refers to a non-regular file.  Or MAP_PRIVATE was              requested, but fd is not open for reading.  Or MAP_SHARED was requested              and PROT_WRITE is set, but fd is not open in read/write (O_RDWR) mode.              Or PROT_WRITE is set, but the file is append-only.       EAGAIN The file has been locked, or too much memory has been locked (see              setrlimit(2)).       EBADF  fd is not a valid file descriptor (and MAP_ANONYMOUS was not set).       EINVAL We don't like addr, length, or offset (e.g., they are too large, or not              aligned on a page boundary).       EINVAL (since Linux 2.6.12) length was 0.       EINVAL flags contained neither MAP_PRIVATE or MAP_SHARED, or contained both of              these values.       ENFILE The system limit on the total number of open files has been reached.       ENODEV The underlying file system 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 prot argument asks for PROT_EXEC but the mapped area belongs to a              file on a file system that was mounted no-exec.       ETXTBSY              MAP_DENYWRITE was set but the object specified by fd is open for              writing.       Use of a mapped region can result in these signals:       SIGSEGV              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).

CONFORMING TO         top

       SVr4, 4.4BSD, POSIX.1-2001.

AVAILABILITY         top

       On POSIX systems on which mmap(), msync(2) and munmap() are available,       _POSIX_MAPPED_FILES is defined in <unistd.h> to a value greater than 0.  (See       also sysconf(3).)

NOTES         top

       Since kernel 2.4, this system call has been superseded by mmap2(2).  Nowadays,       the glibc mmap() wrapper function invokes mmap2(2) with a suitably adjusted       value for offset.       On some hardware architectures (e.g., i386), PROT_WRITE implies PROT_READ.  It       is architecture dependent whether PROT_READ implies PROT_EXEC or not.       Portable programs should always set PROT_EXEC if they intend to execute code       in the new mapping.       The portable way to create a mapping is to specify addr as 0 (NULL), and omit       MAP_FIXED from flags.  In this case, the system chooses the address for the       mapping; the address is chosen so as not to conflict with any existing       mapping, and will not be 0.  If the MAP_FIXED flag is specified, and addr is 0       (NULL), then the mapped address will be 0 (NULL).

BUGS         top

       On Linux there are no guarantees like those suggested above under       MAP_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 MAP_POPULATE flag only has effect if prot is       specified as PROT_NONE.       SUSv3 specifies that mmap() should fail if length is 0.  However, in kernels       before 2.6.12, mmap() succeeded in this case: no mapping was created and the       call returned addr.  Since kernel 2.6.12, mmap() fails with the error EINVAL       for this case.

EXAMPLE         top

       The following program prints part of the file specified in its first command-       line argument to standard output.  The range of bytes to be printed is       specified via offset and length values in the second and third command-line       arguments.  The program creates a memory mapping of the required pages of the       file and then uses write(2) to output the desired bytes.       #include <sys/mman.h>       #include <sys/stat.h>       #include <fcntl.h>       #include <stdio.h>       #include <stdlib.h>       #include <unistd.h>       #define handle_error(msg) \           do { perror(msg); exit(EXIT_FAILURE); } while (0)       int       main(int argc, char *argv[])       {           char *addr;           int fd;           struct stat sb;           off_t offset, pa_offset;           size_t length;           ssize_t s;           if (argc < 3 || argc > 4) {               fprintf(stderr, "%s file offset [length]\n", argv[0]);               exit(EXIT_FAILURE);           }           fd = open(argv[1], O_RDONLY);           if (fd == -1)               handle_error("open");           if (fstat(fd, &sb) == -1)           /* To obtain file size */               handle_error("fstat");           offset = atoi(argv[2]);           pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);               /* offset for mmap() must be page aligned */           if (offset >= sb.st_size) {               fprintf(stderr, "offset is past end of file\n");               exit(EXIT_FAILURE);           }           if (argc == 4) {               length = atoi(argv[3]);               if (offset + length > sb.st_size)                   length = sb.st_size - offset;                       /* Can't display bytes past end of file */           } else {    /* No length arg ==> display to end of file */               length = sb.st_size - offset;           }           addr = mmap(NULL, length + offset - pa_offset, PROT_READ,                       MAP_PRIVATE, fd, pa_offset);           if (addr == MAP_FAILED)               handle_error("mmap");           s = write(STDOUT_FILENO, addr + offset - pa_offset, length);           if (s != length) {               if (s == -1)                   handle_error("write");               fprintf(stderr, "partial write");               exit(EXIT_FAILURE);           }           exit(EXIT_SUCCESS);       }

SEE ALSO         top

       getpagesize(2), mincore(2), mlock(2), mmap2(2), mprotect(2), mremap(2),       msync(2), remap_file_pages(2), setrlimit(2), shmat(2), shm_open(3),       shm_overview(7)       B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128-129 and 389-391.

COLOPHON         top

       This page is part of release 3.32 of the Linux man-pages project.  A       description of the project, and information about reporting bugs, can be found       at http://www.kernel.org/doc/man-pages/.Linux                                 2010-06-20                              MMAP(2)