Page 1 of 2

Multitasking Tutorial

Posted: Sun Oct 16, 2005 5:36 am
by Cjmovie
Well, I decided that multitasking is fresh in my head. So I wrote a tutorial on it. I'd like suggestions, criticism, etc. right now before I submit it anywhere. Also I'm not 100% on that exact code working, as I haven't had a chance to test it, but it should work. I checked it to the intel manuals twice :).

http://hosted.cjmovie.net/TutMultitask.htm

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 7:49 am
by falconfx
Very good tutorial, Cjmovie! ;)

It is the simplest multi-tasking tutorial I've ever read.

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 11:07 am
by proxy
very nice, i wish i had something like this when i was trying to figure out stack swapping. Seems you took the advice given in here to you and really took it in and tried to understand it, well done.

only one comment (not an error just an optimization)

instead of

Code: Select all

  if(CurrentTask == 0)CurrentTask = 1;
  else CurrentTask = 0;
you could just do:

Code: Select all

currentTask = (currentTask + 1) % 2;
where 2 is the number of tasks you have, this way your round robin will scale nicely to having more threads. Also, next thing you need to figure out is how to block a thread ;)

it's really not hard, you just need to mark it as "non-runnable" somehow and make your taskSwitch function skip over any threads which are not-runnable until it find a runnable one...falling back on an idle thread if none found of course.

good job and good luck.

proxy

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 11:24 am
by Warrior
Nice job, this should come in handy. Never seen such a detailed explination.

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 12:07 pm
by zloba
and make your taskSwitch function skip over any threads which are not-runnable until it find a runnable one...
or move them to a separate "blocked" list.

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 2:07 pm
by Cjmovie
As I said, it was intended to be super-simple. However, now that I see the other little things I could add, it seems dumb NOT to add them.
IDK, Maybe I will add an explanation for keeping track of entire processes and not just threads, different scheduling methods, or even some other things like preparing the kernel for multitasking (Try calling the same kpritnf function in two threads at once - Not pretty!)

So I do have some things to add. But really, nobody had any trouble with it? Maybe I should get somebody who doesn't know what's going on to read it :).

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 2:26 pm
by Pype.Clicker
you don't really need a "cli" in your interrupt handler, right ? ...
... or do you ?

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 3:51 pm
by Cjmovie
Actually we don't, but in most tutorials (which I've found people usually just copy and paste code....) it has the 'cli', and I figured, it won't hurt anything. Better KISS. :)

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 5:41 pm
by zloba
Here are some suggestions: :) (no offence - I am a lot better at bitching about other people's writing than at writing my own)
To use the method I try to describe here, which is among the simplest,
How about naming the method, and providing a brief description of it, so the reader can see what is coming up without reading the whole document, and decide if this is what they want, or they already know that, etc. It also helps to remember things when you come back. (an "abstract" of the document)

Protected Mode isn't mentioned, maybe add it to the prerequisites (there are real-mode "Hello, World" OSes out there) I wasn't sure if you're using it or not from the first glance.

Maybe you can state your assumptions about the reader's knowledge?

You might want to explain what flavor of multitasking _you_ describe, its properties, advantages and limitations, alternatives, etc. Here's an actual question from a newbie (me):
You should now have enough understanding to bring your OS from the DOS age of mono tasking...to the modern age of multi-tasking, multi-threaded operating systems!
Will this method work with privileges and separate address spaces, and why?
---
small, irrelevant details:
Even the CLI (Command Line Interpreter) based linux (using bash, or the like)
1. linux is not CLI-based, CLI may be linux-based. You can start X11 at any time, if you have it.
2. Command-Line Interface, maybe? (as opposed to GUI) "Interpreter" may be valid anyway.
is multitasking - You can switch between processes with F1, F2, F3 and F4 on debian, for instance.
I assume you mean Alt+F* - that switches consoles (terminals), not processes. You switch between processes (jobs) in shell using fg, bg and ctrl+z to move processes to and from background execution or suspension.

I'll shut up now.

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 6:21 pm
by Cjmovie
I'll shut up now.
Yes, that would make my life easier. But it would also make my life more boring, more useless, and less of a lot of other things. This is exactly why I posted - I was hoping for improvements.

I will do as such.

Re:Multitasking Tutorial

Posted: Sun Oct 16, 2005 11:55 pm
by distantvoices
You might as well have a look at my Multitasking text:

www.distantvoices.org/html/multitasking.html

Just compare, maybe we can come up with a good text.

Oh and btw - the blocked tasks: just don't insert them into a runable queue of any priority. I insert them to a blocked queue for the sake of having them collected at a handy place for statistics - but as well you can mark them blocked (which I'm doing also - for the sake of scrutinizng the tcb table.

stay safe. :-)

Re:Multitasking Tutorial

Posted: Tue Oct 18, 2005 2:43 pm
by TheChuckster
I don't know if this is an intentional challenge to the reader but you forgot to reset the master PIC by outportb(0x20,0x20).

Re:Multitasking Tutorial

Posted: Tue Oct 18, 2005 7:47 pm
by orloff
Very very nice multitasking tutorial, just what i was looking for.

Could you please give me some insight on the AllocPage() function?

Code: Select all

Threads[id].esp0 = AllocPage() + 4096;
Thanks again for the great tutorial, keep up the good work.

Re:Multitasking Tutorial

Posted: Tue Oct 18, 2005 8:01 pm
by Warrior
It simply gives out chunks of memory in 4KB and the 4096 is to get to the very end of it since the stack grows downward.

Re:Multitasking Tutorial

Posted: Wed Oct 19, 2005 2:45 pm
by Cjmovie
You might want to read this:
http://www.osdev.org/osfaq2/index.php/A ... g%20Memory
or maybe:
http://osdever.net/tutorials/memory1.php?the_id=44

If you don't understand AllocPage. As it says at the beginning, you MUST already have a memory manager to use the 'exact' code in the tutorial, sure you could 'fudge' a little, but It's not a good idea unless you know you'll always be running exactly 1 or exactly 5 tasks or something...