我已经使用Android共享内存api在Cpp中编写了代码。我使用了来自android ndk paltform-26的ASharedMemory_create api(对于arm64体系结构,为libandroid.so)并编译了代码。成功编译后,在运行程序时,我得到了相同的内存映射失败。
#include <iostream>
using namespace std;
#include <stdio.h>
#include "sharedmem.h"
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
int main()
{
int a = ASharedMemory_create("Memory",128);
cout << " The file descriptor in hex: " << hex << a << endl;
cout << " The file descriptor in dec: " << dec << a << endl;
size_t memsize =ASharedMemory_getSize(a);
cout << " The file descriptor size: " << memsize << endl;
void *mapper= mmap(0,128,PROT_READ | PROT_WRITE,MAP_SHARED,a,0);
cout << "the mapped value is : " << mapper << endl;
if (MAP_FAILED == mapper )
{
cout << "mmap failed";
}
return 0;
}
情况:1 十六进制文件描述符:f4244c0(随机值) dec中的文件描述符:-198809920(随机值) 文件描述符大小:40961573537 映射的值为:0x7ffeeefw7dsd(随机值) 映射的值为:0xffffffffffffffffff mmap失败
情况:2 十六进制文件描述符:f4244c0(随机值) 十月的文件描述符:1273477444 文件描述符大小:1273477444(如果fd为正,则size和fd相同) 映射的值为:0x7ffeeefw7dsd(随机值) 映射的值为:0xffffffffffffffffff mmap失败