POSIX declarations used by Linux and macOS timing, descriptor, process, pipe, and context-switching helpers.

Functions that report failure with -1 set errno.


Constants

Error numbers

NameLinuxmacOSMeaning
EAGAIN1135Resource temporarily unavailable.
EBADF—9Bad file descriptor.
EINPROGRESS11536Operation in progress.
EINTR44Interrupted operation.
ENOENT—2No such file or directory.
EWOULDBLOCKEAGAINEAGAINOperation would block.

Descriptor control

NameLinuxmacOSMeaning
F_SETFD22Sets descriptor flags.
FD_CLOEXEC11Close-on-exec descriptor flag.
O_NONBLOCK20484Nonblocking file-status flag.

Memory mappings

NameLinuxmacOSMeaning
MAP_ANONYMOUS0x00200x1000Anonymous mapping flag.
MAP_PRIVATE0x00020x0002Private mapping flag.
MAP_STACK0x020000—Stack mapping flag.

Open flags

NameLinuxmacOSMeaning
O_CLOEXEC0x80000—Close-on-exec open flag.
O_CREAT0x40—Create-if-absent open flag.
O_RDWR0x2—Read-and-write open flag.

Memory protections

NameLinuxmacOSMeaning
PROT_NONE0x00x0No-access protection.
PROT_READ0x10x1Read protection.
PROT_WRITE0x20x2Write protection.

Process and wait

NameLinuxmacOSMeaning
SIGKILL99Terminate-process signal.
WNOHANG1—Do not wait for a child state change.

Clocks

NameLinuxmacOSMeaning
CLOCK_BOOTTIME7—Boot-time clock identifier.
CLOCK_MONOTONIC16Monotonic clock identifier.

Schemas

clockid_t

(-- clockid_t) Defines clock identifier storage.

Int32 alias

__darwin_mcontext_data

(-- __darwin_mcontext_data) Defines Darwin machine-context storage.

Nat8 824 array; macOS only

itimerspec

(-- itimerspec) Defines interval-timer settings.

Dict schema; Linux only

Linux

FieldSchemaRole
it_intervaltimespectimer interval
it_valuetimespeccurrent timer expiration

long

(-- long) Defines signed integer storage.

Intx alias

sigset_t

(-- sigset_t) Defines signal-set storage.

Nat32 alias; macOS only

stack_t

(-- stack_t) Defines the stack used by a context.

Dict schema; macOS only

macOS

FieldSchemaRole
ss_spNatxstack storage address
ss_sizeNatxstack size in bytes
ss_flagsInt32stack flags

time_t

(-- time_t) Defines time-value storage.

Intx alias

timespec

(-- timespec) Defines seconds and nanoseconds for one time value.

Dict schema

FieldSchemaRole
tv_sectime_tseconds component
tv_nseclongnanoseconds component

ucontext_t

(-- ucontext_t) Defines user-context storage.

Linux: Ucontext alias; macOS: Dict schema

Linux

FieldSchemaRole
uc_flagsNatxcontext flags
uc_link[ucontext_t] Mrefsuccessor context
uc_stack{ss_sp: Natx; ss_flags: Int32; ss_size: Natx;}stack used by this context
  • The remaining fields are processor-specific: linux/architecture selects the processor layout for Ucontext, and dependent schemas such as stack_t belong to linux/architecture/Ucontext, not linux/posix. For the linux/architecture link, see epoll_event.

macOS

FieldSchemaRole
uc_onstackInt32alternate-stack state
uc_sigmasksigset_tsignal mask
uc_stackstack_tstack used by this context
uc_link[ucontext_t] Mrefsuccessor context
uc_mcsizeNatxmachine-context size
uc_mcontext[__darwin_mcontext_data] Mrefmachine context
__mcontext_data__darwin_mcontext_datamachine-context storage

Helpers

WEXITSTATUS

(status -- result) Extracts the exit status from a wait status for which WIFEXITED is true and returns an Int32.

WIFEXITED

(status -- result) Reports whether a wait status represents normal process termination as a Cond.

Imported functions

Stack effects list a C function's parameters with the first parameter on top; see importFunction.

clock_gettime

(tp clk_id -- result) Writes the selected clock time to one timespec.

Inputs

  • tp: timespec Ref caller-owned writable time storage; the call writes it.
  • clk_id: clockid_t clock identifier.

Outputs

  • result: Int32 0 on success, or -1 on failure.

close

(fildes -- result) Closes a file descriptor.

Inputs

  • fildes: Int32 file descriptor to close.

Outputs

  • result: Int32 0 on success, or -1 on failure.

execvp

(argv file -- result) Replaces the current process image with the named executable.

Inputs

  • argv: Natx address of a zero-terminated array of addresses of zero-terminated argument strings.
  • file: Natx address of a zero-terminated executable path; a name without a slash is searched for in the directories listed in PATH.

Outputs

  • result: Int32 -1 on failure; on success, the call does not return.

fcntl

(varargs cmd fildes -- result) Applies a descriptor command.

Inputs

  • varargs: List or Tuple holding the additional arguments, the first one as item 0; use () when there are none.
  • cmd: Int32 descriptor command.
  • fildes: Int32 file descriptor to control.

Outputs

  • result: Int32 command-specific result, or -1 on failure.

fork

(-- result) Creates a child process.

Inputs

  • None.

Outputs

  • result: Int32 child process identifier in the parent, 0 in the child, or -1 on failure.

getcontext

(ucp -- result) Saves the current user context.

Inputs

  • ucp: ucontext_t Ref caller-owned writable context storage; the call writes it.

Outputs

  • result: Int32 0 on success, or -1 on failure.

getpid

(-- result) Returns the calling process identifier.

Inputs

  • None.

Outputs

  • result: Int32 calling process identifier.

kill

(sig pid -- result) Sends a signal to a process.

Inputs

  • sig: Int32 signal number.
  • pid: Int32 process identifier.

Outputs

  • result: Int32 0 on success, or -1 on failure.

makecontext

(varargs argc func ucp --) Initializes a user context for a function call.

Inputs

  • varargs: List or Tuple holding the additional arguments, the first one as item 0; use () when there are none.
  • argc: Int32 number of additional arguments passed to the target function.
  • func: Natx address of the target function.
  • ucp: ucontext_t Ref context obtained with getcontext, with uc_stack and uc_link configured; the call sets its entry point and arguments.

Outputs

  • None.

mmap

(offset fd flags prot length addr -- result) Maps memory into the process address space.

Inputs

  • offset: Intx mapping offset in bytes, a multiple of the page size.
  • fd: Int32 file descriptor for the mapped object, or -1 with MAP_ANONYMOUS in flags for an anonymous mapping.
  • flags: Int32 mapping flags, which must include MAP_PRIVATE or MAP_SHARED.
  • prot: Int32 memory-protection flags.
  • length: Natx nonzero mapping length in bytes.
  • addr: Natx requested mapping address, or 0nx for no address hint.

Outputs

  • result: Natx mapped address, or the all-bits-one value on failure.

mprotect

(prot length addr -- result) Changes protection on a memory range.

Inputs

  • prot: Int32 memory-protection flags.
  • length: Natx range length in bytes.
  • addr: Natx start address of the memory range, a multiple of the page size.

Outputs

  • result: Int32 0 on success, or -1 on failure.

open

(varargs flags path -- result) Opens a path with flags.

Inputs

  • varargs: List or Tuple holding the additional arguments, the first one as item 0; use () when there are none.
  • flags: Int32 open flags; O_CREAT also requires the file-creation mode as an Int32 item of varargs.
  • path: Natx address of a zero-terminated path string.

Outputs

  • result: Int32 new descriptor, or -1 on failure.

Remarks

  • Linux declaration only.

pause

(-- result) Waits for a signal.

Inputs

  • None.

Outputs

  • result: Int32 -1 after a signal handler returns.

Remarks

  • Linux declaration only.

pipe

(filedes -- result) Creates a pipe and fills its descriptor pair.

Inputs

  • filedes: {in: Int32; out: Int32;} Ref caller-owned descriptor-pair storage; the call writes the read descriptor to in and the write descriptor to out.

Outputs

  • result: Int32 0 on success, or -1 on failure.

read

(size buffer filedes -- result) Reads bytes from a file descriptor.

Inputs

  • size: Natx number of bytes requested.
  • buffer: Natx address of writable byte storage.
  • filedes: Int32 file descriptor to read.

Outputs

  • result: Intx number of bytes read, 0ix at end of file, or -1ix on failure.

swapcontext

(ucp oucp -- result) Saves one user context and activates another.

Inputs

  • ucp: ucontext_t Ref context to activate.
  • oucp: ucontext_t Ref caller-owned writable storage for the saved context; the call writes it.

Outputs

  • result: Int32 0 when the saved context is resumed, or -1 on failure.

waitpid

(options wstatus pid -- result) Waits for a child process state change.

Inputs

  • options: Int32 wait options.
  • wstatus: Int32 Ref caller-owned writable storage that receives the wait status, or a typed NIL Int32 Ref when the status is not needed.
  • pid: Int32 child process identifier to wait for.

Outputs

  • result: Int32 child process identifier, 0 when WNOHANG finds no state change, or -1 on failure.

write

(size buffer filedes -- result) Writes bytes to a file descriptor.

Inputs

  • size: Natx number of bytes to write.
  • buffer: Natx address of bytes to write.
  • filedes: Int32 file descriptor to write.

Outputs

  • result: Intx number of bytes written, or -1ix on failure.

Examples

Signature examples

Printing imported names during compilation shows exact imported signatures for representative declarations.

"control"     use
"linux/posix" use

{} Int32 {} [
  @clock_gettime printStack _:;
  @fcntl printStack _:;
  @pipe printStack _:;
  @waitpid printStack _:;
  0
] "main" exportFunction

Expected Output During Compilation

{clk_id: Int32; tp: {tv_sec: Intx; tv_nsec: Intx;} Ref;} Int32 {convention: "";} codeRef
{fildes: Int32; cmd: Int32;} Int32 {convention: ""; variadic: TRUE;} codeRef
{filedes: {in: Int32; out: Int32;} Ref;} Int32 {convention: "";} codeRef
{pid: Int32; wstatus: Int32 Ref; options: Int32;} Int32 {convention: "";} codeRef

macOS signature and layout example

Printing representative macOS declarations during compilation shows exact imported signatures and the field count of ucontext_t.

"control"     use
"macos/posix" use

{} Int32 {} [
  @clock_gettime printStack _:;
  @getpid printStack _:;
  ucontext_t fieldCount printStack _:;
  0
] "main" exportFunction

Expected Output During Compilation

{clk_id: Int32; tp: {tv_sec: Intx; tv_nsec: Intx;} Ref;} Int32 {convention: "";} codeRef
{} Int32 {convention: "";} codeRef
7

Pipe round-trip

"String"  use
"control" use
"posix"   use

{} Int32 {} [
  fds: {in: Int32; out: Int32;};
  pipeResult: @fds pipe;
  bytesWritten: 1nx "A" storageAddress fds.out new write;
  byte: 0n8;
  bytesRead: 1nx @byte storageAddress fds.in new read;
  closeIn: fds.in new close;
  closeOut: fds.out new close;
  ("pipe=" pipeResult LF
    "written=" bytesWritten LF
    "read=" bytesRead LF
    "byte=" byte LF
    "closeIn=" closeIn LF
    "closeOut=" closeOut LF) printList
  0
] "main" exportFunction

Expected Output

pipe=0
written=1
read=1
byte=65
closeIn=0
closeOut=0

See also