One of MMAP memory ing operations

Source: Internet
Author: User

In the driver code, we often need to map the hardware address to the virtual memory, which is why we need to learn MMAP! The so-called cutting-edge without mistake, let's calm down and learn this very useful tool.

 

Prototype: void * MMAP (void * Start, size_t length, int Prot, int flags,
Int FD, off_t offset );

Parameter: Start address of the ing area. (It is generally recommended to be null so that the kernel can automatically find a suitable address)

 

Length: the length of the ing area.


Prot: The expected memory protection flag. It cannot conflict with the file opening mode. Is a value of the following. You can run or

Reasonably combined.
Prot_exec // page content can be executed
Prot_read // The page content can be read
Prot_write // page can be written
Prot_none // page inaccessible

 

Flags: specifies the type of the ing object, and whether the ing options and ing pages can be shared. The value can be one or more

.

Map_shared // share the ing space with all other processes mapped to this object. Write to the shared area

Input, which is equivalent to output to a file. Until msync () or munmap () is called

The file will not be updated.
Map_private // create a private ing for copying when writing. Writing in the memory area does not affect the original

File. This flag is mutually exclusive with the above one and can only be used.

(More optional parameters are available on the Internet)

FD: a valid file description.

 

Offset: the starting point of the mapped object content.

 

Return: returns the first address of the mapped virtual memory.

 

The routine is as follows:

# Include <stdio. h> <br/> # include <sys/types. h> <br/> # include <sys/STAT. h> <br/> # include <fcntl. h> <br/> # include <unistd. h> <br/> # include <sys/Mman. h> </P> <p> int main () <br/>{< br/> int FD; <br/> char * start; <br/> char Buf [100]; </P> <p>/* open the file */<br/> FD = open ("testfile", o_rdwr ); </P> <p> Start = MMAP (null, 100, prot_read | prot_write, map_shared, FD, 0 ); </P> <p>/* read data */<br/> strcpy (BUF, start); <br/> printf ("Buf = % s/n ", Buf); </P> <p>/* write data */<br/> strcpy (start, "Buf is not null! "); </P> <p> munmap (START, 100);/* unmap */<br/> close (FD ); </P> <p> return 0; <br/>}< br/> 

1. The test routine first opens the test file testfile and obtains the file descriptor FD. Call the MMAP function to map the test file to the virtual memory, and return the first address to the start pointer.

2. When reading data, use strcpy to copy the string from the first address of the virtual memory to the Buf. The essence is to copy the testfile content of the test file to the Buf (ing has been done beforehand ).

3. When writing data, you can also use strcpy. However, the write action does not change the length of the test file. That is to say, the original content of the test file is 10 bytes in length. If we write more than 10 bytes, the file will only save the first 10. If less than 10 bytes of content are written, garbled characters will appear in the spare bytes without actual content.

4. The function of the munmap function is used to remove the ing. It is usually used with MMAP. This function is relatively simple and will not be described in detail here.

 

Through the above analysis, we use the memory ing method to replace the traditional system calls such as read and write, which may not be fully convenient to display compared with files, if it is used to map hardware physical addresses to virtual memory addresses, the results will be well reflected.

 

 

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.