Question

I've got an application that makes use of System V shared memory segments. Normally it manages these internally and no one needs to touch them. But for emergencies we've got a utility that manually clears the shared memory segments.

The problem is that to do it, it runs ipcs, and grabs chunks of the output using cut. That seems pretty fragile. It already runs slightly different commands on different platforms to reflect the fact that the ipcs output is formatted differently on Linux / AIX / Solaris etc.

Is there a better way to find shared memory segments than parsing ipcs output?

Was it helpful?

Solution

You could reimplement your own version of ipcs that gives the same output regardless of the OS. This requires some system-level programming though.

On Linux ipcs uses shmctl(0, SHM_INFO, ...) to find out the index of the highest used shared memory segment, then runs shmctl(index, SHM_STAT, ...) in a loop over all indexes from 0 to the highest index in order to obtain information about each segment. This should also work on FreeBSD (not documented but apparent from the kernel source), although on that OS ipcs uses sysctl to read the values of kern.ipc.shm*.

On Solaris ipcs uses shmids(NULL, 0, &nids) to obtain the number of segments IDs, then calls shmids(&ids, nids, ...) to obtain the list of actual IDs and then uses shmctl(id, IPC_STAT, ...) to obtain information on each segment.

ipcs is a fairly old instrument and one would not expect its output to change much in the future, at least not until POSIX shared memory completely displaces SysV IPC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top