Karim Belabas on Sun, 11 Jan 2004 19:42:28 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Using fifos for in/output


* Bill Allombert <allomber@math.u-bordeaux.fr> [2004-01-08 20:26]:
> On Thu, Jan 08, 2004 at 02:15:33PM +0000, Prof. J. E. Cremona wrote:
> > [I am sending this to pari-dev, though I do not seem to have received
> > anything from pari-dev for ages.  So this is partly a test to see if I
> > still subscribed to that.]
> > 
> > A couple of months ago there were some interesting postings from Bill
> > and Karim about using a fifo (special unix file) for input.  I am
> > interested in using a second fifo for output as well, either by
> > redirecting stout or by using write().
> > 
> > I really want this to work from within a C/C++ program, so that I can
> > start up a gp process in the background, have my program occasionally
> > send it input lines via its input fifo and then pick up the (one-line)
> > output from its output fifo.  This would be more efficient than my
> > current practice of starting a new "slave" gp for each such
> > calculation, and getting its output via a temporary (ordinary) file to
> > which gp directs its output.
> 
> Maybe you can reuse code from pariGUIde, 
> <http://sourceforge.net/projects/pariguide/>
> file gpcontroller.cpp,
> <http://cvs.sourceforge.net/viewcvs.py/*checkout*/pariguide/pariguide/pariguide/gpcontroller.cpp>
> 
> 
> It implement exactly this functionnality.

You may also be able to reuse the code from TeXmacs (see
hhtp://www.texmacs.org), in particular src/System/Link/pipe_link.cpp .

It works more or less like this:

  #define IN  0
  #define OUT 1

  pipe (pp_in );
  pipe (pp_out);
  pipe (pp_err); pid = fork ();

  if (pid==0) { // the child
    close (pp_in  [OUT]);
    close (pp_out [IN ]);
    close (pp_err [IN ]);
    dup2  (pp_in  [IN ], STDIN );
    close (pp_in  [IN ]);
    dup2  (pp_out [OUT], STDOUT);
    close (pp_out [OUT]);
    dup2  (pp_err [OUT], STDERR);
    close (pp_err [OUT]);

    execl("gp", "-f", "-q", NULL);
  } else { // main process
    in  = pp_in  [OUT];  // STDIN  of gp process
    out = pp_out [IN ];  // STDOUT of gp process
    err = pp_err [IN ];  // STDERR of gp process
    close (pp_in [IN]);
    close (pp_out [OUT]);
    close (pp_err [OUT]);
  }

Hope this helps,


    Karim.
-- 
Karim Belabas                     Tel: (+33) (0)1 69 15 57 48
Dep. de Mathematiques, Bat. 425   Fax: (+33) (0)1 69 15 60 19
Universite Paris-Sud              http://www.math.u-psud.fr/~belabas/ 
F-91405 Orsay (France)            http://pari.math.u-bordeaux.fr/  [PARI/GP]