当我们使用 mmap 函数时,操作系统会做什么工作? [英] What jobs OS does when we use mmap function?

查看:58
本文介绍了当我们使用 mmap 函数时,操作系统会做什么工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个使用 mmap 函数将给定文件映射到内存的示例.在这个例子中,我没有使用 fwritewrite 函数将内容写入磁盘文件(只是将内容打印到 stdout),但实际上反映了修改后的内存内容在磁盘文件上.我猜操作系统会跟踪映射内存并在修改映射内存时写入磁盘.我想知道操作系统的详细信息.

Here is a example that maps a given file to memory using mmap function. In this example, I didn't use fwrite or write function to write something into a disk file(just print contents to stdout), but the modified memory content is actually reflected on the disk file. I'm guessing that OS tracks mapped memory and writes to disk when mapped memory is modified. I'd like to know the detail of what OS does.

example.c

#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[]){

    if(argc < 2){
        printf("File path not mentioned\n");
        exit(0);
    }

    const char *filepath = argv[1];
    int fd = open(filepath, O_RDWR);
    if(fd < 0){
        printf("\n\"%s \" could not open\n",
               filepath);
        exit(1);
    }

    struct stat statbuf;
    int err = fstat(fd, &statbuf);
    if(err < 0){
        printf("\n\"%s \" could not open\n",
                       filepath);
        exit(2);
    }

    char *ptr = mmap(NULL,statbuf.st_size,
            PROT_READ|PROT_WRITE,MAP_SHARED,
            fd,0);
    if(ptr == MAP_FAILED){
        printf("Mapping Failed\n");
        return 1;
    }
    close(fd);

    ptr[statbuf.st_size - 2] = '@';
    ssize_t n = write(1,ptr,statbuf.st_size);
    if(n != statbuf.st_size){
        printf("Write failed\n");
    }
    while(1);
}

mytext.txt

hello world!

编译运行

gcc example.c && ./a.out mytext.txt

由于最后一行没有退出程序,我可以通过cat mytext.txt

program is not exited due to the last line and I can see the modified contents in another terminal via cat mytext.txt

hello world@

推荐答案

我猜操作系统会跟踪映射内存并在映射内存被修改时写入磁盘.

I'm guessing that OS tracks mapped memory and writes to disk when mapped memory is modified.

是和否.当然,操作系统会保存映射到磁盘文件的内存记录,并确保对该内存的更改写入磁盘.但是,我不希望立即写入更改.

Yes and no. Certainly the operating system keeps records of memory mapped to files on disk and ensures that changes to that memory are written to disk. However, I do not expect the changes are written immediately.

基于常识(我已经很久没有看到操作系统的这些特定内部结构了),可能会发生的情况是管理文件系统的软件会记录磁盘上的某些块映射到内存并且,一旦它们被更改,它们就是脏的",这意味着它们已经被修改,最终需要写回磁盘.

Based on general knowledge (it has been a long time since I looked at these particular internals of an operating system), what may happen is that the software managing the file system keeps a record that certain blocks on disk are mapped to memory and, once they are changed, that they are "dirty," meaning they have been modified and will need to be written back to disk eventually.

操作系统可能会在某些情况下将脏页写入磁盘.例如,当磁盘上的文件已经关闭(这也意味着内存页已经被取消映射,因为将内存映射到文件包括保持文件打开)并且有卸载磁盘的请求时,脏页必须是写入磁盘.作为策略选择,它们也可能会定期写入磁盘.

The operating system may write dirty pages to disk on certain occasions. For example, when the files on the disk have been closed (which also means the memory pages have been unmapped, because mapping memory to a file includes holding the file open) and there is a request to unmount the disk, the dirty pages must be written to disk. They might also be written to disk periodically, as a policy choice.

但是,即使更改没有写入磁盘,这些更改也可能看起来在您的文件中!当您读取文件时,文件系统会确定磁盘上的哪些块包含您正在请求的文件部分.然后它看到那些块已经在内存中.因此,为了满足您的读取请求,它为您提供内存中的数据,而不是从磁盘读取数据.因此,对于作为用户的您来说,即使数据尚未在磁盘上,但更改的数据实际上似乎在文件中.

However, the changes might appear to be in your file even though they are not written to disk! As you read the file, the file system figures out which blocks on disk contain the parts of the file you are requesting. Then it sees those blocks are already in memory. So, to satisfy your read request, it gives you the data from memory instead of reading it from disk. Thus, to you as a user, it will appear that the changed data is actually in the file even though the data is not yet on disk.

这种行为实际上不符合管理文件系统的一般设计.如果您打开一个文件并读取了 100 个字节,文件系统软件最初必须将正确的块归档到磁盘上,将其读入内存,然后将您请求的 100 个字节复制到您的进程中.该块可能是 512 字节或 4096 字节或其他一些用于操作磁盘的大小.文件系统将如何处理该块?在请求一个文件的 100 个字节后,程序很可能请求另一个 100 个字节.再次从磁盘读取它是一种耻辱.所以文件系统软件会保留它.这意味着它会保存一个数据库,其中包含当前内存中的哪些磁盘的哪些块.

This behavior actually falls out of a general design for managing the file system. If you have a file open and read 100 bytes, the file system software initially has to file the right block on disk, read it into memory, and copy the 100 bytes you requested to your process. That block might be 512 bytes or 4096 bytes or some other size that is used for operating with disks. What is the file system going to do with that block? After requesting 100 bytes of a file, a program is very likely to request another 100 bytes. It would be a shame to read it from disk again. So the file system software keeps it around. Which means it keeps a database of which blocks from which disks it currently has in memory.

接下来,假设其他程序读取同一个文件.显然,如果必要的块已经在内存中,我们将从内存中的块中提供该程序数据,而不是再次从磁盘读取它.因此文件系统共享所有这些块(取决于适当的权限).如果一个程序更改了文件中的数据,它会转到内存中的块,因此即使尚未写入磁盘,其他程序也会立即看到它.

Next, suppose some other program reads the same file. Clearly, if the necessary block is already in memory, we are going to give that program data from the block in memory, not read it from disk again. So the file system shares all these blocks (subject to appropriate permissions). And if one program changes the data in the file, it goes to the block in memory, so other programs see it right away even if it is not yet written to disk.

内存映射文件与这种在内存中缓存磁盘块的方案密切相关.进程的虚拟内存只是映射到文件系统软件保存文件块的物理内存,并且每个进程都具有相同的文件视图,无论它是使用内存映射还是读写操作.

Memory mapping a file ties right into this scheme of caching disk blocks in memory. The virtual memory of a process just gets mapped to the physical memory where the file system software is keeping the blocks of a file, and every process has the same view of the file regardless of whether it is using memory mapping or read and write operations.

(这是一般信息;特定的操作系统可能会有不同的表现.)

(This is general information; particular operating systems may behave differently.)

这篇关于当我们使用 mmap 函数时,操作系统会做什么工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆