[CODE] shared memory and its management

From: Mark A. Heilpern (heilpern@MINDSPRING.COM)
Date: 05/02/98


Thanks to the headstart from Erwin Andreasen, I now
feel rather comfortable with declaring and finding shared
memory segments. However, it seems to me that unless
I can convince malloc to allocate from one or several,
they only hold limited usefulness (unless I write my
own memory manager, which I would rather not do.) So,
does anyone know if there id either a way to convince
malloc to manage a buffer i tell it about, or of a simple
replacement for malloc() that does?

By the way, for anyone interested in learning more about
shared memory, here is a program I wrote under Linux
kernel 2.0.33 which demonstrates some of what I imagine
are the more important uses. It's dumb, but it gets to the
point easily.



#include <stdio.h>
#include <sys/shm.h>

main()
{
  int id;
  unsigned int *segptr;

  id = shmget(0x11223344, 4, IPC_CREAT|IPC_EXCL|SHM_W|SHM_R);
  if (id<0) {
    id = shmget(0x11223344, 0, SHM_W|SHM_R);
    if (id>=0)printf("Create failed, segment found: %x\n", id);
    else {
      printf("Fatal: cannot create nor find the segment!\n");
      perror("shmget");
      exit(-1);
    }
  } else printf("Segment created: %x\n", id);

  system("ipcs -m");

  segptr = (unsigned int *)shmat(id, 0, 0);
  printf("The int before incrementing: %u\n", *segptr);
  (*segptr)++;
  printf("The int after incrementing: %u\n", *segptr);

  if (*segptr >= 5) {
    printf("Destroying the buffer.\n");
    shmctl(id, IPC_RMID, 0);
  }

  shmdt((char *)segptr);
}


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST