"Linux Programming" store mapped I/O

Source: Internet
Author: User

Storing mapped I/O causes a disk file to be matched to a buffer in the storage space, and the read and write operations on the buffer are read and write to the file, so that read and write system calls can no longer be used.
The function that maps the file to the store is completed by Mmap and the function prototype is as follows:
#include <sys/mman.h>/* Successfully returned map area start address, error returned map_failed */void *mmap (void *addr, size_t len, int prot, int flag, int fil Edes, off_t off);


Parameter description:
    • Addr: Specifies the starting address of the mapped store, typically 0 for the system to select the start address.
    • Len: The number of bytes that need to be mapped.
    • Prot: The protection requirement for the mapped store cannot exceed the permissions of the open file.
      • Prot_read: Map Area readable
      • Prot_write: Mapped area writable
      • Prot_exec: Map Area Executable
      • Prot_none: Map Area not accessible
    • Flag: Affects the properties of the mapped store.
      • Map_fixed: The return value must be equal to addr, which is not conducive to migration and is discouraged.
      • Map_shared: Indicates that the store operation is equivalent to the write to the file.
      • Map_private: A store operation on the map area causes a private copy of the mapping file to be created.
    • Filedes: Specifies the file descriptor to be mapped, which needs to be opened before mapping.
    • OFF: The starting offset of the byte in the file to be mapped, typically 0.

The storage mappings are as follows:

Test code:
 #include < stdio.h> #include <fcntl.h> #include <sys/mman.h> #include <string.h> #define File_mode (s_irusr | S_IWUSR | S_irgrp |    S_iroth) int main (int argc, char *argv[]) {int Fdin, fdout;    void *src, *DST;     struct stat statbuf;        if (argc! = 3) {printf ("Usage:%s <fromfile> <tofile>\n", argv[0]);    return-1;    } Fdin = open (argv[1], o_rdonly); Fdout = open (argv[2], O_RDWR | O_creat |     O_trunc, File_mode);     Fstat (Fdin, &statbuf);    Lseek (Fdout, statbuf.st_size-1, Seek_set);   Write (Fdout, "", 1);    /* Lseek offset is greater than file length, the write operation will be extended file */src = mmap (0, Statbuf.st_size, Prot_read, map_shared, Fdin, 0); DST = mmap (0, statbuf.st_size, Prot_read |     Prot_write, map_shared, fdout, 0);  memcpy (DST, SRC, statbuf.st_size);    /* Data replication */Munmap (SRC, statbuf.st_size);     Munmap (DST, statbuf.st_size); return 0;} 


This function implements a copy of the file contents. The combined operation of Lseek + write increases the size of the target file to the same size as the source file. Because when the file offset set by Lseek is greater than the file's current length, the next write operation will cause the file to grow. If the target file is not expanded, the process receives a sigbus signal indicating that there is an address in the store that cannot be mapped to the file.
Mmap actually maps the kernel buffers that contain the contents of the file to the application address space, and then copies the data directly with the memcpy. The advantage is that it avoids data transfer between kernel space and user space like read, write system calls.
Reference: Advanced programming for the UNIX environment p390-p395.

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.