Mmap Storage Map IO

Source: Internet
Author: User

mmap,munmap--

#include <sys/mman.h>void*mmap(void*addr, size_tlength,intProtintFlagsintFD, off_t offset);intMunmap (void*addr, size_tlength);intMprotect (void*addr, size_t Len,intProt);intMsync (void*addr, size_tlength,intFlags); mmap, Munmap-Map orUnmap filesorDevices into memory.

Storage mapping IO maps a disk file to a buffer in storage space. Then when the data is taken from the buffer, it is equivalent to reading the corresponding byte in the file. Similarly, when the data is stored in a buffer, the corresponding bytes are automatically written to the file. This allows the IO to be executed without using read and write. In order to use this feature, you should first tell the kernel to map a given file to a storage area, which is implemented by the MMAP function.

The addr parameter is used to specify the starting address of the mapped store, which is typically set to 0, which means that the start address of the map area is selected by the system, and the return address of the function is the starting address of the map area.

FD Specifies the descriptor of the file to be mapped and opens the file before mapping it to an address space.

Length is the number of bytes mapped.

Offset is the starting offset to map bytes in the file.

The prot parameter describes the protection requirements for the mapped store, but cannot exceed the file open mode access, prot optional values are as follows:
Prot_exec Pages may executed.
Prot_read Pages may be READ.
Prot_write Pages may written.
Prot_none Pages may isn't accessed.

The flags parameter affects several properties of the mapped store, where both map_shared and map_private must choose one, and many other map_xxx are optional.

The Mprotect function can change the permissions of an existing mapped store, and the Msync function is similar to Fsync, which is used to store the map area and flush the page into the mapped file.

SIGSEGV and Sigbus are the two two signals associated with the mapping store. The former is typically used to indicate that a process is attempting to access a store that is not available to it, such as a read-only mapped store, which indicates that the access mapping area does not exist.

After the fork is called, the child process inherits the mapped store, but the new program after exec does not inherit.

The following is an example of copying files using storage map Io, which may have different CPU times for users and systems under different operating systems than read/write, but the clock time is significantly faster than read/write. Because: When using read and write, the data is copied from the kernel buffer read to the application buffer, then the data in the application buffer is copied to the kernel buffer, and when using Mmap and Munmap, The data in one kernel buffer mapped to the application address space is copied directly to another kernel buffer that is also mapped to the application address space.

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <sys/mman.h>intMainintARGC, Char*ARGV[]){intFdin;intFdout; void*src; void*DST; structStatStatbuf;if(3! = argc) {printf("Usage: %s <fromfile> <tofile>\n", argv[0]);return-1; }if((Fdin =Open(argv[1], o_rdonly) <0) {printf("Can ' t create %s for reading\n", argv[1]);return-1; }if((Fdout =Open(argv[2], O_RDWR | O_creat | O_trunc)) <0) {printf("Can ' t create %s for writing\n", argv[2]);return-1; }if(Fstat (Fdin, &statbuf) <0) {printf("Fstat error\n");return-1; }if(Lseek (Fdout, Statbuf.st_size-1, seek_set) = =-1) {printf("Lseek error\n");return-1; }if(Write(Fdout,"",1) !=1) {printf("Write error\n");return-1; }if(src = mmap (0, Statbuf.st_size, Prot_read, map_shared, Fdin,0)) = = map_failed) {printf("Mmap error for input\n");return-1; }if(DST = mmap (0, Statbuf.st_size, Prot_read | Prot_write, map_shared, Fdout,0)) = = map_failed) {printf("Mmap error for output\n");return-1; } memcpy (DST, SRC, statbuf.st_size);Exit(0);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Mmap Storage Map IO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.