Note: process status (other than running) is system dependent.
Windows does not seem to support stopped and aborted. Also,
processes killed using the process_control
predicate (described
next) are often marked as invalid rather than exited, because
Windows seems to lose all information about such processes. Process
status might be inaccurate in some Unix systems as well, if the process
has terminated and wait() has been executed on that process.
This predicate succeeds, if the operation was performed successfully. Otherwise, it fails. The wait operation fails if the process specified in Pid does not exist or is not a child of the parent XSB process.
The kill operation might fail, if the process to be killed does not exist or if the parent XSB process does not have the permission to terminate that process. Unix and Windows have different ideas as to what these permissions are. See kill(2) for Unix and TerminateProcess for Windows.
Note: under Windows, the programmer's manual warns of dire consequences if one kills a process that has DLLs attached to it.
spawn_process/5
).
Each term has the form:
process(Pid,ToStream,FromStream,StderrStream,CommandLine)
.
spawn_process
, except for the following:
(1) The first argument is an atom or a list of atoms, like in
spawn_process
. However, if it is a list of atoms, then the
resulting shell command is obtained by string concatenation. This is
different from spawn_process
where each member of the list must
represent an argument to the program being invoked (and which must be
the first member of that list). (2) The last argument is the error
code returned by the shell command and not a process id. The code -1
and 127 mean that the shell command failed.
The shell/5 predicate is similar to spawn_process
in that
it spawns another process and can capture that process' input and
output ports.
The important difference, however, is that XSB will ways until the
process spawned by shell/5 terminates. In contrast, the process
spawned by spawn_process
will run concurrently with XSB. In
this latter case, XSB must explicitly synchronize with the spawned
subprocess using the predicate process_control/2
(using the wait operation), as described earlier.
The fact that XSB must wait until shell/5 finishes has a very
important implication: the amount of data the can be sent to and from
the shell command is limited (1K is probably safe). This is because the
shell command communicates with XSB via pipes, which have limited
capacity. So, if the pipe is filled, XSB will hang waiting for shell/5 to finish and shel/5 will wait for XSB to consume data
from the pipe. Thus, use spawn_process/5
for any kind of
significant data exchange between external processes and XSB.
Another difference between these two forms of spawning subprocesses is
that CmdSpec in shell/5 can represent any shell
statement, including those that have pipes and I/O redirection. In
contrast, spawn_process
only allows command of the form ``program
args''. For instance,
| ?- file_open(test,w,IOport), shell('cat | sort > data', IOport, none, none, ErrCode)As seen from this example, the same rules for blocking I/O streams apply to shell/5. Finally, we should note that the already familiar standard predicates shell/1 and shell/2 are implemented using shell/5.