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
createconsumes one non-empty command sequence whose first item is the program path.createalso accepts one single String-like command value and treats it as a one-item command sequence.create2accepts the same command sequence together with one options object.- Each command item is converted through
Stringview semantics before process creation. - On Linux and macOS,
linux/Processandmacos/Processboth re-export the sharedposix/Processimplementation (PosixProcessasProcess,toPosixProcessastoProcess); the items become the separateargventries ofexecvp. - On Windows, process creation consumes one command-line string;
createusesCommandEncoder.crtand UTF-16 conversion follows the encoding. create2andtoProcess2may select another encoder throughoptions.encodeCommand.
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
resultis aString. On failure it reads<function> failed, result=<error code>followed by a line feed; the function ispipe,fcntl,fork, orexecvpwith theerrnovalue (Windows:CreateProcessWwithGetLastError). On Linux and macOS, a program that cannot be executed is anexecvpfailure: 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.
Preconditions
Remarks
- Destroying a created
Processwaits without requesting the exit status.
Helper constructors
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
- A newly constructed
Processis uninitialized. - A successful
createorcreate2call initializes theProcesswrapper. - A created
Processowns one process resource untilwaitcompletes or the wrapper is destroyed. killleaves the created state unchanged untilwaitcompletes.waitclears the created state after the process terminates.killandwaituse assertion-based preconditions. Failures of POSIXkill/waitpidor WindowsTerminateProcess/WaitForSingleObject/GetExitCodeProcessprint<function> failed, result=<error code>followed by a line feed and exit the calling process with status 1. On Windows, aCloseHandlefailure is only reported;waitstill clears the wrapper's created state.FALSE @process.waitwaits for termination and returns no exit status.TRUE @process.waitreturns oneInt32exit-status value.- On Windows, that value is the process exit code converted to
Int32. - On Linux and macOS, a normal exit returns the normal exit status and any other termination form returns the negated raw wait status.
POSIX implementation (Linux and macOS)
- The shared POSIX implementation uses
fork,execvp,pipe,waitpid, andkill. - The child process executes the command through
execvp; the parent reads the child pipe to distinguish creation failure from a successfulexecvp.
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
- String: UTF-8 string views, owned strings, formatting helpers, and text conversion utilities.
- CommandLine: Command-line argument view construction and printing.
- linux/runningTime · macos/runningTime · windows/runningTime: Elapsed-time helper built on platform timing APIs.