2019-01-02 00:41:39 -05:00
|
|
|
do
|
2019-12-21 02:35:10 -05:00
|
|
|
local tTasks,nPid,nTimeout,cPid = {},1,0.25,0 -- table of tasks, next process ID, event timeout, current PID
|
2019-01-02 00:41:39 -05:00
|
|
|
function os.spawn(f,n) -- creates a process from function *f* with name *n*
|
2019-10-27 08:34:02 -04:00
|
|
|
tTasks[nPid] = {
|
|
|
|
c=coroutine.create(f), -- actual coroutine
|
|
|
|
n=n, -- process name
|
|
|
|
p=nPid, -- process PID
|
|
|
|
P=cPid, -- parent PID
|
|
|
|
e={} -- environment variables
|
|
|
|
}
|
2019-01-02 00:41:39 -05:00
|
|
|
if tTasks[cPid] then
|
|
|
|
for k,v in pairs(tTasks[cPid].e) do
|
|
|
|
tTasks[nPid].e[k] = tTasks[nPid].e[k] or v
|
|
|
|
end
|
2018-11-02 12:05:41 -04:00
|
|
|
end
|
|
|
|
nPid = nPid + 1
|
|
|
|
return nPid - 1
|
|
|
|
end
|
2019-01-02 00:41:39 -05:00
|
|
|
function os.kill(pid) -- removes process *pid* from the task list
|
|
|
|
tTasks[pid] = nil
|
|
|
|
end
|
2019-12-15 22:30:27 -05:00
|
|
|
function os.pid() -- returns the current process' PID
|
2019-10-27 08:34:02 -04:00
|
|
|
return cPid
|
|
|
|
end
|
2019-12-15 22:30:27 -05:00
|
|
|
function os.tasks() -- returns a table of process IDs
|
2019-10-27 08:34:02 -04:00
|
|
|
local rt = {}
|
|
|
|
for k,v in pairs(tTasks) do
|
|
|
|
rt[#rt+1] = k
|
|
|
|
end
|
|
|
|
return rt
|
|
|
|
end
|
2019-12-15 22:30:27 -05:00
|
|
|
function os.taskInfo(pid) -- returns info on process *pid* as a table with name and parent values
|
2019-11-08 21:52:39 -05:00
|
|
|
pid = pid or os.pid()
|
2019-11-08 21:14:58 -05:00
|
|
|
if not tTasks[pid] then return false end
|
2019-10-27 08:34:02 -04:00
|
|
|
return {name=tTasks[pid].n,parent=tTasks[pid].P}
|
|
|
|
end
|
|
|
|
function os.sched() -- the actual scheduler function
|
|
|
|
os.sched = nil
|
2018-11-02 12:05:41 -04:00
|
|
|
while #tTasks > 0 do
|
|
|
|
local tEv = {computer.pullSignal(nTimeout)}
|
|
|
|
for k,v in pairs(tTasks) do
|
|
|
|
if coroutine.status(v.c) ~= "dead" then
|
|
|
|
cPid = k
|
|
|
|
coroutine.resume(v.c,table.unpack(tEv))
|
|
|
|
else
|
|
|
|
tTasks[k] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-02 00:41:39 -05:00
|
|
|
function os.setenv(k,v) -- set's the current process' environment variable *k* to *v*, which is passed to children
|
|
|
|
if tTasks[cPid] then
|
|
|
|
tTasks[cPid].e[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function os.getenv(k) -- gets a process' *k* environment variable
|
|
|
|
if tTasks[cPid] then
|
|
|
|
return tTasks[cPid].e[k]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|