共享内存Segv
我尝试使用Boost,并在分配了3000个大小为24的对象后有一个segv,现在我开始使用 sys/ipc.h 和 sys/shm.h ,我分配了25 mio字节(如果我正确理解这一点)
似乎也可以在我的linux机器上正常工作 ipcs -m 将显示分配的段
0x000081bc 917516 testUser 644 25000000 0
sysctl -p 将打印
kernel.shmmax = 25500000
由于某种原因它可以工作直到程序达到“43406 x 24字节”那就是它将segv的地方。我很高兴得到一些提示我的问题所在。另请注意,这是否是分配和使用对象共享内存的错误方法。
#define MAXMYMEM 25000000
int sharedMemId;
x *p_sharedMemory;
x *p_other;
sharedMemId = shmget(2232, MAXMYMEM, IPC_CREAT | 0644);
if(sharedMemId >= 0){
p_sharedMemory = (x*) shmat( sharedMemId, 0 , 0);
if(p_sharedMemory != ( x *)-1) {
cout << sizeof(x) << endl;
for(unsigned int i = 0 ; i < 1000000;i++ ){
(p_sharedMemory + (sizeof(x) * i))->setTest(i);
}
I have tried to use Boost and had a segv after 3000 allocations of objects of size 24, now i started to use sys/ipc.h and sys/shm.h , i allocate 25 mio bytes (if i understand this properly)
it seems also to work properly on my linux box ipcs -m will show the allocated segment
0x000081bc 917516 testUser 644 25000000 0
sysctl -p will print
kernel.shmmax = 25500000
For some reason it works until the program reaches "43406 x 24 bytes" that's the place it will segv. I would be glad to get some hints where my problem lies. Please also note if this is the wrong way to allocate and use the shared memory with objects.
#define MAXMYMEM 25000000
int sharedMemId;
x *p_sharedMemory;
x *p_other;
sharedMemId = shmget(2232, MAXMYMEM, IPC_CREAT | 0644);
if(sharedMemId >= 0){
p_sharedMemory = (x*) shmat( sharedMemId, 0 , 0);
if(p_sharedMemory != ( x *)-1) {
cout << sizeof(x) << endl;
for(unsigned int i = 0 ; i < 1000000;i++ ){
(p_sharedMemory + (sizeof(x) * i))->setTest(i);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(p_sharedMemory + (sizeof(x) * i))->setTest(i);
为什么在这里使用
sizeof(x)
?向指向 x 的指针加 1 将指向下一个 x,而不是下一个字节。我怀疑这是你的问题。将
(p_sharedMemory + (sizeof(x) * i))->setTest(i);
更改为(p_sharedMemory + (sizeof(x) * i))->setTest(i);
Why are you using
sizeof(x)
here? Adding one to a pointer that points to an x will point at the next x, not the next byte. I suspect this is your problem.change
(p_sharedMemory + (sizeof(x) * i))->setTest(i);
to