Operating system for OpenComputers
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

40 satır
1.1KB

  1. do
  2. 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] = {["c"]=coroutine.create(f),["n"]=n,["p"]=nPid,e={}}
  5. if tTasks[cPid] then
  6. for k,v in pairs(tTasks[cPid].e) do
  7. tTasks[nPid].e[k] = tTasks[nPid].e[k] or v
  8. end
  9. end
  10. nPid = nPid + 1
  11. return nPid - 1
  12. end
  13. function os.kill(pid) -- removes process *pid* from the task list
  14. tTasks[pid] = nil
  15. end
  16. function sched() -- the actual scheduler function
  17. while #tTasks > 0 do
  18. local tEv = {computer.pullSignal(nTimeout)}
  19. for k,v in pairs(tTasks) do
  20. if coroutine.status(v.c) ~= "dead" then
  21. cPid = k
  22. coroutine.resume(v.c,table.unpack(tEv))
  23. else
  24. tTasks[k] = nil
  25. end
  26. end
  27. end
  28. end
  29. function os.setenv(k,v) -- set's the current process' environment variable *k* to *v*, which is passed to children
  30. if tTasks[cPid] then
  31. tTasks[cPid].e[k] = v
  32. end
  33. end
  34. function os.getenv(k) -- gets a process' *k* environment variable
  35. if tTasks[cPid] then
  36. return tTasks[cPid].e[k]
  37. end
  38. end
  39. end