The Standard ML Basis Library

The Unix structure

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 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 TextIO.stdOut and TextIO.stdIn streams, respectively, of the other process.


signature UNIX
structure Unix : UNIX


type proc
type signal
val executeInEnv : (string * string list * string list) -> proc
val execute : (string * string list) -> proc
val streamsOf : proc -> (TextIO.instream * TextIO.outstream)
val reap : proc -> OS.Process.status
val kill : (proc * signal) -> unix


type proc
represents a handle to an operating system process.

type signal
represents a Unix-like signal which can be sent to another process. Note that signal values must be obtained from some other structure. For example, an implementation providing Posix would probably use type signal = Posix.Signal.signal.

executeInEnv (cmd, args, env)
asks the operating system to execute the command cmd with the argument list args and in the environment env, as a separate process. (Typically, a string in the env list has the form "NAME=VALUE". See also OS.Process.getEnv.) A proc value representing the new process is returned.

execute (cmd, args)
asks the operating system to execute the command cmd with the argument list args as a separate process. The new process executes using the same environment as the calling process. A proc value representing the new process is returned.

For implementations providing Posix modules, this function is equivalent to executeInEnv (cmd, args, Posix.ProcEnv.environ ()).

streamsOf pr
returns the pair (ins, outs) of input and output streams associated with pr. When the process pr is created, its TextIO.stdOut becomes the source for the stream ins and the output stream outs becomes the source for its TextIO.stdIn.

reap pr
closes the input and output streams associated with pr, and then suspends the current process until the system process corresponding to pr terminates. Returns the exit status given by pr when it terminated.

kill (pr,s)
sends the signal s to the process pr.


Note that the interpretation of the string cmd depends very much on the underlying operating system. Typically, the cmd argument will be a full pathname. On Unix systems, simple command searching, allowing cmd to be a relative pathname, can be achieved by, for example, replacing cmd with /bin/sh and replacing the argument list args with "-c"::(concat (cmd::args)).

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 process 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 the Posix modules.

See Also

Posix, Posix.Signal, Posix.ProcEnv, TextIO, OS.Process

[ INDEX | TOP | Parent | Root ]

Last Modified January 31, 1997
Comments to John Reppy.
Copyright © 1997 Bell Labs, Lucent Technologies