CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / doc / ref / chap11.txt
Views: 418346
1
2
11 Processes
3
4
GAP can call other programs, such programs are called processes. There are
5
two kinds of processes: first there are processes that are started, run and
6
return a result, while GAP is suspended until the process terminates. Then
7
there are processes that will run in parallel to GAP as subprocesses and GAP
8
can communicate and control the processes using streams
9
(see InputOutputLocalProcess (10.8-2)).
10
11
12
11.1 Process and Exec
13
14
11.1-1 Process
15
16
Process( dir, prg, stream-in, stream-out, options )  operation
17
18
Process runs a new process and returns when the process terminates. It
19
returns the return value of the process if the operating system supports
20
such a concept.
21
22
The first argument dir is a directory object (see 9.3) which will be the
23
current directory (in the usual UNIX or MSDOS sense) when the program is
24
run. This will only matter if the program accesses files (including running
25
other programs) via relative path names. In particular, it has nothing to do
26
with finding the binary to run.
27
28
In general the directory will either be the current directory, which is
29
returned by DirectoryCurrent (9.3-4) –this was the behaviour of GAP 3– or a
30
temporary directory returned by DirectoryTemporary (9.3-3). If one expects
31
that the process creates temporary or log files the latter should be used
32
because GAP will attempt to remove these directories together with all the
33
files in them when quitting.
34
35
If a program of a GAP package which does not only consist of GAP code needs
36
to be launched in a directory relative to certain data libraries, then the
37
first entry of DirectoriesPackageLibrary (76.3-5) should be used. The
38
argument of DirectoriesPackageLibrary (76.3-5) should be the path to the
39
data library relative to the package directory.
40
41
If a program calls other programs and needs to be launched in a directory
42
containing the executables for such a GAP package then the first entry of
43
DirectoriesPackagePrograms (76.3-6) should be used.
44
45
The latter two alternatives should only be used if absolutely necessary
46
because otherwise one risks accumulating log or core files in the package
47
directory.
48
49
 Example 
50
gap> path := DirectoriesSystemPrograms();;
51
gap> ls := Filename( path, "ls" );;
52
gap> stdin := InputTextUser();;
53
gap> stdout := OutputTextUser();;
54
gap> Process( path[1], ls, stdin, stdout, ["-c"] );;
55
awk ls mkdir
56
gap> # current directory, here the root directory
57
gap> Process( DirectoryCurrent(), ls, stdin, stdout, ["-c"] );;
58
bin lib trans tst CVS grp prim thr two
59
src dev etc tbl doc pkg small tom
60
gap> # create a temporary directory
61
gap> tmpdir := DirectoryTemporary();;
62
gap> Process( tmpdir, ls, stdin, stdout, ["-c"] );;
63
gap> PrintTo( Filename( tmpdir, "emil" ) );
64
gap> Process( tmpdir, ls, stdin, stdout, ["-c"] );;
65
emil
66

67
68
prg is the filename of the program to launch, for portability it should be
69
the result of Filename (9.4-1) and should pass IsExecutableFile (9.6-4).
70
Note that Process does no searching through a list of directories, this is
71
done by Filename (9.4-1).
72
73
stream-in is the input stream that delivers the characters to the process.
74
For portability it should either be InputTextNone (10.9-1) (if the process
75
reads no characters), InputTextUser (10.6-1), the result of a call to
76
InputTextFile (10.5-1) from which no characters have been read, or the
77
result of a call to InputTextString (10.7-1).
78
79
Process is free to consume all the input even if the program itself does not
80
require any input at all.
81
82
stream-out is the output stream which receives the characters from the
83
process. For portability it should either be OutputTextNone (10.9-2) (if the
84
process writes no characters), OutputTextUser (10.6-2), the result of a call
85
to OutputTextFile (10.5-2) to which no characters have been written, or the
86
result of a call to OutputTextString (10.7-2).
87
88
options is a list of strings which are passed to the process as command line
89
argument. Note that no substitutions are performed on the strings, i.e.,
90
they are passed immediately to the process and are not processed by a
91
command interpreter (shell). Further note that each string is passed as one
92
argument, even if it contains space characters. Note that input/output
93
redirection commands are not allowed as options.
94
95
In order to find a system program use DirectoriesSystemPrograms (9.3-6)
96
together with Filename (9.4-1).
97
98
 Example 
99
gap> path := DirectoriesSystemPrograms();;
100
gap> date := Filename( path, "date" );
101
"/bin/date"
102

103
104
The next example shows how to execute date with no argument and no input,
105
and collect the output into a string stream.
106
107
 Example 
108
gap> str := "";; a := OutputTextString(str,true);;
109
gap> Process( DirectoryCurrent(), date, InputTextNone(), a, [] );
110
0
111
gap> CloseStream(a);
112
gap> Print(str);
113
Fri Jul 11 09:04:23 MET DST 1997
114

115
116
11.1-2 Exec
117
118
Exec( cmd, option1, ..., optionN )  function
119
120
Exec runs a shell in the current directory to execute the command given by
121
the string cmd with options option1, ..., optionN.
122
123
 Example 
124
gap> Exec( "date" );
125
Thu Jul 24 10:04:13 BST 1997
126

127
128
cmd is interpreted by the shell and therefore we can make use of the various
129
features that a shell offers as in following example.
130
131
 Example 
132
gap> Exec( "echo \"GAP is great!\" > foo" );
133
gap> Exec( "cat foo" );
134
GAP is great!
135
gap> Exec( "rm foo" );
136

137
138
Exec calls the more general operation Process (11.1-1). The function Edit
139
(6.10-1) should be used to call an editor from within GAP.
140
141
142