views:

277

answers:

1

Hi.

I have some legacy code that uses shmget/shmat/shmdt to create, attach and manage shared memory segments.

The app with the code sometimes crashes, leaving the segments in memory. The code re-uses same segment key to reconnect to them, but the problem it uses different shared memory sizes every time, and unable to connect because of this.

My question is:

1) Is it possible to change the shared memory size on connection?

2) If not, how I can connect to the shared memory segment (even if I might not know the size), in order to erase it (for later re-creation of a newer one)?

Thanks!

+1  A: 

You can use shmctl to delete and create one of your own size. I presume the legacy code will try to use the existing shared memory if it is not able to shmget?

Moron
Hi.Shmctl requires shmid, which can be only retrieved by shmget.And shmget doesn't work if I don't know the exact size.This exactly the problem :).Any idea?
SyBer
You should be able to use strace to figure out what shmget calls the app is using.
Moron
I have the app source code - it's not a problem.Problem that the already allocated shared memory size could be different, than the one already specified.In this case shmget doesn't work - because it requires the EXACT size of the existing shared memory segment (which unknown at the time the shmget is running).
SyBer
You can't change the app source code? Even if the size is not known, by not using IPC_CREAT, you should be able to make shmget get the existing segment.
Moron
So the right command should be something in form of shmget(key, 0, 0777)?This will open it even with size unknown?
SyBer
Instead of 0777, why don't you use the macros provided? I believe it will work, as you are expected to provide size only when creating. Does not make sense to provide size if you are trying to open an existing one. In fact shmctl provides you a way to query that, given the key. Also, if the max possible size is known, you could just always use that and not worry about mismatched size in the first place.
Moron
shmctl wants shmid - which can be retrieved only with shmget.Good idea about the max size, though unfortunately there are no known limits here which I can use.I will try the size-less access and see how it works.
SyBer