qjs:os module provides low-level operating system functionality including file I/O, process management, signals, timers, and worker threads.
Most OS functions return
0 on success or a negative error code (-errno) on failure.File Operations
open(filename, flags, mode)
Opens a file and returns a file descriptor.
Path to the file
Bitwise OR of open flags (see below)
File permissions (used when creating files)
File descriptor (non-negative) or negative error code
Open Flags
os.O_RDONLY- Read-onlyos.O_WRONLY- Write-onlyos.O_RDWR- Read and writeos.O_APPEND- Append modeos.O_CREAT- Create file if it doesn’t existos.O_EXCL- Fail if file exists (used with O_CREAT)os.O_TRUNC- Truncate file to zero lengthos.O_TEXT- Text mode (Windows only)
close(fd)
Closes a file descriptor.
File descriptor to close
Returns 0 on success or
-errno on errorseek(fd, offset, whence)
Seeks to a position in the file.
File descriptor
Byte offset. Use BigInt for large files.
Reference point:
std.SEEK_SET, std.SEEK_CUR, or std.SEEK_ENDNew file position, or negative error code. Returns BigInt if offset was BigInt.
read(fd, buffer, offset, length)
Reads data from a file descriptor into an ArrayBuffer.
File descriptor
Destination buffer
Byte offset in buffer to start writing
Number of bytes to read
Number of bytes read, or negative error code
write(fd, buffer, offset, length)
Writes data from an ArrayBuffer to a file descriptor.
File descriptor
Source buffer
Byte offset in buffer to start reading
Number of bytes to write
Number of bytes written, or negative error code
File System Operations
remove(filename)
Deletes a file.
Path to file to remove
Returns 0 on success or
-errno on errorrename(oldname, newname)
Renames or moves a file.
Current path
New path
Returns 0 on success or
-errno on errorrealpath(path)
Resolves the canonical absolute pathname.
Path to resolve
Array containing
[absolutePath, errorCode]. Check if errorCode is 0.getcwd()
Returns the current working directory.
Array containing
[path, errorCode]exePath()
Returns the full path of the current executable.
Executable path or undefined if not available/supported
chdir(path)
Changes the current working directory.
New directory path
Returns 0 on success or
-errno on errormkdir(path, mode)
Creates a directory.
Directory path to create
Directory permissions
Returns 0 on success or
-errno on errormkdtemp(pattern)
Creates a temporary directory with a unique name.
Pattern where XXXXXX is replaced with random characters. Must end with XXXXXX.
Array containing
[path, errorCode]. Directory is created with mode 0o700.mkstemp(pattern)
Creates a temporary file with a unique name and opens it.
Pattern where XXXXXX is replaced with random characters. Must end with XXXXXX.
Array containing
[path, fd] where fd is the file descriptor or negative error code.stat(path) / lstat(path)
Returns file status information.
File path
Array containing
[statObject, errorCode]. The stat object contains:dev- Device IDino- Inode numbermode- File mode/permissionsnlink- Number of hard linksuid- User IDgid- Group IDrdev- Device ID (if special file)size- File size in bytesblocks- Number of blocks allocatedatime- Last access time (milliseconds since epoch)mtime- Last modification time (milliseconds since epoch)ctime- Last status change time (milliseconds since epoch)
lstat() returns information about the link itself for symbolic links, while stat() follows the link.File Mode Constants
os.S_IFMT- File type maskos.S_IFIFO- FIFO (named pipe)os.S_IFCHR- Character deviceos.S_IFDIR- Directoryos.S_IFBLK- Block deviceos.S_IFREG- Regular fileos.S_IFSOCK- Socketos.S_IFLNK- Symbolic linkos.S_ISGID- Set-group-ID bitos.S_ISUID- Set-user-ID bit
utimes(path, atime, mtime)
Changes file access and modification times.
File path
Access time in milliseconds since epoch
Modification time in milliseconds since epoch
Returns 0 on success or
-errno on errorsymlink(target, linkpath)
Creates a symbolic link.
Target path (what the link points to)
Path of the symbolic link to create
Returns 0 on success or
-errno on errorreadlink(path)
Reads the target of a symbolic link.
Path to symbolic link
Array containing
[target, errorCode]readdir(path)
Reads directory contents.
Directory path
Array containing
[entries, errorCode]. Entries include ”.” and ”..” if successful.TTY Functions
isatty(fd)
Checks if a file descriptor is a terminal.
File descriptor
True if fd is a TTY
ttyGetWinSize(fd)
Returns the terminal window size.
TTY file descriptor
Array
[width, height] or null if not availablettySetRaw(fd)
Sets the terminal to raw mode (no line buffering, no echo).
TTY file descriptor
Process Management
exec(args, options)
Executes a new process.
Command and arguments. First element is the command.
Optional configuration
If true, waits for process to complete and returns exit code. If false, returns process ID.
If true, searches for executable in PATH
Executable path (defaults to args[0])
Working directory for the process
File descriptor to use for stdin
File descriptor to use for stdout
File descriptor to use for stderr
Environment variables as key-value pairs
Set process UID (Unix only)
Set process GID (Unix only)
Supplementary group IDs (Unix only)
If blocking: exit code (positive) or negated signal number (negative). If non-blocking: process ID.
waitpid(pid, options)
Waits for a child process to change state (Unix system call).
Process ID to wait for
Wait options (e.g.,
os.WNOHANG)Array containing
[ret, status]. ret contains -errno on error.os.WNOHANG- Return immediately if no child has exited
dup(fd)
Duplicates a file descriptor.
File descriptor to duplicate
New file descriptor or negative error code
dup2(oldfd, newfd)
Duplicates a file descriptor to a specific descriptor number.
Source file descriptor
Target file descriptor number
Returns newfd on success or negative error code
pipe()
Creates a pipe for inter-process communication.
Array
[readFd, writeFd] or null on errorSignals
signal(signal, func)
Registers a signal handler.
Signal number (e.g.,
os.SIGINT)Handler function,
null for default handler, or undefined to ignore signalSignal Constants
os.SIGINT- Interrupt (Ctrl+C)os.SIGABRT- Abortos.SIGFPE- Floating point exceptionos.SIGILL- Illegal instructionos.SIGSEGV- Segmentation faultos.SIGTERM- Termination request
kill(pid, sig)
Sends a signal to a process.
Process ID
Signal number
Asynchronous I/O
setReadHandler(fd, func)
Registers a callback for when data is available to read.
File descriptor
Callback function, or null to remove handler
Only one read handler per file descriptor is supported.
setWriteHandler(fd, func)
Registers a callback for when the file descriptor is ready for writing.
File descriptor
Callback function, or null to remove handler
Timers
sleep(delay_ms)
Blocks for the specified duration (synchronous).
Delay in milliseconds
sleepAsync(delay_ms)
Asynchronously sleeps for the specified duration.
Delay in milliseconds
Promise that resolves after the delay
setTimeout(func, delay)
Calls a function after a delay.
Function to call
Delay in milliseconds
Timer ID for use with clearTimeout
clearTimeout(id)
Cancels a timer.
Timer ID returned by setTimeout
Platform Information
os.platform
Platform identifier:
"linux", "darwin", "win32", or "js"Workers (Threads)
Worker(module_filename)
Creates a new worker thread with an API similar to Web Workers.
Path to the module to execute in the worker thread (relative to current script)
Worker Static Properties
In a worker thread, represents the parent worker. Used to communicate with the parent.
Worker Instance Methods
postMessage(msg)
Sends a message to the worker.
Message to send. The message is cloned using structured clone algorithm.
SharedArrayBuffer instances are shared.Worker Instance Properties
onmessage
Getter and setter for the message handler function.
Function called when a message is received. Receives an event object with a
data property.A worker thread is kept alive as long as there is at least one non-null
onmessage handler.