我知道这可能是一个奇怪的要求,但这里是:我必须在我的 x86-64 Linux ELF 可执行文件中创建一个读写部分/段,以便该部分可以在进程及其子进程之间共享。我会处理缓冲区中的任何数据同步要求,所以请不要担心这个麻烦。
我知道mmap
可以将“共享”页面映射到我的进程中,但由于某种原因,我不能使用mmap
或类似的功能。我能做的是使用一个大char
数组。到目前为止,我不知道这是否是一项不可能完成的任务。
这是我的测试代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
// reserve 20M bytes as JIT buffer
#define BUF_SIZE 20*1024*1024
char jit_buffer[BUF_SIZE] __attribute__((section(".buffer"), aligned(4096)));
int main()
{
printf("pid=%d\n\n", getpid());
printf(".main Start Address: %p\n", &main);
printf(".buffer Start Address: %p\n", jit_buffer);
getchar();
return 0;
}
gcc -o bssmap bssmap.c
执行后,我可以查看 .buffer 部分,但发现它是私有的,我不知道是否以及如何使其共享?
$ ./bssmap
pid=557836
.text Start Address: 0x564e9637f189
.buffer Start Address: 0x564e96383000
在另一个终端
$ cat /proc/557836/maps
564e9637e000-564e9637f000 r--p 00000000 08:02 7213494 bssmap
564e9637f000-564e96380000 r-xp 00001000 08:02 7213494 bssmap
564e96380000-564e96381000 r--p 00002000 08:02 7213494 bssmap
564e96381000-564e96382000 r--p 00002000 08:02 7213494 bssmap
564e96382000-564e97783000 rw-p 00003000 08:02 7213494 bssmap <-- 'private' but want 'shared'
顺便说一句,我知道 GCC 手册中有一个“共享”属性,但它适用于 Windows,在 Linux 中编译时会被忽略并发出警告。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句