how 2 start multitasking?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
viral

how 2 start multitasking?

Post by viral »

Hi..
I am working on an 32 - bit OS in c++. My os got:
1. Setup GDT.
2. IDT.
3. Video driver.
3. Keyboard driver.
Now I want to start multitasking, I have read many tuts but i guess they dint help me. My questions are:
1. How to start a task?
2. How to store info about all task?
3. How to switch them by just software side? (no tss plz i am a kidd..)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:how 2 start multitasking?

Post by Pype.Clicker »

How to store info about all task?
- the "ancient unix way": have a table of N "tasks control block" and sweep that table to see what's the next task to be run (each control block should at least have a 'state' information telling if it's ready, running, sleeping, zombie, etc.)

- the "more modern way": have a set (e.g. a binary tree) of threads for each process so that you can kill all of them when requested to kill the process and find one thread efficiently; have a set/table of processes so that you can list them all ; have a queue of "ready" thread in the scheduler structure.

If that sounds totally dark-side to you, i suggest you read deeper about dynamic structures like AVL trees, hash tables and linked lists first.
How to switch them just by software ?
http://www.osdev.org/osfaq2/index.php/C ... 0Switching should carry all the information you need. Basically you have
- to convince you that you can keep all the information you need on your stack
- have a single function that will pick the current stack pointer, move it to the current task control block and then load the stack pointer of the new task control block into the cpu
- convince yourself that, by the previous operation, you've now entered a new task state and that letting the CPU go will gently resume suspended operations.
- check that your implementation is "irq-safe" (e.g. the IRQs are re-enabled iff an IRQ caused the switch)
how do i start a task ?
The nicest way is to prepare a new Thread Control Block and a corresponding stack so that the task switcher code will not even notice that the stack is new.
You can do this by faking "what would happen if there were a call for switch right before the first instruction of my new task is executed".
viral

Re:how 2 start multitasking?

Post by viral »

Really thanks 4 that...
I am learning at a snail-speed. Can u please better explain in other word (if possible). I am trying to make things work. But still not gettin what to do now... can u plz give me steps to impliment?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:how 2 start multitasking?

Post by Pype.Clicker »

i'm abroad for now, so i'll leave that to someone else. BI tutorial (on distantvoices) should give you those steps, though.
viral

Re:how 2 start multitasking?

Post by viral »

Thanks for the reply...
Can anyone really know:
* How to start a task?
* Where the Stack of all task lies?
Kemp

Re:how 2 start multitasking?

Post by Kemp »

If by stack you mean the stack the tasks use, then usually they'll be given a stack each that you can stick anywhere you wnat within their memory space. If you mean the list of tasks running, then that's up to you to keep track of, I personally use a series of linked lists.
viral

Re:how 2 start multitasking?

Post by viral »

So that means I have to assign Stack for every Process by myself (If I am wrong then plz do correct me). I had gone through the tutorial by BlueIllusionOS at http://www.distantvoices.org/html/multitasking.html

I dint get some terms..
1. What is &kstack[d][KSTACKTOP] ?
AR

Re:how 2 start multitasking?

Post by AR »

kstack appears to be an array of pointers to kernel stacks, each task has its own kernel stack, what he's doing is getting a pointer to the top of the task's stack.
viral

Re:how 2 start multitasking?

Post by viral »

Thanks for reply..
I am still trying to work it out....
Poseidon

Re:how 2 start multitasking?

Post by Poseidon »

maybe a look at www.codezen.org/viridis helps... it made a lot of things clear to me (both the whitepapers and the source code).
Warrior

Re:how 2 start multitasking?

Post by Warrior »

Thx Poseidon, a lot of good articles there.
viral

Re:how 2 start multitasking?

Post by viral »

Thnx a lot....
wonderfull links..............
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:how 2 start multitasking?

Post by Neo »

Pype.Clicker wrote: - the "more modern way": have a set (e.g. a binary tree) of threads for each process so that you can kill all of them when requested to kill the process and find one thread efficiently; have a set/table of processes so that you can list them all ; have a queue of "ready" thread in the scheduler structure.
.............
@Pype: One thing that I wanted to know.
Did you mean a tree in general or binary trees in particular?

I mean if a parent has more than 2 children processes then ....?
Only Human
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:how 2 start multitasking?

Post by Pype.Clicker »

well, it could be whatever tree you want provided that
- it's fast to search
- it's quick to adjust for insertions/deletions
- it has fair memory usage.

Most people pick binary trees for this because there are plenty of well-known implementations with provable performances, but you could go for B+tree if you prefer ...
viral

Re:how 2 start multitasking?

Post by viral »

Ya right..
binary trees are easy to implement.. but then a parent can have atmost 2 childs .. if we restrict to binary trees
Post Reply