thox process manager API

In order to understand this API, you must first read the explanations about how the startup and process manager work.

General constants

class system.QUANTUM: number

The Round Robin quantum as a number of instructions to set to debug.sethook().

class system.YIELD

The yield types in the process manager coroutine hijacking system.

BASIC: number

The basic yield, which means the process has manually called its version of the coroutine.yield() function. Can be returned in the process manager main loop if the process calls it from its main execution thread, and is then interpreted as YIELD.PREEMPT.

PREEMPT: number

The pre-empt yield, automatically sent from the hook set by debug.sethook() after a fixed number of instructions if the coroutine hasn’t yielded yet.

CALL: number

The call yield, sent when the user calls os.call().

ANSWER: number

The answer yield, sent when the user calls os.answer().

BIND: number

The bind yield, sent when the user calls os.bind().

UNBIND: number

The unbind yield, sent when the user calls os.unbind().

Process Manager

class system.Process

A process as represented from the process manager’s point of view.

thread: thread

The thread wrapping the function to execute, produced by coroutine.create().

state: number

The state the process currently is in, determining its treatment by the main process running loop, amongst one of the following:

RUNNING: number

The process is to be resumed on the next round. The code in the thread has its own environment for sandboxing purposes.

WAITING: number

The process is currently pulling events, which could be calls (if at least one RPC call is bound to it), answers, or events from queues the process is listening to.

ZOMBIE: number

The process has terminated or has been terminated, and is about to be removed.

answer: table

The arguments to return to the process on next resume, stored as a sequence. This is usually the answer to the last system call, or an empty table if the last yield was a pre-empt or equivalent.

It is usually composed of:

  • a boolean indicating the status, true being that the system call has been successful and false signifying that an error has occurred.

  • the payload as a table. It is composed of the message member if it is an error, and free-format members depending on the system call if the call has been successful.

events: table<os.Event>

The list of events to be sent to the function, as a sequence from which to pop events each time.

calls: table<cid, pm.Call>

The array of calls managed by this process.

outgoingCount: number

The number of outgoing calls.

incomingCount: number

The number of incoming calls.

alarms: table<pm.Alarm>

A list of alarms which the process has summoned for itself. Alarms in this array must be sorted by alarm trigger time.

commandLine: table<string>

The command line with which the process has been invoked. First element should contain the name or path of the script.

resume()

Resumes the coroutine if it is sleeping.

class system.Call

A representation of a call from a process.

type: number

The type of the managed call, amongst the following:

IN: number

The call is an incoming call that awaits an answer from the current process.

OUT: number

The call is an outgoing call that awaits an answer from another process.

ALARM: number

The call is an outgoing call to the process manager to trigger an alarm after some time.

pid: number

The process identifier of the callee or the caller for incoming and outgoing calls.

cid: number

The call identifier on the other side (callee or caller, which pid is the one above) for incoming and outgoing calls.

class system.Alarm

An alarm as represented in the process manager.

cid: number

The call identifier which created the alarm, with which to respond when the alarm is through.

alarmAt: number

The clock value at or after which to trigger the alarm event as an answer to the process’ call.

class system.ProcessManager

The process manager object, which manages processes, sandboxing, CPU time allocations, IPC, and permissions.

spawn(program, options)

Spawns a process and returns its Process IDentifier (PID). The process is spawned with the running state by default.

TODO: specify the options

Parameters
  • program (str) – The program as Lua code in textual form.

  • options (table) – The options for executing a process.

kill(pid)

Kills a process by settings its state to a zombie.

TODO: answer unanswered calls?

run()

Run the processes until there are none left.

RPC bindings

The process manager provides the initial process these RPC calls on its default context:

os.rpc.alarm(seconds)

Ask for an alarm to be sent to you in a given number of seconds. This function is usually called for sleeping a certain time, by calling it then waiting for the answer.

Example usage:

-- Sleep for five seconds and a half.
rpc.alarm(5.5)
Parameters

seconds (number) – Amount of seconds, possibly decimal.

Returns

The actual number of seconds passed since the call.

Return type

number