有没有可能的方法将内存位置更改为 C 中的共享内存?
在c中,您可以将
shmid = shmget(SHMEM_KEY, sizeof(int*) * n , SHEMEM_MODE | IPC_CREAT);
int* shmem = shmat(shmid, NULL, 0);
第一个给定的可用内存空间分配为共享内存。
有没有办法将当前内存空间分配为共享内存?
In c you can do
shmid = shmget(SHMEM_KEY, sizeof(int*) * n , SHEMEM_MODE | IPC_CREAT);
int* shmem = shmat(shmid, NULL, 0);
to assign first given free memory space as a shared memory.
Is there any way to assigne current memory space as a shared memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用
shmat()
为您的共享内存添加别名创建到地址空间中的任意页面对齐范围因此这不会占用您已经拥有的一些内存并将其发布;它占用了一些新的共享内存,然后复制要发布的内容,然后使用
shmat
将其别名为您想要发布的内容 - 这具有相同的效果。You use
shmat()
to alias the shared memory you created to any arbitrary page-aligned range in your address-spaceSo this isn't taking some memory you already have and publishing it; its taking some new shared memory, you then copy what you want to publish across, then use
shmat
to alias it to where you had what you wanted to publish - this has the same effect.首先,我建议不要使用旧的
shmget
接口,而是使用更现代的 POSIX 接口
shm_open
和mmap
(如果有的话,您没有指定您的系统)
然后
mmap
允许您在您的地址空间中建议一个地址您想要的部分
并不完全是您所要求的,但您可以最接近
得到,我认为
First I would advise not to use the oldish
shmget
interfaces but themore modern POSIX interfaces
shm_open
andmmap
(if you have them,you didn't specify your system)
Then the
mmap
allows you to propose an address in your address spacewhere you'd like to have the segment
this is not exactly what you are asking for, but the closest you can
get, I think