Can I resize Linux shared memory with shmctl?

后端 未结 3 2141
情话喂你
情话喂你 2021-02-20 03:03

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 th

3条回答
  •  忘掉有多难
    2021-02-20 03:51

    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.

提交回复
热议问题