| 1 |
= MLtonProcess |
| 2 |
|
| 3 |
[source,sml] |
| 4 |
---- |
| 5 |
signature MLTON_PROCESS = |
| 6 |
sig |
| 7 |
type pid |
| 8 |
|
| 9 |
val spawn: {args: string list, path: string} -> pid |
| 10 |
val spawne: {args: string list, env: string list, path: string} -> pid |
| 11 |
val spawnp: {args: string list, file: string} -> pid |
| 12 |
|
| 13 |
type ('stdin, 'stdout, 'stderr) t |
| 14 |
|
| 15 |
type input |
| 16 |
type output |
| 17 |
|
| 18 |
type none |
| 19 |
type chain |
| 20 |
type any |
| 21 |
|
| 22 |
exception MisuseOfForget |
| 23 |
exception DoublyRedirected |
| 24 |
|
| 25 |
structure Child: |
| 26 |
sig |
| 27 |
type ('use, 'dir) t |
| 28 |
|
| 29 |
val binIn: (BinIO.instream, input) t -> BinIO.instream |
| 30 |
val binOut: (BinIO.outstream, output) t -> BinIO.outstream |
| 31 |
val fd: (Posix.FileSys.file_desc, 'dir) t -> Posix.FileSys.file_desc |
| 32 |
val remember: (any, 'dir) t -> ('use, 'dir) t |
| 33 |
val textIn: (TextIO.instream, input) t -> TextIO.instream |
| 34 |
val textOut: (TextIO.outstream, output) t -> TextIO.outstream |
| 35 |
end |
| 36 |
|
| 37 |
structure Param: |
| 38 |
sig |
| 39 |
type ('use, 'dir) t |
| 40 |
|
| 41 |
val child: (chain, 'dir) Child.t -> (none, 'dir) t |
| 42 |
val fd: Posix.FileSys.file_desc -> (none, 'dir) t |
| 43 |
val file: string -> (none, 'dir) t |
| 44 |
val forget: ('use, 'dir) t -> (any, 'dir) t |
| 45 |
val null: (none, 'dir) t |
| 46 |
val pipe: ('use, 'dir) t |
| 47 |
val self: (none, 'dir) t |
| 48 |
end |
| 49 |
|
| 50 |
val create: |
| 51 |
{args: string list, |
| 52 |
env: string list option, |
| 53 |
path: string, |
| 54 |
stderr: ('stderr, output) Param.t, |
| 55 |
stdin: ('stdin, input) Param.t, |
| 56 |
stdout: ('stdout, output) Param.t} |
| 57 |
-> ('stdin, 'stdout, 'stderr) t |
| 58 |
val getStderr: ('stdin, 'stdout, 'stderr) t -> ('stderr, input) Child.t |
| 59 |
val getStdin: ('stdin, 'stdout, 'stderr) t -> ('stdin, output) Child.t |
| 60 |
val getStdout: ('stdin, 'stdout, 'stderr) t -> ('stdout, input) Child.t |
| 61 |
val kill: ('stdin, 'stdout, 'stderr) t * Posix.Signal.signal -> unit |
| 62 |
val reap: ('stdin, 'stdout, 'stderr) t -> Posix.Process.exit_status |
| 63 |
end |
| 64 |
---- |
| 65 |
|
| 66 |
|
| 67 |
== Spawn |
| 68 |
|
| 69 |
The `spawn` functions provide an alternative to the |
| 70 |
`fork`/`exec` idiom that is typically used to create a new |
| 71 |
process. On most platforms, the `spawn` functions are simple |
| 72 |
wrappers around `fork`/`exec`. However, under Windows, the |
| 73 |
`spawn` functions are primitive. All `spawn` functions return |
| 74 |
the process id of the spawned process. They differ in how the |
| 75 |
executable is found and the environment that it uses. |
| 76 |
|
| 77 |
* `spawn {args, path}` |
| 78 |
+ |
| 79 |
starts a new process running the executable specified by `path` |
| 80 |
with the arguments `args`. Like `Posix.Process.exec`. |
| 81 |
|
| 82 |
* `spawne {args, env, path}` |
| 83 |
+ |
| 84 |
starts a new process running the executable specified by `path` with |
| 85 |
the arguments `args` and environment `env`. Like |
| 86 |
`Posix.Process.exece`. |
| 87 |
|
| 88 |
* `spawnp {args, file}` |
| 89 |
+ |
| 90 |
search the `PATH` environment variable for an executable named `file`, |
| 91 |
and start a new process running that executable with the arguments |
| 92 |
`args`. Like `Posix.Process.execp`. |
| 93 |
|
| 94 |
|
| 95 |
== Create |
| 96 |
|
| 97 |
`MLton.Process.create` provides functionality similar to |
| 98 |
`Unix.executeInEnv`, but provides more control control over the input, |
| 99 |
output, and error streams. In addition, `create` works on all |
| 100 |
platforms, including Cygwin and MinGW (Windows) where `Posix.fork` is |
| 101 |
unavailable. For greatest portability programs should still use the |
| 102 |
standard `Unix.execute`, `Unix.executeInEnv`, and `OS.Process.system`. |
| 103 |
|
| 104 |
The following types and sub-structures are used by the `create` |
| 105 |
function. They provide static type checking of correct stream usage. |
| 106 |
|
| 107 |
=== Child |
| 108 |
|
| 109 |
* `('use, 'dir) Child.t` |
| 110 |
+ |
| 111 |
This represents a handle to one of a child's standard streams. The |
| 112 |
`'dir` is viewed with respect to the parent. Thus a `('a, input) |
| 113 |
Child.t` handle means that the parent may input the output from the |
| 114 |
child. |
| 115 |
|
| 116 |
* `Child.{bin,text}{In,Out} h` |
| 117 |
+ |
| 118 |
These functions take a handle and bind it to a stream of the named |
| 119 |
type. The type system will detect attempts to reverse the direction |
| 120 |
of a stream or to use the same stream in multiple, incompatible ways. |
| 121 |
|
| 122 |
* `Child.fd h` |
| 123 |
+ |
| 124 |
This function behaves like the other `Child.*` functions; it opens a |
| 125 |
stream. However, it does not enforce that you read or write from the |
| 126 |
handle. If you use the descriptor in an inappropriate direction, the |
| 127 |
behavior is undefined. Furthermore, this function may potentially be |
| 128 |
unavailable on future MLton host platforms. |
| 129 |
|
| 130 |
* `Child.remember h` |
| 131 |
+ |
| 132 |
This function takes a stream of use `any` and resets the use of the |
| 133 |
stream so that the stream may be used by `Child.*`. An `any` stream |
| 134 |
may have had use `none` or `'use` prior to calling `Param.forget`. If |
| 135 |
the stream was `none` and is used, `MisuseOfForget` is raised. |
| 136 |
|
| 137 |
=== Param |
| 138 |
|
| 139 |
* `('use, 'dir) Param.t` |
| 140 |
+ |
| 141 |
This is a handle to an input/output source and will be passed to the |
| 142 |
created child process. The `'dir` is relative to the child process. |
| 143 |
Input means that the child process will read from this stream. |
| 144 |
|
| 145 |
* `Param.child h` |
| 146 |
+ |
| 147 |
Connect the stream of the new child process to the stream of a |
| 148 |
previously created child process. A single child stream should be |
| 149 |
connected to only one child process or else `DoublyRedirected` will be |
| 150 |
raised. |
| 151 |
|
| 152 |
* `Param.fd fd` |
| 153 |
+ |
| 154 |
This creates a stream from the provided file descriptor which will be |
| 155 |
closed when `create` is called. This function may not be available on |
| 156 |
future MLton host platforms. |
| 157 |
|
| 158 |
* `Param.forget h` |
| 159 |
+ |
| 160 |
This hides the type of the actual parameter as `any`. This is useful |
| 161 |
if you are implementing an application which conditionally attaches |
| 162 |
the child process to files or pipes. However, you must ensure that |
| 163 |
your use after `Child.remember` matches the original type. |
| 164 |
|
| 165 |
* `Param.file s` |
| 166 |
+ |
| 167 |
Open the given file and connect it to the child process. Note that the |
| 168 |
file will be opened only when `create` is called. So any exceptions |
| 169 |
will be raised there and not by this function. If used for `input`, |
| 170 |
the file is opened read-only. If used for `output`, the file is opened |
| 171 |
read-write. |
| 172 |
|
| 173 |
* `Param.null` |
| 174 |
+ |
| 175 |
In some situations, the child process should have its output |
| 176 |
discarded. The `null` param when passed as `stdout` or `stderr` does |
| 177 |
this. When used for `stdin`, the child process will either receive |
| 178 |
`EOF` or a failure condition if it attempts to read from `stdin`. |
| 179 |
|
| 180 |
* `Param.pipe` |
| 181 |
+ |
| 182 |
This will connect the input/output of the child process to a pipe |
| 183 |
which the parent process holds. This may later form the input to one |
| 184 |
of the `Child.*` functions and/or the `Param.child` function. |
| 185 |
|
| 186 |
* `Param.self` |
| 187 |
+ |
| 188 |
This will connect the input/output of the child process to the |
| 189 |
corresponding stream of the parent process. |
| 190 |
|
| 191 |
=== Process |
| 192 |
|
| 193 |
* `type ('stdin, 'stdout, 'stderr) t` |
| 194 |
+ |
| 195 |
represents a handle to a child process. The type arguments capture |
| 196 |
how the named stream of the child process may be used. |
| 197 |
|
| 198 |
* `type any` |
| 199 |
+ |
| 200 |
bypasses the type system in situations where an application does not |
| 201 |
want the it to enforce correct usage. See `Child.remember` and |
| 202 |
`Param.forget`. |
| 203 |
|
| 204 |
* `type chain` |
| 205 |
+ |
| 206 |
means that the child process's stream was connected via a pipe to the |
| 207 |
parent process. The parent process may pass this pipe in turn to |
| 208 |
another child, thus chaining them together. |
| 209 |
|
| 210 |
* `type input, output` |
| 211 |
+ |
| 212 |
record the direction that a stream flows. They are used as a part of |
| 213 |
`Param.t` and `Child.t` and is detailed there. |
| 214 |
|
| 215 |
* `type none` |
| 216 |
+ |
| 217 |
means that the child process's stream my not be used by the parent |
| 218 |
process. This happens when the child process is connected directly to |
| 219 |
some source. |
| 220 |
+ |
| 221 |
The types `BinIO.instream`, `BinIO.outstream`, `TextIO.instream`, |
| 222 |
`TextIO.outstream`, and `Posix.FileSys.file_desc` are also valid types |
| 223 |
with which to instantiate child streams. |
| 224 |
|
| 225 |
* `exception MisuseOfForget` |
| 226 |
+ |
| 227 |
may be raised if `Child.remember` and `Param.forget` are used to |
| 228 |
bypass the normal type checking. This exception will only be raised |
| 229 |
in cases where the `forget` mechanism allows a misuse that would be |
| 230 |
impossible with the type-safe versions. |
| 231 |
|
| 232 |
* `exception DoublyRedirected` |
| 233 |
+ |
| 234 |
raised if a stream connected to a child process is redirected to two |
| 235 |
separate child processes. It is safe, though bad style, to use the a |
| 236 |
`Child.t` with the same `Child.*` function repeatedly. |
| 237 |
|
| 238 |
* `create {args, path, env, stderr, stdin, stdout}` |
| 239 |
+ |
| 240 |
starts a child process with the given command-line `args` (excluding |
| 241 |
the program name). `path` should be an absolute path to the executable |
| 242 |
run in the new child process; relative paths work, but are less |
| 243 |
robust. Optionally, the environment may be overridden with `env` |
| 244 |
where each string element has the form `"key=value"`. The `std*` |
| 245 |
options must be provided by the `Param.*` functions documented above. |
| 246 |
+ |
| 247 |
Processes which are `create`-d must be either `reap`-ed or `kill`-ed. |
| 248 |
|
| 249 |
* `getStd{in,out,err} proc` |
| 250 |
+ |
| 251 |
gets a handle to the specified stream. These should be used by the |
| 252 |
`Child.*` functions. Failure to use a stream connected via pipe to a |
| 253 |
child process may result in runtime dead-lock and elicits a compiler |
| 254 |
warning. |
| 255 |
|
| 256 |
* `kill (proc, sig)` |
| 257 |
+ |
| 258 |
terminates the child process immediately. The signal may or may not |
| 259 |
mean anything depending on the host platform. A good value is |
| 260 |
`Posix.Signal.term`. |
| 261 |
|
| 262 |
* `reap proc` |
| 263 |
+ |
| 264 |
waits for the child process to terminate and return its exit status. |
| 265 |
|
| 266 |
|
| 267 |
== Important usage notes |
| 268 |
|
| 269 |
When building an application with many pipes between child processes, |
| 270 |
it is important to ensure that there are no cycles in the undirected |
| 271 |
pipe graph. If this property is not maintained, deadlocks are a very |
| 272 |
serious potential bug which may only appear under difficult to |
| 273 |
reproduce conditions. |
| 274 |
|
| 275 |
The danger lies in that most operating systems implement pipes with a |
| 276 |
fixed buffer size. If process A has two output pipes which process B |
| 277 |
reads, it can happen that process A blocks writing to pipe 2 because |
| 278 |
it is full while process B blocks reading from pipe 1 because it is |
| 279 |
empty. This same situation can happen with any undirected cycle formed |
| 280 |
between processes (vertexes) and pipes (undirected edges) in the |
| 281 |
graph. |
| 282 |
|
| 283 |
It is possible to make this safe using low-level I/O primitives for |
| 284 |
polling. However, these primitives are not very portable and |
| 285 |
difficult to use properly. A far better approach is to make sure you |
| 286 |
never create a cycle in the first place. |
| 287 |
|
| 288 |
For these reasons, the `Unix.executeInEnv` is a very dangerous |
| 289 |
function. Be careful when using it to ensure that the child process |
| 290 |
only operates on either `stdin` or `stdout`, but not both. |
| 291 |
|
| 292 |
|
| 293 |
== Example use of MLton.Process.create |
| 294 |
|
| 295 |
The following example program launches the `ipconfig` utility, pipes |
| 296 |
its output through `grep`, and then reads the result back into the |
| 297 |
program. |
| 298 |
|
| 299 |
[source,sml] |
| 300 |
---- |
| 301 |
open MLton.Process |
| 302 |
val p = |
| 303 |
create {args = [ "/all" ], |
| 304 |
env = NONE, |
| 305 |
path = "C:\\WINDOWS\\system32\\ipconfig.exe", |
| 306 |
stderr = Param.self, |
| 307 |
stdin = Param.null, |
| 308 |
stdout = Param.pipe} |
| 309 |
val q = |
| 310 |
create {args = [ "IP-Ad" ], |
| 311 |
env = NONE, |
| 312 |
path = "C:\\msys\\bin\\grep.exe", |
| 313 |
stderr = Param.self, |
| 314 |
stdin = Param.child (getStdout p), |
| 315 |
stdout = Param.pipe} |
| 316 |
fun suck h = |
| 317 |
case TextIO.inputLine h of |
| 318 |
NONE => () |
| 319 |
| SOME s => (print ("'" ^ s ^ "'\n"); suck h) |
| 320 |
|
| 321 |
val () = suck (Child.textIn (getStdout q)) |
| 322 |
---- |