Multi-tasking
Started by logsys, Jul 15 2014 11:19 AM
12 replies to this topic
#1
Posted 15 July 2014 - 11:19 AM
Lately, I have been trying to implement multitasking into my OS, but no success at all.. My problem is, how do I terminate a coroutine and how do I run multiple coroutines? As far as I could tell, a coroutine gets into a "standby" mode when that coroutine runs another. Can someone do a detailed tutorial for me? I always get confused with it
#3
Posted 15 July 2014 - 08:46 PM
kinda left me confused, but im getting the handle of it.. now how do I kill a coroutine? like my program doesnt have coroutine.yield neither it ended and I want to kill it
#4
Posted 15 July 2014 - 10:04 PM
I think you misunderstand coroutines.
Coroutines are a way to run a function, save its state, give it it's own events, and resume it later. The "own events" means that every coroutine gets its own copy of all the events.
You are probably asking for "How can the parent program terminate one of its coroutines?". That's impossible.
Let's think of a program that uses "sleep" somewhere. After the program has called the sleep function, that program pauses until the sleep ends (returns). There's no way for the program to stop that sleep. It's the same with coroutines, specifically with coroutine.resume.
---
Remember, os.pullEvent and functions that use os.pullEvent like sleep, read, turtle movement, slowprint, ... ALL YIELD. If there's no such function in your program it will error: "to long without yielding", so there's probably one in your coroutine.
Coroutines are a way to run a function, save its state, give it it's own events, and resume it later. The "own events" means that every coroutine gets its own copy of all the events.
You are probably asking for "How can the parent program terminate one of its coroutines?". That's impossible.
Let's think of a program that uses "sleep" somewhere. After the program has called the sleep function, that program pauses until the sleep ends (returns). There's no way for the program to stop that sleep. It's the same with coroutines, specifically with coroutine.resume.
---
Remember, os.pullEvent and functions that use os.pullEvent like sleep, read, turtle movement, slowprint, ... ALL YIELD. If there's no such function in your program it will error: "to long without yielding", so there's probably one in your coroutine.
Edited by flaghacker, 15 July 2014 - 10:08 PM.
#5
Posted 15 July 2014 - 11:32 PM
It's also worth noting that Lua can only do one thing at once. There's no multithreading, parallel makes it look like there is, but there isn't.
The best way in my opinion is to look at APIs or OSs that have the functionality that you want.
The best way in my opinion is to look at APIs or OSs that have the functionality that you want.
#6
Posted 16 July 2014 - 08:11 AM
oeed, on 15 July 2014 - 11:32 PM, said:
It's also worth noting that Lua can only do one thing at once. There's no multithreading, parallel makes it look like there is, but there isn't.
The best way in my opinion is to look at APIs or OSs that have the functionality that you want.
The best way in my opinion is to look at APIs or OSs that have the functionality that you want.
Isn't that also a property of CPU's itself? That just goes so fast you can't notice it. Or isn't that the case anymore with modern CPU's?
#7
Posted 16 July 2014 - 08:26 AM
Modern CPUs generally have multiple physical cores - for example, a basic 3ghz dual-core system has two cores, each of which can run instructions at a rate of up to 3ghz. By spreading the load between them, they can literally process instructions in true parallel.
You might remember about ten years ago (and earlier), where if a program on your computer went haywire, it'd chew up all your system resources - meaning that the computer struggled just to get the task manager open so you could terminate the problematic process.
All that mostly went away with the rise of multi-core CPUs. This is because most programmers don't bother to write multi-threaded code, so their programs can only run on one core at a time - meaning that even if their code DOES go haywire, the other core is still free to do whatever else it is the user wants to do. In short, these CPUs are great for running multiple processor-intensive programs at the same time, but for the average user you'll never really want more than a dual.
A negative side effect is that users don't know there's a problem when a program suddenly decides to "consume" a core. Or rather, they maybe notice that the system's heating up and running its fans flat out, but since performance isn't affected they don't care.
You might remember about ten years ago (and earlier), where if a program on your computer went haywire, it'd chew up all your system resources - meaning that the computer struggled just to get the task manager open so you could terminate the problematic process.
All that mostly went away with the rise of multi-core CPUs. This is because most programmers don't bother to write multi-threaded code, so their programs can only run on one core at a time - meaning that even if their code DOES go haywire, the other core is still free to do whatever else it is the user wants to do. In short, these CPUs are great for running multiple processor-intensive programs at the same time, but for the average user you'll never really want more than a dual.
A negative side effect is that users don't know there's a problem when a program suddenly decides to "consume" a core. Or rather, they maybe notice that the system's heating up and running its fans flat out, but since performance isn't affected they don't care.
#8
Posted 16 July 2014 - 08:26 AM
flaghacker, on 16 July 2014 - 08:11 AM, said:
oeed, on 15 July 2014 - 11:32 PM, said:
It's also worth noting that Lua can only do one thing at once. There's no multithreading, parallel makes it look like there is, but there isn't.
The best way in my opinion is to look at APIs or OSs that have the functionality that you want.
The best way in my opinion is to look at APIs or OSs that have the functionality that you want.
Isn't that also a property of CPU's itself? That just goes so fast you can't notice it. Or isn't that the case anymore with modern CPU's?
#9
Posted 17 July 2014 - 06:36 PM
ok, so what ive seen from that is that coroutines cant be killed. In an normal program, without OSes, it returns "Terminated" when CTRL+T is pressed, but in normal programs they yield even if for a bit? If yes, how can I prevent it from resuming? if the program yields, in a sleep or something, will my program resumes? if yes, can I simply make it disapear with a process = nil?
Edited by logsys, 17 July 2014 - 06:36 PM.
#10
Posted 17 July 2014 - 06:50 PM
Assuming you're implementing coroutines yourself and not using the parallel api; yes, once the running program yields, control will fall back to your program, in turn you must yield, and then when you resume the program you must provide it with the event data you received (taking into consideration it's filter if it has one). As for the killing coroutines, it isn't possible. Simply remove the reference — for example routine = nil — and it will remain in a suspended status until the garbage collector comes through and destroys it (which in Minecraft is every game loop *shudders*).
I really suggest that you have a read through Bubba's# coroutine tutorial if you haven't already. As well as taking a look at how the Parallel API works.
# a.k.a. AliquotMesozoic and ChallengeMe
I really suggest that you have a read through Bubba's# coroutine tutorial if you haven't already. As well as taking a look at how the Parallel API works.
# a.k.a. AliquotMesozoic and ChallengeMe
#11
Posted 17 July 2014 - 08:32 PM
Thank you all for your support! +1 for all of you!
#12
Posted 18 July 2014 - 02:07 PM
As this is still open.. I will post here a question.. im since 4 hours trying to solve this error.. I have this code(code 1) to manage the coroutines.. It has worked fine when I used the code 2 that is similar to that one.. When I try to open my file browser inside LogOS, doesnt do anything.. and if I terminate with CTRL+T or the T button on the ccemuredux, it errors inside the program.. I hope you guys have time to debug my code and help me finding the error. I tried outside the OS, but it went far badly..
Here are the codes:
Here are the codes:
Code 1
Code 2
#13
Posted 18 July 2014 - 03:33 PM
logsys, on 18 July 2014 - 02:07 PM, said:
As this is still open.. I will post here a question.. im since 4 hours trying to solve this error.. I have this code(code 1) to manage the coroutines.. It has worked fine when I used the code 2 that is similar to that one.. When I try to open my file browser inside LogOS, doesnt do anything.. and if I terminate with CTRL+T or the T button on the ccemuredux, it errors inside the program.. I hope you guys have time to debug my code and help me finding the error. I tried outside the OS, but it went far badly..
Here are the codes:
Here are the codes:
Code 1
Code 2
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users











