Alusus bindings for POSIX functions used to manage processes, file descriptors, pipes, and signals.
The library is a thin layer over the C library: each function maps directly to its POSIX
counterpart, so the usual POSIX semantics (return values, errno, etc.) apply.
The library targets Linux. The values of the constants match Linux, and prctl is only
available on Linux.
import "Apm";
Apm.importPackage("Alusus/Posix@0.1");
The following example forks a child process that sends a message to its parent through a pipe:
import "Srl/Console";
import "Srl/String";
import "Apm";
Apm.importPackage("Alusus/Posix@0.1");
use Srl;
func main {
def fds: array[Int[32], 2];
if Posix.pipe(fds~ptr) != 0 return;
// Flush buffered output so it is not duplicated in the child.
Posix.flush(0);
var pid: Int[32] = Posix.fork();
if pid < 0 return;
if pid == 0 {
// Child: write the message then exit immediately.
Posix.close(fds(0));
var msg: CharsPtr = "Hello from the child process!";
Posix.writeAll(fds(1), msg, String.getLength(msg));
Posix.close(fds(1));
Posix.exitNow(0);
}
// Parent: read until end of file.
Posix.close(fds(1));
def buf: array[Char, 256];
def received: String;
while true {
var count: ArchInt = Posix.read(fds(0), buf~ptr, 255);
if count <= 0 break;
received.append(buf~ptr, count);
}
Posix.close(fds(0));
def status: Int[32];
Posix.waitpid(pid, status~ptr, 0);
Console.print("Parent received: %s\n", received.buf);
}
main();
More examples can be found in the Examples folder:
- pipe.alusus: Communicating between a parent and a child process through a pipe.
- exec.alusus: Running an external program and capturing its output using
dup,execvp, andpoll. - signals.alusus: Ignoring signals, non-blocking waits, and killing a child process.
- process_info.alusus: Process id, working directory,
prctl, anddup.
The Examples/test_all.alusus script runs all the examples (English and Arabic) and
compares their output against Examples/expected_result.output. To run it against the local copy of the library,
link the repository into the examples' local package folder first:
cd Examples
mkdir -p .apm/Alusus
ln -s "$(pwd)/.." ".apm/Alusus/Posix@0.1"
alusus test_all.alusus
| Name | Value | Description |
|---|---|---|
STDIN |
0 | Standard input. |
STDOUT |
1 | Standard output. |
STDERR |
2 | Standard error output. |
Used with the events and revents fields of PollFd.
| Name | Value | Description |
|---|---|---|
POLLIN |
1 | There is data to read. |
POLLERR |
8 | An error condition occurred (only in revents). |
POLLHUP |
16 | The other end hung up (only in revents). |
| Name | Value | Description |
|---|---|---|
SIGKILL |
9 | Kills the process. Cannot be caught or ignored. |
SIGPIPE |
13 | Sent when writing to a pipe that has no readers. |
Used as the handler argument of signal.
| Name | Value | Description |
|---|---|---|
SIG_DFL |
0 | Use the default action of the signal. |
SIG_IGN |
1 | Ignore the signal. |
| Name | Value | Description |
|---|---|---|
WNOHANG |
1 | Makes waitpid return immediately if no child has exited yet. |
| Name | Value | Description |
|---|---|---|
PR_SET_PDEATHSIG |
1 | Used with prctl to set the signal sent to this process when its parent dies. |
class PollFd {
def fd: Int[32];
def events: Int[16];
def revents: Int[16];
}
Describes a file descriptor to watch using poll. Equivalent to POSIX struct pollfd.
fd: The file descriptor to watch.events: The events to watch for (e.g.POLLIN).revents: Filled bypollwith the events that actually occurred.
func fork(): Int[32];
Creates a new child process by duplicating the calling process. Equivalent to POSIX fork.
Returns 0 in the child process, the process id of the child in the parent process, or -1 on failure.
It is recommended to call flush(0) before forking so that output buffered by the parent is not
written twice. The child should end using exitNow rather than returning normally.
func pipe(fds: ptr[array[Int[32]]]): Int[32];
Creates a unidirectional pipe. Equivalent to POSIX pipe.
fds: Pointer to an array of two integers. On successfds(0)receives the read end andfds(1)receives the write end of the pipe.
Returns 0 on success, -1 on failure.
func read(fd: Int[32], buf: ptr, count: ArchWord): ArchInt;
Reads up to count bytes from a file descriptor into a buffer. Equivalent to POSIX read.
fd: The file descriptor to read from.buf: The buffer to receive the data.count: The maximum number of bytes to read.
Returns the number of bytes read, 0 at end of file, or -1 on failure. Note that no null terminator is added to the buffer.
func write(fd: Int[32], buf: ptr, count: ArchWord): ArchInt;
Writes up to count bytes from a buffer to a file descriptor. Equivalent to POSIX write.
fd: The file descriptor to write to.buf: The data to write.count: The number of bytes to write.
Returns the number of bytes actually written, which may be less than count, or -1 on failure. Use
writeAll to write the entire buffer.
func writeAll(fd: Int[32], buf: CharsPtr, count: ArchInt): Bool;
Writes the whole buffer to a file descriptor, retrying after partial writes. This is a helper function provided by this library, not a POSIX function.
fd: The file descriptor to write to.buf: The data to write.count: The number of bytes to write.
Returns true if all the data was written, false if a write fails.
func close(fd: Int[32]): Int[32];
Closes a file descriptor. Equivalent to POSIX close.
Returns 0 on success, -1 on failure.
func dup(fd: Int[32]): Int[32];
func dup(oldFd: Int[32], newFd: Int[32]): Int[32];
The first form duplicates a file descriptor using the lowest unused descriptor number. Equivalent to POSIX
dup.
The second form makes newFd a copy of oldFd, closing newFd first if needed. Equivalent to POSIX dup2.
This is commonly used to redirect the standard input or output of a child process, for example
dup(fds(1), STDOUT).
Both forms return the new file descriptor, or -1 on failure.
func poll(fds: ptr[array[PollFd]], count: ArchWord, timeout: Int[32]): Int[32];
Waits for events on a set of file descriptors. Equivalent to POSIX poll.
fds: Pointer to an array ofPollFdrecords describing the descriptors and events to watch.count: The number of records infds.timeout: Maximum time to wait in milliseconds. A negative value waits indefinitely, and 0 returns immediately.
Returns the number of records with non-zero revents, 0 on timeout, or -1 on failure.
func kill(pid: Int[32], sig: Int[32]): Int[32];
Sends a signal to a process. Equivalent to POSIX kill.
pid: The id of the target process.sig: The signal to send, e.g.SIGKILL.
Returns 0 on success, -1 on failure.
func waitpid(pid: Int[32], status: ptr[Int[32]], options: Int[32]): Int[32];
Waits for a child process to change state. Equivalent to POSIX waitpid.
pid: The id of the child to wait for, or -1 to wait for any child.status: Pointer to receive the status of the child. Can be 0 if the status is not needed. If the child exited normally(status >> 8) & 0xffholds its exit code; if it was killed by a signalstatus & 0x7fholds the signal number.options: 0 to block until the child changes state, orWNOHANGto return immediately.
Returns the id of the child whose state changed, 0 if WNOHANG was given and no child has changed
state yet, or -1 on failure.
func execvp(file: CharsPtr, argv: ptr[array[CharsPtr]]): Int[32];
Replaces the current process image with a new program, searching for it in the directories listed
in the PATH environment variable. Equivalent to POSIX execvp.
file: The name or path of the program to run.argv: Pointer to a null terminated array of arguments. By convention the first argument is the name of the program.
This function only returns on failure, in which case it returns -1.
func exitNow(code: Int[32]);
Terminates the current process immediately without running exit handlers or flushing stdio buffers.
Equivalent to POSIX _exit. This is the recommended way to end a child process created by fork.
code: The exit code of the process.
func flush(stream: ptr): Int[32];
Flushes a stdio output stream. Equivalent to C fflush.
stream: The stream to flush, or 0 to flush all output streams.
Returns 0 on success, or -1 on failure.
func signal(sig: Int[32], handler: ArchWord): ArchWord;
Sets the handler of a signal. Equivalent to POSIX signal.
sig: The signal number, e.g.SIGPIPE.handler:SIG_DFL,SIG_IGN, or the address of a handler function of typefunc (Int[32])cast toArchWord.
Returns the previous handler, or -1 on failure.
func prctl(option: Int[32], arg: ArchWord): Int[32];
Performs operations on the calling process. Equivalent to Linux prctl. Linux only.
option: The operation to perform, e.g.PR_SET_PDEATHSIG.arg: The argument of the operation. ForPR_SET_PDEATHSIGthis is the signal to send to this process when its parent dies.
Returns 0 on success (for most options), or -1 on failure.
func getpid(): Int[32];
Returns the id of the calling process. Equivalent to POSIX getpid.
func chdir(path: CharsPtr): Int[32];
Changes the current working directory of the process. Equivalent to POSIX chdir.
Returns 0 on success, -1 on failure.
func getcwd(buf: CharsPtr, size: ArchWord): CharsPtr;
Gets the current working directory of the process. Equivalent to POSIX getcwd.
buf: The buffer to receive the path.size: The size of the buffer in bytes.
Returns buf on success, or 0 on failure (e.g. if the buffer is too small).
Copyright (C) 2026 Sarmad Khalid Abdullah
This project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0).
See the COPYING and COPYING.LESSER files for details.