Operating system for OpenComputers
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

69 lignes
1.8KB

  1. do
  2. local tTasks,nPid,nTimeout,cPid = {},1,0.25,0 -- table of tasks, next process ID, event timeout, current PID
  3. function os.spawn(f,n) -- function string -- number -- 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) -- number -- -- removes process *pid* from the task list
  20. tTasks[pid] = nil
  21. end
  22. function os.pid() -- -- number -- returns the current process' PID
  23. return cPid
  24. end
  25. function os.tasks() -- -- table -- returns a table of process IDs
  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) -- number -- table -- returns info on process *pid* as a table with name and parent values
  33. pid = pid or os.pid()
  34. if not tTasks[pid] then return false end
  35. return {name=tTasks[pid].n,parent=tTasks[pid].P}
  36. end
  37. function os.sched() -- the actual scheduler function
  38. os.sched = nil
  39. while #tTasks > 0 do
  40. local tEv = {computer.pullSignal(nTimeout)}
  41. for k,v in pairs(tTasks) do
  42. if coroutine.status(v.c) ~= "dead" then
  43. cPid = k
  44. coroutine.resume(v.c,table.unpack(tEv))
  45. else
  46. tTasks[k] = nil
  47. end
  48. end
  49. end
  50. end
  51. function os.setenv(k,v) -- set's the current process' environment variable *k* to *v*, which is passed to children
  52. if tTasks[cPid] then
  53. tTasks[cPid].e[k] = v
  54. end
  55. end
  56. function os.getenv(k) -- gets a process' *k* environment variable
  57. if tTasks[cPid] then
  58. return tTasks[cPid].e[k]
  59. end
  60. end
  61. function os.setTimeout(n)
  62. if type(n) == "number" and n >= 0 then
  63. nTimeout = n
  64. return true
  65. end
  66. return false
  67. end
  68. end