有没有可能的方法将内存位置更改为 C 中的共享内存?

发布于 2024-09-18 08:44:08 字数 206 浏览 2 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千紇 2024-09-25 08:44:08

您使用 shmat() 为您的共享内存添加别名创建到地址空间中的任意页面对齐范围

因此这不会占用您已经拥有的一些内存并将其发布;它占用了一些新的共享内存,然后复制要发布的内容,然后使用 shmat 将其别名为您想要发布的内容 - 这具有相同的效果。

You use shmat() to alias the shared memory you created to any arbitrary page-aligned range in your address-space

So 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.

只有一腔孤勇 2024-09-25 08:44:08

首先,我建议不要使用旧的 shmget 接口,而是使用
更现代的 POSIX 接口 shm_openmmap (如果有的话,
您没有指定您的系统)

然后 mmap 允许您在您的地址空间中建议一个地址
您想要的部分

并不完全是您所要求的,但您可以最接近
得到,我认为

First I would advise not to use the oldish shmget interfaces but the
more modern POSIX interfaces shm_open and mmap (if you have them,
you didn't specify your system)

Then the mmap allows you to propose an address in your address space
where you'd like to have the segment

this is not exactly what you are asking for, but the closest you can
get, I think

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文