& Lt; Functions & gt; APUE: mmap function, apuemmap Function

Source: Internet
Author: User

<Strong> APUE: mmap function, apuemmap Function
Initially

After reading the memory ing I/O, it means you can understand it, that is, you can directly operate the file and then directly operate through the buffer to reduce the time it takes to call the read and write operations. In addition, the following is an example of copy.
However, after I map two files, I can use the memcpy memory replication function to write the files, but this is the operation of the two files, I want to modify a file myself? What should I do when there is only one file? So I started to build a program to verify it. The problem 14.11 after the book is also solved :)

Conjecture

First, create an empty file, first write a bit of data, then map it to the buffer zone, and then write a bit of data. To put it simply, open => write => mmap => write. However, if the buffer is not involved here, it is still created in the file operation!

Continue to think about a new idea. There is a sentence in the book.The return address of this function is the return address of this ing area.That is to say, the returned value of mmap can be used. We can output its content. I guess this content should be the content of the file. In this case, I can perform corresponding operations on the returned value. Isn't this the operation in the ing cache?

Since the book mentions that the msync function synchronizes the modification of the ing area to the file, the modification to the file can be reflected. The idea of one breath in one breath, and finally a release ing area, is a basic cleanup operation. It should be easy to design.

First, we need to create a file, write a little data, and then map it to the past. Read the content (return value) of the ing area, modify the ing area, write files synchronously, and clean up the work. This idea should be similar.

Implementation

One problem before encoding is that the returned value of mmap is void *, and we want to show that it is indeed a string, so we have to forcibly convert the returned value. After this step is completed, start Encoding

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<fcntl.h>#include<unistd.h>#include<sys/mman.h>#include<errno.h>static void error(const char *msg){    fprintf(stderr,"%s:%s\n",msg,strerror(errno));    exit(1);}int main(void){    int     fd;    char     *dst;    if((fd = open("mapfile",O_RDWR|O_CREAT|O_TRUNC,0666)) < 0)        error("open error");    if(write(fd,"abc",3) != 3)        error("wirte error");    /*let this file map to memory area*/    if((dst =(char *)mmap(0,64,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0)) == MAP_FAILED)    error("mmap error");    close(fd);    printf("%s\n",dst);    dst[2]='d';    printf("%s\n",dst);    if(msync((void *)dst,64,MS_SYNC) < 0)        error("msync error");    if(munmap((void *)dst,64) < 0)        error("munmap error");    return 0;    }

 

This is probably the first encoding, and the running result is quite good.

After studying this code, I tried to modify the content that has already been written. It does mean a change. It seems that the conjecture on the returned value is correct, the modification to the return value is the modification to the ing area. At the same time, I also tested that after the file descriptor is disabled, the cache area will not be affected, so it can be operated and synchronized to the file. This is indeed critical.

But I don't think this is enough. My goal is to continue writing data into it. Just as I thought at the beginning, after ing, I still need to write other data, check whether this is the same as the write function.

Modify code

At the beginning, we only wrote three data records, and the cache is 64, which is sufficient for writing. But the running result

Like above, there is no change... According to the principle, there should be an x.

When reading a book, I found that there is an offset setting in the copy function example in the book.The revelation offset of the ing file is limited by the system's virtual storage page. If the length of the ing area is not an integer multiple of the page, any changes will not be reflected in the file, you cannot use mmap to add data to a file. You need to lengthen the file.That is to say, I need an offset here. The example of copy on the reading is that the length of inputfile is offset from the file. That is to say, there is a hole in the file from the beginning to the offset. After the file is mapped, the displayed amount is sufficient, in this way, the changed data can be written to the file. Return to the one I just mentioned. Although the ing area is 64, the file length is not enough. This is different from write. write can be written directly later. Therefore, the file size must be sufficient.

Continue to modify code

Now the running result is in line with my conjecture. below is the running result.

As you can see, cat outputs an 'X' that I just inserted, and then looks at the actual situation of the file. It fully conforms to my conjecture and puts an x in the 11th-bit format, as to why there are three outputs, because there is a '\ 0' at the end of the string, then 'X' is not connected together.

In fact, there is another detail. When a file is empty, it is not only the offset location, but also a character to mark the actual length of the file, otherwise, the length of the file is the same as the original size without the offset, and the file is not expanded.

There is a good way to use ftruncate to directly set the file size, saving the complexity of using lseek and write.

Summary

Mmap mentioned it in IPC again, but this is involved in APUE. This reminds me of the use of this function when I was driving the camera again, but I have no idea at all, I followed the teacher's last word, but now I can understand the process of using storage ing. As for what level-2 Cache and level-1 cache, I feel that it can be felt in the actual project.

(Full text)

Related Article

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.