我可以使用shmctl调整Linux共享内存的大小吗? [英] Can I resize Linux shared memory with shmctl?

查看:51
本文介绍了我可以使用shmctl调整Linux共享内存的大小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++应用程序,它通过shmget(2)在Linux系统上分配共享内存.我存储在共享内存中的数据会定期增长,并且我想以一种类似于realloc()增长常规内存的方式来调整共享内存的大小.有没有办法做到这一点?我在IBM网站上找到了一个提到SHM_SIZE命令的文档,但是Linux和BSD的联机帮助页中没有该文档,即使在特定于Linux的部分中也是如此.

解决方案

简单的答案:没有简单的方法.

原因很合理.共享内存分别附加到每个进程的虚拟空间.每个进程都有自己的虚拟地址空间.每个进程都可以在任意(不是字面上的对齐方式设置一些限制)任意地址处附加该段.系统如何保证通过将区域扩展4MiB来使该段的每个用户"都能够将bigget块容纳在与较小段以前相同的起始地址上?

但您不应该放弃!您可以发挥创造力.您可以想到一个 header 段的概念,您可以在其中存储有关实际有效负载段的信息.您可以使每个进程遵循某些规则,例如:当有效载荷段的ID(如 header segment 中描述的那样)与已知的不匹配时,重新连接有效载荷段.

建议:我怀疑您知道这一点,但是切勿将指向指针的数据保留在共享区域内,仅偏移.

我希望你能使用我的胡言乱语.

I have a C++ application that allocates shared memory on a Linux system via shmget(2). The data that I store in the shared memory grows periodically, and I'd like to resize the shared memory in a way analogous to the way realloc() grows regular memory. Is there a way to do this? I found a doc on IBM's site that mentions a SHM_SIZE command, but the Linux and BSD manpages do not have it, even in the Linux-specific sections.

解决方案

Simple answer: there is no easy way.

The reasons are pretty logical. Shared memory is being attached to virtual space of every process individually. Each process has it's own virtuall address space. Each process is free to attach the segment at any (not literally, alignment sets some restrictions) arbitrary address. How can system guarantee that, let's say by extending area by 4MiB, every 'user' of this segment will be able to fit bigget block at the same starting address where the smaller segment previously was?

But you should not give up! You can be creative. You can come up with the idea of having one header segment, where you store information about real payload segment. You can make every process to obey some rules as for example: reattach payload segment when its id, as described somewhere in header segment, does not match the known one.

The advice: I suspect you know this, but never keep pointers to data within shared region, only offset.

I hope you'll have some use of my gibberish.

这篇关于我可以使用shmctl调整Linux共享内存的大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆