Memory image Io-linux C Programming Guide

Source: Internet
Author: User

A memory image is a way to improve file read speed by mapping files to memory.

You can use the pointer to quickly access the data in the file

1.

Functions that are used

To map externally stored files to memory, you need to use the

#include <sys/types.h>#include<sys/mman.h>void *mmap (void *start.size_t length,int prot,int flag,int fd,off_t offset);

At this point, if start is not NULL, the file will be mapped to this location, but it is not guaranteed to succeed;

Length indicates the size of the memory image occupied, byte-written;

The prot represents the security properties of the memory image, and the options are:

Prot_exec              // mapped memory may be executable   prot_none              //  mapped memory inaccessible prot_read                // memory that is mapped is readable Prot_write            // mapped memory writable

Flag indicates the memory image's flags

Map_fixed                // If an image cannot be created at start, an error is returned map_reivate            // changes to memory are not reflected in the external memory file map_shared            // Save changes in memory to the external memory file

off_t represents the offset of the content of the image from the file header

Call failed return-1, otherwise the image memory start address is returned

Note You need to open a file in a process to map it * * *

2.

Modify the protection value of the memory image prot

int Protect (constvoid *addr,size_t length,int prot);

Success returns 0, error returns-1, and errno is placed.

3.

Write memory image to external memory

int msync (constvoid *start,size_t length,int  flag); flag parameter options are: Ms_async             // dispatches a write operation and returns Ms_invalidate       // image to the same file is not valid for update to new data Ms_sync             // write function returned after completion

4.

Modify Image Memory Size

void *mremap (voidlong flag)

The parameter flag indicates whether the location of the image is moved when needed

The call returns the new address of the image successfully, otherwise the error value-1

Memory image Io-linux C Programming Guide

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.