[aplusdev] adap (IPC) vs Python shm module

Brian Redman ber at ms.com
Sun Apr 1 11:37:57 EDT 2001


Hello Alexei,

Here's how you can interface to your python shared memory
writer using A+.

Assume the writer does something like this C program:
(perhaps rplace will show me how to do it in python)

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define DSIZE 512

main(int argc, char *argv[]) {
  key_t key = ftok("/", 'a');
  int shmflg = IPC_CREAT|0666;
  int shmid;
  double *x;

  printf("key: %d\n", key);

  shmid = shmget(key, DSIZE*sizeof(double), shmflg);
  if (shmid < 0) {
    perror("shmget");
    exit(1);
  }
  printf("id: %d\n", shmid);

  x = shmat(shmid, (void *)0, 0);
  if (x < 0) {
    perror("shmat");
    exit(1);
  }
  
  while (1) {
    int i;

    for (i = 0;i < DSIZE; i++) {
      x[i] += 1.0 * i;
    }
    sleep(10);
  }
}
	

Then your A+ program may look like this:


DSIZEû512;
SIZEOFDOUBLEû8;
xûDSIZEÒ0.0
'/lib/libc.so.6' _dyld ('shmget';'shmget';9 9 9 9;
                        'shmat';'shmat';9 9 9 9;
                        'ftok';'ftok';9 7 11) 

Õ'key:',îkeyûftok{1Ò'/';'a'};

Õ'id:',îshmidûshmget{key;DSIZE«SIZEOFDOUBLE;0};
if (shmid<0) {
  sys.perror{"shmget"};
  sys.exit{1};
}

addrûshmat{shmid;0;0};
if (addr<0) {
  sys.perror{"shmat"};
  sys.exit{1};
}

while (1) {
  (iûDSIZE) do {
    Õx[i]ûc.double_pointed_to_by{addr+SIZEOFDOUBLE«i};
  };
  sys.sleep(10);
};


Here is the same code in ascii which the A+ interpretter also
parses (I leave $mode uni for the curious).
	
$mode ascii

DSIZE := 512;
SIZEOFDOUBLE := 8;
x := DSIZE rho 0.0
'/lib/libc.so.6' _dyld ('shmget';'shmget';9 9 9 9;
                        'shmat';'shmat';9 9 9 9;
                        'ftok';'ftok';9 7 11) 

drop 'key:', form key := ftok{1 rho '/';'a'};

drop 'id:', form shmid := shmget{key;DSIZE * SIZEOFDOUBLE;0};
if (shmid<0) {
  sys.perror{"shmget"};
  sys.exit{1};
}

addr := shmat{shmid;0;0};
if (addr<0) {
  sys.perror{"shmat"};
  sys.exit{1};
}

while (1) {
  (i := DSIZE) do {
    drop x[i] := c.double_pointed_to_by{addr+SIZEOFDOUBLE * i};
  };
  sys.sleep(10);
};


I've copied this to the list to demonstate the dynamic loading
capability of A+. If it looks promising we can discuss what this
code is doing.

	ber



More information about the apluslist mailing list