[ref] 11 Processes

[Top] [Up] [Previous] [Next] [Index]

# 11 Processes

### Sections

1. Process
2. Exec

GAP can call other programs, such programs are called processes. There are two kinds of processes: First there are processes that are started, run and return a result, while GAP is suspended until the process terminates. Then there are processes that will run in parallel to GAP as subprocesses and GAP can communicate and control the processes using streams (see InputOutputLocalProcess).

## 11.1 Process

- `Process(` dir`,` prg`,` stream-in`,` stream-out`,` options`) O`

  `Process` runs a new process and returns when the process terminates. It returns the return value of the process if the operating system supports such a concept.

  The first argument dir is a directory object (see Directories) which will be the current directory (in the usual UNIX or MSDOS sense) when the program is run. This will only matter if the program accesses files (including running other programs) via relative path names. In particular, it has nothing to do with finding the binary to run.

  In general the directory will either be the current directory, which is returned by `DirectoryCurrent` (see DirectoryCurrent) --this was the behaviour of GAP 3-- or a temporary directory returned by `DirectoryTemporary` (see DirectoryTemporary). If one expects that the process creates temporary or log files the latter should be used because GAP will attempt to remove these directories together with all the files in them when quitting.

  If a program of a GAP package which does not only consist of GAP code needs to be launched in a directory relative to certain data libraries, then the first entry of `DirectoryPackageLibrary` should be used. The argument of `DirectoryPackageLibrary` should be the path to the data library relative to the package directory.

  If a program calls other programs and needs to be launched in a directory containing the executables for such a GAP package then the first entry of `DirectoriesPackagePrograms` should be used.

  The latter two alternatives should only be used if absolutely necessary because otherwise one risks accumulating log or core files in the package directory.

  Examples

      gap> path := DirectoriesSystemPrograms();;
      gap> ls := Filename( path, "ls" );;
      gap> stdin := InputTextUser();;
      gap> stdout := OutputTextUser();;
      gap> Process( path[1], ls, stdin, stdout, ["-c"] );;
      awk    ls     mkdir

      gap> # current directory, here the root directory
      gap> Process( DirectoryCurrent(), ls, stdin, stdout, ["-c"] );;
      bin    lib    trans  tst    CVS    grp    prim   thr    two
      src    dev    etc    tbl    doc    pkg    small  tom

      gap> # create a temporary directory
      gap> tmpdir := DirectoryTemporary();;
      gap> Process( tmpdir, ls, stdin, stdout, ["-c"] );;
      gap> PrintTo( Filename( tmpdir, "emil" ) );
      gap> Process( tmpdir, ls, stdin, stdout, ["-c"] );;
      emil

  prg is the filename of the program to launch, for portability it should be the result of `Filename` (see Filename) and should pass `IsExecutableFile`. Note that `Process` does no searching through a list of directories, this is done by `Filename`.

  stream-in is the input stream that delivers the characters to the process. For portability it should either be `InputTextNone` (if the process reads no characters), `InputTextUser`, the result of a call to `InputTextFile` from which no characters have been read, or the result of a call to `InputTextString`.

  `Process` is free to consume all the input even if the program itself does not require any input at all.

  stream-out is the output stream which receives the characters from the process. For portability it should either be `OutputTextNone` (if the process writes no characters), `OutputTextUser`, the result of a call to `OutputTextFile` to which no characters have been written, or the result of a call to `OutputTextString`.

  options is a list of strings which are passed to the process as command line argument. Note that no substitutions are performed on the strings, i.e., they are passed immediately to the process and are not processed by a command interpreter (shell). Further note that each string is passed as one argument, even if it contains space characters. Note that input/output redirection commands are not allowed as options.

  Examples

  In order to find a system program use `DirectoriesSystemPrograms` together with `Filename`.

      gap> path := DirectoriesSystemPrograms();;
      gap> date := Filename( path, "date" );
      "/bin/date"

  Now execute `date` with no argument and no input, collect the output into a string stream.

      gap> str := "";; a := OutputTextString(str,true);;
      gap> Process( DirectoryCurrent(), date, InputTextNone(), a, [] );
      0
      gap> CloseStream(a);
      gap> Print(str);
      Fri Jul 11 09:04:23 MET DST 1997

  ## 11.2 Exec

- `Exec(` cmd`,` option1`, ...,` optionN`) F`

  `Exec` runs a shell in the current directory to execute the command given by the string cmd with options ``option1`, ...,` optionN``.

      gap> Exec( "date" );
      Thu Jul 24 10:04:13 BST 1997

  cmd is interpreted by the shell and therefore we can make use of the various features that a shell offers as in following example.

      gap> Exec( "echo \"GAP is great!\" > foo" );
      gap> Exec( "cat foo" );
      GAP is great!
      gap> Exec( "rm foo" );

  `Exec` calls the more general operation `Process` (see Process). `Edit` (see Edit) should be used to call an editor from within GAP.

  [Top] [Up] [Previous] [Next] [Index]

  GAP 4 manual
  May 2002