在linux中,如何为内存区域创建文件描述符

时间:2022-03-31 12:49:54

I have some program handling some data either in a file or in some memory buffer. I want to provide uniform way to handle these cases.

我有一些程序在文件或某些内存缓冲区中处理一些数据。我想提供统一的方法来处理这些案件。

I can either 1) mmap the file so we can handle them uniformly as a memory buffer; 2) create FILE* using fopen and fmemopen so access them uniformly as FILE*.

我可以1)mmap文件,以便我们可以统一处理它们作为内存缓冲区; 2)使用fopen和fmemopen创建FILE *,以便将它们统一访问为FILE *。

However, I can't use either ways above. I need to handle them both as file descriptor, because one of the libraries I use only takes file descriptor, and it does mmap on the file descriptor.

但是,我无法使用上述任何一种方法。我需要将它们作为文件描述符处理,因为我使用的其中一个库只接受文件描述符,并且它在文件描述符上进行mmap。

So my question is, given a memory buffer (we can assume it is aligned to 4K), can we get a file descriptor that backed by this memory buffer? I saw in some other question popen is an answer but I don't think fd in popen can be mmap-ed.

所以我的问题是,给定一个内存缓冲区(我们可以假设它与4K对齐),我们可以得到一个由这个内存缓冲区支持的文件描述符吗?我在其他一些问题中看到popen是一个答案,但我不认为popen中的fd可以是mmap-ed。

2 个解决方案

#1


8  

You cannot easily create a file descriptor (other than a C standard library one, which is not helpful) from "some memory region". However, you can create a shared memory region, getting a file descriptor in return.

您无法从“某些内存区域”轻松创建文件描述符(除了C标准库之外,没有用)。但是,您可以创建共享内存区域,获取文件描述符作为回报。

From shm_overview (7):

来自shm_overview(7):

shm_open(3)
Create and open a new object, or open an existing object. This is analogous to open(2). The call returns a file descriptor for use by the other interfaces listed below.

shm_open(3)创建并打开一个新对象,或打开一个现有对象。这类似于open(2)。该调用返回一个文件描述符,供下面列出的其他接口使用。

Among the listed interfaces is mmap, which means that you can "memory map" the shared memory the same as you would memory map a regular file.

列出的接口中有mmap,这意味着您可以像对内存映射常规文件一样“内存映射”共享内存。

Thus, using mmap for both situations (file or memory buffer) should work seamlessly, if only you control creation of that "memory buffer".

因此,如果只控制“内存缓冲区”的创建,那么对两种情况(文件或内存缓冲区)使用mmap都应该无缝工作。

#2


0  

You could write (perhaps using mmap) your data segment to a tmpfs based file (perhaps under /run/ directory), then pass the opened file descriptor to your library.

您可以将您的数据段(可能使用mmap)写入基于tmpfs的文件(可能位于/ run /目录下),然后将打开的文件描述符传递给您的库。

#1


8  

You cannot easily create a file descriptor (other than a C standard library one, which is not helpful) from "some memory region". However, you can create a shared memory region, getting a file descriptor in return.

您无法从“某些内存区域”轻松创建文件描述符(除了C标准库之外,没有用)。但是,您可以创建共享内存区域,获取文件描述符作为回报。

From shm_overview (7):

来自shm_overview(7):

shm_open(3)
Create and open a new object, or open an existing object. This is analogous to open(2). The call returns a file descriptor for use by the other interfaces listed below.

shm_open(3)创建并打开一个新对象,或打开一个现有对象。这类似于open(2)。该调用返回一个文件描述符,供下面列出的其他接口使用。

Among the listed interfaces is mmap, which means that you can "memory map" the shared memory the same as you would memory map a regular file.

列出的接口中有mmap,这意味着您可以像对内存映射常规文件一样“内存映射”共享内存。

Thus, using mmap for both situations (file or memory buffer) should work seamlessly, if only you control creation of that "memory buffer".

因此,如果只控制“内存缓冲区”的创建,那么对两种情况(文件或内存缓冲区)使用mmap都应该无缝工作。

#2


0  

You could write (perhaps using mmap) your data segment to a tmpfs based file (perhaps under /run/ directory), then pass the opened file descriptor to your library.

您可以将您的数据段(可能使用mmap)写入基于tmpfs的文件(可能位于/ run /目录下),然后将打开的文件描述符传递给您的库。