开发者

Is opening a shared memory multiple times bad in any way?

I have a class which opens a posix shared memory using shm_open. Now I have to create objects of this class multiple times (5-10 objects total), and that means the same shared memory is going to be opened that many times.

The shared memory has variable size, and can be from 1k to 500k.

The questions regarding this are :

1. Is this going to cause increased memory usage? (I do n开发者_StackOverflow社区ot see this happening, so my guess is no)

2. Is this going to cause bad performances when accessing shared memory?

3. Anything else that can go wrong?


The answer is on the page you linked shm_open to:

shm_open() creates and opens a new, or opens an existing, POSIX shared memory object. A POSIX shared memory object is in effect a handle which can be used by unrelated processes to mmap(2) the same region of shared memory.

To access a shared memory object, you need a handle to it. shm_open() can be used both to open (create) new, and reopen existing shared memory object. No memory increase nor bad performances occur when reopening an existing object.


It will not increase memory usage, however you use up handles each time you open a shared memory and you may run out. (Each process has a limit to the number of open handles.)

You need to manage access to the shared memory possibly using synchronization objects such as semaphores.

Mmap maps to a different base-address each time, so storing pointers pointing into shared memory is a bad idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜