std/sys/process
std/sys/process — what a program knows about its own invocation, and
how it starts another one.
Command-line arguments, working directory, environment variables,
process identity, abnormal termination — and ProcessBuilder /Process for a child.
Distinct from std/sys, which exposes runtime-wide constants and
primitives (clock, OS info). Normal termination is exit, declared
once in std/sys and reachable bare from the prelude; this module
does not redeclare it.
Where the platform lives. Nowhere in this file, for the two
questions that have a stable C answer: the process id is getpid on
unix and _getpid on the Windows CRT, so each is bound under@target(…) beside the one function that calls it. Everything else
that diverges is resolved once in the runtime and reached here as one
symbol per question — argv and the environment are UTF-16 on Windows
and only the wide entry points read them without loss, so std/ffi/runtime
is what this module reads them through rather than libc's narrow view.
API shape: free functions, some auto-imported via the prelude
(argc, hasEnv, pid, parentPid), the rest reachable viause std::sys::process::…;.
Free functions
| Type | Method and description |
|---|---|
| cwd() : string Absolute path of the process's current working directory."" when the platform will not answer — the cwd was deleted out fromunder the process, or its path is not representable. Not a throws:this is read by a diagnostic or a path join, and forcing a try/catch around a primitive that essentially never fails would cost every call site more than the case is worth. |
| chdir(path : string) : void Change the process's working directory to path.The platform answers only whether it moved, so the raise says what was asked rather than why it was refused: the reasons — no such directory, not a directory, no permission to enter it — are not distinguished by the call, and inventing one of them would be worse than naming none. |
| argc() : i32 Number of command-line arguments, including the program name at index 0. |
| __argAt(index : i32) : string The argument at index, or "" when there is none or it outruns thescratch buffer. Private: argAt is the checked form and is whatsource calls, so the sentinel never reaches a caller who did not ask for it. |
| argAt(index : i32) : string The argument at index in argv. |
| programName() : string The program name — argv[0], or "" where the platform suppliednone. Non-throwing where argAt(0) throws: a usage banner and a diagnosticprefix are what read it, and neither has a catch to offer. |
| getEnv(key : string) : string Read environment variable key. |
| hasEnv(key : string) : bool True iff environment variable key is present (even if empty). |
| setEnv(key : string, value : string) : void Set environment variable key to value; inherited by child processes.Refused once the program has created a thread, and silently: a reader holds a pointer into the environment table and a write frees what it replaces, with no lock either side could join. Set what a program needs before it spawns. |
| unsetEnv(key : string) : void Remove environment variable key from this process's environment.Refused after a thread exists, for the reason setEnv gives. |
| envOrDefault(key : string, fallback : string) : string Read environment variable key, falling back when it is unset OR setto the empty string. The two cases are indistinguishable through getEnv, and a callerthat wants a fallback wants it for both — which is what saves an if (!hasEnv("X")) guard at every call site. |
| __getProcessId() : i32 The unix spelling of this process's own id. |
| __getParentProcessId() : i32 The unix spelling of the id of the process that started this one. |
| __getProcessId() : i32 The Windows CRT's spelling of the same query. |
| pid() : i64 This process's own process ID. |
| parentPid() : i64 The parent process's ID, or 0 where the platform keeps none aprocess can read. Windows is that case: the id of the creating process lives in a structure only the snapshot API walks, and that walk names a reused id as readily as a live one — a parent that has exited leaves its number to somebody else. 0 is the honest answer, not a placeholderfor one that could be looked up. |
| parentPid() : i64 See the unix arm: this platform keeps no readable link to a parent. |
| abort() : void Abnormally terminate the process by raising SIGABRT. |
Method detail
#cwd
Absolute path of the process's current working directory."" when the platform will not answer — the cwd was deleted out from
under the process, or its path is not representable. Not a throws:
this is read by a diagnostic or a path join, and forcing a try/catch
around a primitive that essentially never fails would cost every call
site more than the case is worth.
#chdir
Change the process's working directory to path.
The platform answers only whether it moved, so the raise says what was
asked rather than why it was refused: the reasons — no such directory,
not a directory, no permission to enter it — are not distinguished by
the call, and inventing one of them would be worse than naming none.
path target directory (absolute or relative to the current cwd).IOException when path does not exist or is not a directory.#argc
Number of command-line arguments, including the program name at index 0.
#__argAt
The argument at index, or "" when there is none or it outruns the
scratch buffer. Private: argAt is the checked form and is what
source calls, so the sentinel never reaches a caller who did not ask
for it.
index position in argv; 0 is the program name#argAt
The argument at index in argv.
index position in argv; 0 is the program name.IndexOutOfBoundsException when index is < 0 or >= argc().#programName
The program name — argv[0], or "" where the platform supplied
none.
Non-throwing where argAt(0) throws: a usage banner and a diagnostic
prefix are what read it, and neither has a catch to offer.
#getEnv
Read environment variable key.
key environment variable name to look up."" when unset — use hasEnv to disambiguate.#hasEnv
True iff environment variable key is present (even if empty).
key environment variable name to test for existence.#setEnv
Set environment variable key to value; inherited by child processes.
Refused once the program has created a thread, and silently: a
reader holds a pointer into the environment table and a write frees
what it replaces, with no lock either side could join. Set what a
program needs before it spawns.
key environment variable name to assign.value value to bind; overwrites any prior binding.#unsetEnv
Remove environment variable key from this process's environment.
Refused after a thread exists, for the reason setEnv gives.
key environment variable name to remove; a no-op when already unset.#envOrDefault
Read environment variable key, falling back when it is unset OR set
to the empty string.
The two cases are indistinguishable through getEnv, and a caller
that wants a fallback wants it for both — which is what saves anif (!hasEnv("X")) guard at every call site.
key environment variable to read.fallback value returned when key is unset or empty.#__getProcessId
The unix spelling of this process's own id.
#__getParentProcessId
The unix spelling of the id of the process that started this one.
#__getProcessId
The Windows CRT's spelling of the same query.
#pid
This process's own process ID.
#parentPid
The parent process's ID, or 0 where the platform keeps none a
process can read.
Windows is that case: the id of the creating process lives in a
structure only the snapshot API walks, and that walk names a reused
id as readily as a live one — a parent that has exited leaves its
number to somebody else. 0 is the honest answer, not a placeholder
for one that could be looked up.
#parentPid
See the unix arm: this platform keeps no readable link to a parent.
#abort
Abnormally terminate the process by raising SIGABRT.
C class ProcessBuilder
Pattern: build the command with ProcessBuilder, callstart() to spawn, then drive the child through Process.
stdout/stderr are captured by default; the user reads them
after waitFor() returns. The program is named to the constructor and
every further argv entry is one addArg(arg), because Axle marshals no
array of strings across the FFI boundary.
Constructors
| Type | Method and description |
|---|---|
| constructor(command : string) Begin building a command. |
Method detail
#constructor
Begin building a command.
command the program to run (looked up via $PATH if not a path).Methods
| Type | Method and description |
|---|---|
| addArg(self, arg : string) : ProcessBuilder |
| setDirectory(self, dir : string) : ProcessBuilder |
| setEnv(self, key : string, value : string) : ProcessBuilder |
| inheritIO(self) : ProcessBuilder |
| start(self) : Process |
Method detail
#addArg
#setDirectory
#setEnv
#inheritIO
#start
C class Process
A spawned child process. Drive it through waitFor() /isAlive() / destroy(); once it has exited, read the capturedgetStdout() / getStderr() and its exitValue().
Methods
| Type | Method and description |
|---|---|
| waitFor(self) : i32 Wait until the child exits. A task parks and its carrier keeps running the tasks that share it. A second waitFor from another task waits forthe first to record the status and answers the same one. |
| exitValue(self) : i32 |
| isAlive(self) : bool |
| destroy(self) : void |
| pid(self) : i64 |
| getStdout(self) : string |
| getStderr(self) : string |
| close(self) : void close() aliases destroy() so a try-resources binding can cleanup a child process on scope exit. Pure Axle over destroy: thetwo are the same act, and one of them being a second FFI symbol would be a second place for that to stop being true. |
Method detail
#waitFor
Wait until the child exits. A task parks and its carrier keeps running
the tasks that share it. A second waitFor from another task waits for
the first to record the status and answers the same one.
#exitValue
#isAlive
#destroy
#pid
#getStdout
#getStderr
#close
close() aliases destroy() so a try-resources binding can clean
up a child process on scope exit. Pure Axle over destroy: the
two are the same act, and one of them being a second FFI symbol
would be a second place for that to stop being true.