Linux under C Programming: memory Image those things

Source: Internet
Author: User
Tags include printf linux

The memory image is actually creating an image in memory that is exactly the same as the external storage file. The user can map the entire file to memory or partially to memory. The system will reflect the memory image changes truthfully reflected in the external storage file. This enables the operation of the file to be stored externally through a memory image.

Features of memory images:

1, can speed up the operation of IO.

2, the user can through the pointer to the file operation, indirect ~ ~ ~ ~

3, the realization of the file data sharing, the external memory file map to the shared memories, very convenient to achieve the data sharing, and can be completed to save the data to the external storage work.

Note: Memory images can only operate on files that can be positioned internally, such as normal files. You cannot operate on a pipe, socket file.

To create a memory map:

#include <sys/types.h>     
         
#include <sys/mman.h>     
         
void *mmap (void *start,size_t length,intport,int Flag,int fd,off_t Offset)

Start is usually set to null for pointers, indicating that the mapped memory has a system decision. Because the specified memory often goes wrong.

Length is the amount of memory space occupied by the memory image. in bytes.

Port represents the security of a memory image.

Prot_exec indicates that the image memory may have machine code that can be executed.

Port_none indicates that the memory being imaged cannot be accessed.

Port_read indicates that the image memory is readable

Port_write indicates that the memory of the image is writable

Flag Memory Image flag:

Map_fixed indicates that if the memory image cannot be created from the start address, an error is returned.

Map_private indicates that changes made to the memory image are not reflected in the external storage file.

Map_shared indicates that changes to the memory image are reflected in the external storage file.

FD File Descriptor

Offset represents the distance from the file header of the content that is being imaged.

Undo Memory Mapping:

#include <sys/types.h>     
         
#include <sys/mman.h>     
         
int munmap (void *start,size_t length);

To change the memory properties:

To modify protection values:

int protect (const void *addr,size_tlength,int prot);

Addr indicates that the address is the same as above.

Length memory image size Ibid.

Prot the Reset protection value.

Successfully returned 0 failed return-1

To modify the memory image size:

void *mremap (void *old_addr,size_told_length,size_t new_length,unsigned long *flag)

FLG is used to set whether to move the memory mirror when it is needed. such as: Mrmap_maymove

The call successfully returns the new starting address, and the failure returns-1

The procedure is as follows:

#include <stdio.h>     
#include <sys/types.h>     
#include <sys/stat.h>     
#include <fcntl.h >     
#include <unistd.h>     
#include <sys/mman.h>     
         
int main ()     
{     
    int fd;     
    char *start;     
    Char buf[100];     
             
    /* Open File * 
    /fd = open ("Testfile", O_RDWR);     
                 
    Start=mmap (Null,lseek (fd,0,seek_end), prot_read| prot_write,map_shared,fd,0);     
             
    /* read out data * * * 
    strcpy (buf,start);     
    printf ("mmap =%s\n", buf);         
         
    /* Write Data 
    /* strcpy (Start, "Buf is not null!");     
    printf ("Mmap:%s\", start);     
             
    Munmap (start,100); /* Unlock mapping * * Close 
    (FD);       
             
    return 0;         
}

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.