如何从memfd_create获取内存地址? [英] How to get memory address from memfd_create?

查看:1257
本文介绍了如何从memfd_create获取内存地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要共享内存 在父子之间(使用fork + execl). 我使用memfd_create分配内存,因为它提供了一个 文件描述符,可以方便地在子文件中使用 进程(描述符在execl之前通过dup2绑定到stdin) 附加到已分配的内存.

In my application I need to share memory between parent and child (using fork+execl). I use memfd_create to allocate memory, because it provides a file descriptor, which may be conveniently used in child process (the discriptor is tied to stdin via dup2 before execl) to attach to the allocated memory.

我不使用writeread-我使用指针 直接读写内存.

I do not use write and read - I use pointers to read and write memory directly.

唯一需要解决的难题 是如何获取分配的内存地址 通过fd = memfd_create ....

The only piece of the puzzle which is left to solve is how to get the address of memory, allocated via fd = memfd_create ....

使用mmap是不可取的,因为它会复制内存,而不是提供 memfd_create已分配的内存地址.

Using mmap is undesirable, because it duplicates the memory, instead of giving the memory address already allocated by memfd_create.

下面的代码演示了这一点. 在其输出中,每个mmap地址都以4096递增,这是内存的大小,由fd引用:

This is demonstrated by the following code. In its output each mmap address is incremented by 4096, which is the size of memory, referred to by fd:

0x7f98411c1000
0x7f98411c0000

mmap给出了直接地址, 输出中的地址将是相同的.

whereas if mmap had given the direct address, addresses in the output would be the same.

#include <stdio.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
int main(void)
{
    int fd = syscall(SYS_memfd_create, "shm", 0);
    if (fd == -1) return 1;

    size_t size = 4096; /* minimal */

    int check = ftruncate(fd, size);
    if (check == -1) return 1;

    void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr == MAP_FAILED) return 1;

    void *ptr2 = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr2 == MAP_FAILED) return 1;

    printf("%p\n%p\n", ptr, ptr2);

    return 0;
}

因此,如何获取直接地址,避免内存重复 通过mmap?

So, how to get direct address, avoiding memory duplication by mmap?

推荐答案

由于您已经在自己的答案中写下了要点,因此不确定是否仍然需要答案,只是添加此内容以确保它是完整的:

Not sure if you still need an answer, since you have written the main point in your own answer, but just adding this to be sure it is complete:

memfd_create创建一个仅内存文件(表示它不存储在磁盘上,尽管可以换出).在您写答案时,它会返回一个文件描述符.

memfd_create creates a memory-only file (meaning it is not stored on disk, although it can be swapped out). As you write in your answer, it returns a file descriptor.

mmap确保文件描述符后面的文件在内存中(如果是纯内存文件,则不需要任何操作),并为您提供指向该内存的指针.它不会复制内存(从磁盘映射文件时,磁盘之间的复制除外).如果同一文件被多次映射,则每次对mmap的调用都会保留一个新的虚拟内存区域,但是所有这些区域都将访问物理内存的同一部分.

mmap ensures that the file behind a file descriptor is in memory (which requires no action in case of a memory-only file), and gives you a pointer to that memory. It does not copy the memory (except from disk to memory when mapping a file from disk). If the same file is mapped multiple times, each call to mmap reserves a new region of virtual memory, but all those regions access the same portion of physical memory.

因此,对您的问题的简短回答是您误解了mmap;它不会复制内存,这是解决您的问题的完美解决方案.

So the short answer to your question is that you misunderstand mmap; it does not copy the memory, and it is the perfect solution to your problem.

这篇关于如何从memfd_create获取内存地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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