This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Voidfury's profile picture

Master program (OS) running sub-programs

Started by Voidfury, 27 July 2015 - 05:07 AM
Voidfury #1
Posted 27 July 2015 - 07:07 AM
I'm looking to start making an operating system, and I'm planning out a lot of the things I want, and I am looking over my broad idea again, and realizing how flawed it is:
I am planning on having the operating system resume coroutines which will update individual programs, but the problem would be that this approach heavily necessitates each sub-program to yield in each loop.

I was wondering if anyone had any recommendations to replace this scheme so that the operating system actually serves as the master, and doesn't depend on each program to work perfectly.

Here's a brief example of how the current plan would work:


--Define the different programs as coroutines, which could be turned on by the master
c1 = coroutine.wrap(
   --Program 1
   while true do
	  doUpdateStuff()
	  coroutine.yield()   --If this line is missing or for whatever reason it is not called, worst case scenario is this program seizes control of the entire computer
   end
)
c2 = coroutine.wrap(
   --Program 2
   while true do
	  doUpdateStuff()
	  coroutine.yield()   --
   end
)
c3 = coroutine.wrap(
   --Program 3
   while true do
	  doUpdateStuff()
	  coroutine.yield()   --
   end
)

--Master/OS part:
while true do
   coroutine.resume(c1)
   coroutine.resume(c2)
   coroutine.resume(c3)
   UpdateEverythingWithAllProgramsConsidered()
end

(If I've made any mistakes in syntax or setup, don't worry, I am really only concerned with big picture right now)
Edited on 27 July 2015 - 03:43 PM
Lyqyd #2
Posted 27 July 2015 - 05:46 PM
The problem you describe isn't actually a problem. The issue here is that you are not familiar with the ComputerCraft coroutine and event model. Try reading through the parallel API for an example of a ComputerCraft coroutine manager.
Voidfury #3
Posted 27 July 2015 - 07:10 PM
Alright, I'll take another look at the parallel api, coroutines in Lua, and related wiki pages.