next up previous contents index
Next: Limitations, Bugs, and Unclear Up: FIT2D as a Sub-Process Previous: Code to Calculate Addresses


Complete Example of Running FIT2D as a Sub-process with Shared Memory

The following example shows a complete program which allocates shared memory and starts FIT2D as a sub-process. The operations performed are trivial, but it should be able to serve as a useful template for real applications.

#define _POSIX_SOURCE 1
#define _SVID_SOURCE 1
                       
#include <stdio.h>
#include <errno.h>
#include <sys/shm.h>

/*+---------------------------------------------------------------------------*/
/* RunFIT2D as a sub-process test */
int main (int argc, char **argv)

{
  /* Local Constants */
#define MAX_INPUT 256

  /* Local Variables */
  float *ARRAYS; /* Pointer to shared memory arrays */
  FILE *command; /* File (pipe) for commands to FIT2D */
  char string[512]; /* Command string to run FIT2D */
  int memory_id; /* Identifier for shared memory */
  int num_bytes; /* Number of bytes of memory to allocated */
  int x; /* Loop variable for X-direction */
  int xdim = 1024; /* X-dimension of array */
  int y; /* Loop variable for Y-direction */
  int ydim = 1024; /* Y-dimension of array */
   
  /* Calculate size of shared memory segment */
  num_bytes = f2d_sharedsizec (xdim, ydim, TRUE, FALSE);
  printf ("Size of shared memory segment = %i\n", num_bytes);

  /* Create new shared memory section */
  memory_id = shmget(97, num_bytes, 0777 | IPC_CREAT);

  printf ("memory_id = %i\n", memory_id);

  /* Check memory identifier */
  if (memory_id == -1) {
    printf ("Problem creating shared memory:\n");

    if (errno == EINVAL) {
      printf ("problem with size being out of range\n");
    } else if (errno == EEXIST) {
      printf ("problem caused by segment already existing\n");
    }

    exit (0);
  }

  /* Attach to shared memory */
  if ((ARRAYS = shmat (memory_id, (float *) 0, 0)) == NULL) {
    printf("Could not attach shared memory\n");
    exit (0);
  }

  printf ("runfit2d: After shmat, base address = %i\n", ARRAYS);

  /* Fill "DATA" array with some values */
  for (y = 0; y < ydim; y = y + 1) {

    for (x = 0; x < xdim; x = x + 1) {
      ARRAYS[x + y * xdim] = Real((x - y) % 512);
    }

  }

  printf ("runfit2d: Ready to call FIT2D\n");

  /* Write FIT2D start-up command string */
  sprintf (string, 
   "/users/hammersl/FIT2D/LINUX/fit2d -dim%ix%i -com -data -id=%i", 
   xdim, ydim, memory_id);

  /* Open FIT2D as a sub-process with input through a pipe */
  command = (FILE *) popen (string, "w");

  /* Check sub-process has been started O.K. */
  if (command == NULL) {
    printf ("Problem starting sub-process");
    exit (0);
  }

  /* Send commands to FIT2D */
  fprintf (command, "I ACCEPT\n");
  fflush (command);

  /* Enter Image Processing interface to display data */
  fprintf (command, "IMAGE\n");
  fflush (command);

  /* Input image */
  fprintf (command, "INPUT\n");
  fprintf (command, "/DATA/test_001.mar3450\n");
  fflush (command);

  sleep(4);

  /* Zoom in */
  /* fprintf (command, "ZOOM IN\n");
  fprintf (command, "2\n");
  fprintf (command, "100.0\n");
  fprintf (command, "100.0\n");
  fprintf (command, "900.0\n");
  fprintf (command, "800.0\n");
  fflush (command); */
  
  sleep(4);

  /* Exit interface */
  fprintf (command, "EXIT\n");

  /* Close FIT2D */
  fprintf (command, "EXIT FIT2D\n");
  fprintf (command, "YES\n");
  fflush (command);

  /* Detach from shared memory */
  shmdt (ARRAYS);

  /* Delete shared memory segment */
  if (shmctl (memory_id, IPC_RMID , NULL) != 0) {
    printf ("ERROR: shmctl failed to remove shared memory segment\n");
  }

  /* Close pipe */
  pclose (command);

  exit (0);

}



Andrew Hammersley
2004-01-09