Process is a cross-platform process wrapper with explicit creation, waiting, and termination operations. The platform modules are linux/Process, macos/Process, and windows/Process. Creation reports success and failure through result strings. A failed creation leaves the wrapper uninitialized.


Command arguments and encoding

Windows command line

The items are joined into one command line by an encoder with effect (commands -- buffer). It receives the entire command sequence and returns UTF-8 command-line bytes in a Nat8 Array or another object with span.stringView; a String works, but a Text alone does not. The result is converted to a zero-terminated UTF-16 buffer for CreateProcessW. windows/Process exports CommandEncoder with two encoders: CommandEncoder.crt (the default) writes the program and every argument in double quotes, escapes an embedded " as \", and doubles the backslashes that precede a quote or end an argument; the program itself must not contain ". CommandEncoder.plain joins the items with single spaces and quotes nothing. An options Dict without encodeCommand selects the default; for example, {encodeCommand: [CommandEncoder.plain];} selects the plain encoder.


Process

Methods

create

(commands -- result) Initializes one process from command arguments and returns one result string. Success returns the empty string.

Result

  • result is a String. On failure it reads <function> failed, result=<error code> followed by a line feed; the function is pipe, fcntl, fork, or execvp with the errno value (Windows: CreateProcessW with GetLastError). On Linux and macOS, a program that cannot be executed is an execvp failure: the child reports it through a pipe and the wrapper remains uninitialized.

Preconditions

  • The wrapper is not already initialized.
  • The command sequence is not empty.

create2

(commands options -- result) Windows-only creation variant that accepts one options object in addition to the command sequence.

isCreated

(-- created) Reports whether the wrapper currently refers to one created process.

kill

(--) Terminates the created process forcibly: SIGKILL on Linux and macOS, TerminateProcess with exit code 1 on Windows.

Preconditions

  • The wrapper currently refers to one created process.

wait

(FALSE --)
(TRUE -- exitStatus)
Waits for process termination; exitStatus has schema Int32.

Preconditions

  • The wrapper currently refers to one created process.
  • needExitStatus is a known Cond; it selects one of the two stack effects.

Remarks


Helper constructors

toProcess

(commands -- process result) Constructs one Process, initializes it through create, and returns the wrapper together with the result string.

toProcess2

(commands options -- process result) Windows-only helper that constructs one Process, initializes it through create2, and returns the wrapper together with the result string.

Lifecycle and results


POSIX implementation (Linux and macOS)


Examples

Linux exit-status example

"String"        use
"control"       use
"linux/Process" use

{} Int32 {} [
  process: result: ("/bin/sh" "-c" "exit 7") toProcess;;
  [result.size 0 =] "create failed" ensure
  (TRUE @process.wait toString LF) printList
  0
] "main" exportFunction

Expected Output

7

macOS exit-status example

"String"        use
"control"       use
"macos/Process" use

{} Int32 {} [
  process: result: ("/bin/sh" "-c" "exit 7") toProcess;;
  [result.size 0 =] "create failed" ensure
  (TRUE @process.wait toString LF) printList
  0
] "main" exportFunction

Expected Output

7

Linux failure-result example

"String"        use
"control"       use
"linux/Process" use

{} Int32 {} [
  process: result: "/definitely/not/found/command" toProcess;;
  (result.size 0 > toString " " @process.isCreated ~ toString LF) printList
  0
] "main" exportFunction

Expected Output

TRUE TRUE

See also