tur/process
process management: pid, spawn, wait, exec, cwd, chdir.
Since: Phase B1
Pid
(Pid)
ChildHandle
(ChildHandle)
process/pid
(process/pid)
return the current process ID.
The PID of the calling process as a Pid.
(process/pid) ; => 12345
Since: Phase B2
process/ppid
(process/ppid)
return the parent process ID.
The PPID of the calling process as a Pid.
(process/ppid) ; => 1
Since: Phase B2
process/exit
(process/exit [code : int])
terminate the process with the given exit code.
| code | exit status (0 = success, non-zero = failure) |
(process/exit 0)
Since: Phase B2
process/exec
(process/exec [path : cstr argv : int])
replace the current process image with a new program.
| path | NUL-terminated path to the executable | |
| argv | cons list of cstr arguments (head = argv[0]) |
-1 if execvp fails (errno set); does not return on success.
(process/exec "/bin/ls" (cons "/bin/ls" (cons "-l" 0)))
Since: Phase B2
process/spawn
(process/spawn [path : cstr argv : int])
fork and exec a new child process.
| path | path to the executable | |
| argv | cons list of cstr arguments (head = argv[0]) |
The child as a ChildHandle on success, or a ChildHandle wrapping -1 on fork failure. The handle is linear: reap it exactly once with process/wait.
(let [child (process/spawn "/bin/ls" (cons "/bin/ls" (cons "-l" 0)))]
(process/wait child))
Since: Phase B2
process/wait
(process/wait [child : ChildHandle])
wait for a child process to exit, consuming its handle.
| child | ChildHandle returned by process/spawn (consumed) |
The exit status code (0 = clean exit), -1 on error.
(process/wait child) ; => 0
Since: Phase B2
process/run
(process/run [path : cstr argv : int])
spawn a child process and wait for it to finish.
| path | path to the executable | |
| argv | cons list of cstr arguments |
An ok result (:int pointer) containing the exit code, or an err result containing a strdup'd error message cstr. Free with result-free when done.
(process/run "/bin/ls" (cons "/bin/ls" 0))
Since: Phase B2
process/capture
(process/capture [path : cstr argv : int])
run a command and capture its stdout as a string.
| path | path to the executable | |
| argv | cons list of cstr arguments |
An ok result containing a heap-allocated NUL-terminated cstr of the captured stdout, or an err result with an error message cstr. Free captured output with free(); free the result with result-free.
(process/capture "/bin/echo" (cons "/bin/echo" (cons "hi" 0)))
Since: Phase B2
process/cwd
(process/cwd)
return the current working directory.
A heap-allocated NUL-terminated cstr of the cwd, or NULL on error. The caller must free the returned string.
(process/cwd) ; => "/home/user/project"
Since: Phase B2
process/chdir
(process/chdir [path : cstr])
change the current working directory.
| path | the target directory path cstr |
0 on success, -1 on error (errno set).
(process/chdir "/tmp") ; => 0
Since: Phase B2