Solved Examine all IPC shared memory segments

Bill Evans at Mariposa

Active Member

Reaction score: 16
Messages: 120

As root, I can examine the characteristics of all IPC shared memory segments. But is there a way to see them all that's more straightforward than throwing a Brazillion segment IDs at the wall to see which ones stick, as below?
Code:
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
  int             lo_result;
  int             lo_shmid;
  long long       lo_long;
  struct shmid_ds lo_shmbuf;

  for(lo_long=0;
      lo_long<0x100000000;
      lo_long++
     )
  {
    lo_shmid=lo_long;
    lo_result=shmctl(lo_shmid,IPC_STAT,&lo_shmbuf);
    if(lo_result==0)
    {
      printf("got 0x%08X\n",lo_shmid);
    }
    else
    if(errno==EINVAL)
    {
      /* empty block */
    }
    else
    {
      perror("shmctl");
      exit(1);
    }
  }
  return 0;
} /* main() */
 

ralphbsz

Daemon

Reaction score: 1,269
Messages: 2,033

The ipcs utility can do it. I last used SysV shared memory in 1994 or 1995 (on AIX version 2), so I've forgotten many details. But look at the source code for the ipcs program, and you should find it.

Educated guess: Like many things, this is probably uses OS-specific code, probably a sysctl on *BSD. In Linux, it would probably be somewhere in the /sys file system.
 
Top