In this week I had to write a little code to remove all IPC Shared Memory segments that were created by one process.

I had already done it on Linux and *BSD systems, but never on (Open)Solaris. Then, I tried to search (googling) some method to do it but without successful.

Linux and *BSD’s have a syscall named sysctl() to interface it, but sunos kernel has not.

Well, after some minutes breaking my brain, I discover a syscall easiest to use than sysctl…look the code below:

#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void show_shm (void)
{
  int i;
  int     *buf;
  uint_t  nids;
  uint_t  pnids;
  for (;;)
  {
    if (shmids(buf, nids, &pnids) != 0)
      exit(1);
    if (pnids <= nids)
      break;
    buf = realloc(buf, (nids = pnids) * sizeof (int));
  }
  for (i = 0; i < pnids; i++)
    printf ("%dn", buf[i]);
}

link2_os_blk_125

One Response to “Listing Shared Memory Segments in C”

  1. rocco Says:

    Ta ficando bom em opensolaris hein =p


Leave a Reply