Operating system for OpenComputers
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

60 wiersze
1.4KB

  1. do
  2. local tTasks,nPid,nTimeout,cPid = {},1,0,0 -- table of tasks, next process ID, event timeout, current PID
  3. function os.spawn(f,n) -- creates a process from function *f* with name *n*
  4. tTasks[nPid] = {
  5. c=coroutine.create(f), -- actual coroutine
  6. n=n, -- process name
  7. p=nPid, -- process PID
  8. P=cPid, -- parent PID
  9. e={} -- environment variables
  10. }
  11. if tTasks[cPid] then
  12. for k,v in pairs(tTasks[cPid].e) do
  13. tTasks[nPid].e[k] = tTasks[nPid].e[k] or v
  14. end
  15. end
  16. nPid = nPid + 1
  17. return nPid - 1
  18. end
  19. function os.kill(pid) -- removes process *pid* from the task list
  20. tTasks[pid] = nil
  21. end
  22. function os.pid()
  23. return cPid
  24. end
  25. function os.tasks()
  26. local rt = {}
  27. for k,v in pairs(tTasks) do
  28. rt[#rt+1] = k
  29. end
  30. return rt
  31. end
  32. function os.taskInfo(pid)
  33. return {name=tTasks[pid].n,parent=tTasks[pid].P}
  34. end
  35. function os.sched() -- the actual scheduler function
  36. os.sched = nil
  37. while #tTasks > 0 do
  38. local tEv = {computer.pullSignal(nTimeout)}
  39. for k,v in pairs(tTasks) do
  40. if coroutine.status(v.c) ~= "dead" then
  41. cPid = k
  42. coroutine.resume(v.c,table.unpack(tEv))
  43. else
  44. tTasks[k] = nil
  45. end
  46. end
  47. end
  48. end
  49. function os.setenv(k,v) -- set's the current process' environment variable *k* to *v*, which is passed to children
  50. if tTasks[cPid] then
  51. tTasks[cPid].e[k] = v
  52. end
  53. end
  54. function os.getenv(k) -- gets a process' *k* environment variable
  55. if tTasks[cPid] then
  56. return tTasks[cPid].e[k]
  57. end
  58. end
  59. end