Unix
structure
signature UNIX
(* OPTIONAL *)
structure Unix
:> UNIX (* OPTIONAL *)
The Unix
structure provides several high-level functions for creating and communicating with separate processes, in analogy with the popen
interface provided in the Unix operating system. This module provides a more flexible interface than that provided by the OS.Process.system
function. Using this module, a program can invoke a separate process and obtain input and output streams connected to the standard output and input streams, respectively, of the other process.
type ('a,'b) proc
type signal
datatype exit_status
= W_EXITED
| W_EXITSTATUS of Word8.word
| W_SIGNALED of signal
| W_STOPPED of signal
val fromStatus : OS.Process.status -> exit_status
val executeInEnv : string * string list * string list
-> ('a, 'b) proc
val execute : string * string list -> ('a, 'b) proc
val textInstreamOf : (TextIO.instream, 'a) proc
-> TextIO.instream
val binInstreamOf : (BinIO.instream, 'a) proc
-> BinIO.instream
val textOutstreamOf : ('a, TextIO.outstream) proc
-> TextIO.outstream
val binOutstreamOf : ('a, BinIO.outstream) proc
-> BinIO.outstream
val streamsOf : (TextIO.instream, TextIO.outstream) proc
-> TextIO.instream * TextIO.outstream
val reap : ('a, 'b) proc -> OS.Process.status
val kill : ('a, 'b) proc * signal -> unit
val exit : Word8.word -> 'a
type ('a,'b) proc
type signal
Posix
module would probably equate the signal
and Posix.Signal.signal
types.
datatype exit_status
= W_EXITED
| W_EXITSTATUS of Word8.word
| W_SIGNALED of signal
| W_STOPPED of signal
W_EXITSTATUS
will be non-zero.
If an implementation provides both the Posix
and Unix
structures, then Posix.Process.exit_status
and exit_status
must be the same type.
fromStatus sts
executeInEnv (cmd, args, env)
proc
value naming the child process. Strings in the env list typically have the form "name=value"
(see OS.Process.getEnv
).
The executeInEnv
function raises the OS.SysErr
exception if it fails. Reasons for failure include insufficient memory, too many processes, and the case where cmd does not name an executable file. If the child process fails to execute the command (i.e., the execve
call fails), then it should exit with a status code of 126.
execute (cmd, args)
proc
value naming the child process. The failure semantics of this function are the same as for executeInEnv
.
For implementations providing the Posix
modules, this function is equivalent to
fun execute (cmd, args) = executeInEnv (cmd, args, Posix.ProcEnv.environ ())
textInstreamOf pr
binInstreamOf pr
instream
connected to the standard output stream of the process pr.
Note that multiple calls to these functions on the same proc
value will result in multiple streams that all share the same underlying open file descriptor, which can lead to unpredictable effects because of the state inherent in file descriptors.
textOutstreamOf pr
binOutstreamOf pr
outstream
connected to the standard input stream of the process pr.
Note that multiple calls to these functions on the same proc
value will result in multiple streams that all share the same underlying open file descriptor, which can lead to unpredictable effects due to buffering.
streamsOf pr
(textInstream pr, textOutstream pr)
and is provided for backward compatibility.
reap pr
reap
is applied again to pr, it should immediately return the previous exit status.
Implementation note:
Typically, one cannot rely on the underlying operating system to provide the exit status of a terminated process after it has done so once. Thus, the exit status probably needs to be cached. Also note that
reap
should not return until the process being monitored has terminated. In particular, implementations should be careful not to return if the process has only been suspended.
kill (pr,s)
exit st
OS.Process.atExit
, flushes and closes all I/O streams opened using the Library, then terminates the SML process with termination status st.
BinIO
,OS.Process
,Posix
,Posix.ProcEnv
,Posix.Process
,Posix.Signal
,TextIO
Note that the interpretation of the string cmd in the execute
and executeInEnv
functions depends very much on the underlying operating system. Typically, the cmd argument will be a full pathname.
The semantics of Unix necessitates that processes that have terminated need to be reaped. If this is not done, information concerning the dead process continues to reside in system tables. Thus, a program using execute
or executeInEnv
should invoke reap
on any subprocess it creates.
Implementation note:
Although the flavor of this module is heavily influenced by Unix, and the module is simple to implement given the
Posix
subsystem, the functions are specified at a sufficiently high-level that implementations, including non-Unix ones, could provide this module without having to supply all of thePosix
modules.
Generated October 02, 2003
Last Modified May 9, 2003
Comments to John Reppy.
This document may be distributed freely over the internet as long as the copyright notice and license terms below are prominently displayed within every machine-readable copy.
Copyright © 2003 AT&T and Lucent Technologies. All rights reserved.
Permission is granted for internet users to make one paper copy for their
own personal use. Further hardcopy reproduction is strictly prohibited.
Permission to distribute the HTML document electronically on any medium
other than the internet must be requested from the copyright holders by
contacting the editors.
Printed versions of the SML Basis Manual are available from Cambridge
University Press.
To order, please visit
www.cup.org (North America) or
www.cup.cam.ac.uk (outside North America). |