software 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.
Post Reply
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

software multitasking

Post by xyjamepa »

Hi,
a month ago I tried to implement a hardware multitasking
but I found it hard, complex and slow so I decided
to implement software multitasking but I have no idea
about it so I think I need some explaint about software
multitasking and how it goes with paging
and how can I implement it(I mean here software multitasking),also
any links or tutorials are welcome.
my os sure 32bit protected mode and I'm using DJGPP
Thanx.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

Go here : http://alexfru.narod.ru/
Get "os.zip" its got a simple example coded with DJGPP
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I cheated a bit in my soft multitasker... I basically have a resched function only called by the timer interrupt, which has all the registers, flags and segments in a convenient structure. That's passed to the resched function, which saves the old process data and loads the new data. When it returns, the timer interrupt continues processing and once that's done, the timer interrupt effectively returns to the next process.
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post by iammisc »

most operating systems store a structure with a pointer to the stack for the process and the page directory. To switch tasks, you switch page directories(make sure that your kernel is mapped to every task!), and then you change esp to the new process's stack. Then you pop off all the registers from the new stack. Because your stack contains the return address whatever called the task switching function, on your next ret, control of the processor will be given back to the old process's state.

You will have to create a function that sets up the initial stack properly for each new task.

If you have a microkernel and want your task switching to go a bit faster and make the switcher easier to implement, you could just store all the registers and the cs:eip inside the process structure then to switch processes you would set the new page directory, restore the cpu state, and jmp to the old cs:eip.

While the microkernel one is easier to implement, it also has more liability to not work which is why i recommend for you to follow the first one.
Crazed123
Member
Member
Posts: 248
Joined: Thu Oct 21, 2004 11:00 pm

Post by Crazed123 »

iammisc wrote:If you have a microkernel and want your task switching to go a bit faster and make the switcher easier to implement, you could just store all the registers and the cs:eip inside the process structure then to switch processes you would set the new page directory, restore the cpu state, and jmp to the old cs:eip.

While the microkernel one is easier to implement, it also has more liability to not work which is why i recommend for you to follow the first one.
Why should that technique be particular to microkernels?
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post by iammisc »

That trick is limited for microkernels because it is only should be used for fast-switching contexts not really for a full-fledged multitasking thing.

And plus the wiki says so... :wink:
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

iammisc wrote:And plus the wiki says so... :wink:
Where? Where? i'll kill that article :shock:

Really, the concept of software task switching is independent of kernel style. You can use either method for both types of kernels. The only essential difference is the use of PUSH/POP register instead of MOV register. (which you can even evolve in the other: you could temporarily point esp at the desired structure and push/pop, or you can load the registers from [ESP+...])
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Crazed123
Member
Member
Posts: 248
Joined: Thu Oct 21, 2004 11:00 pm

Post by Crazed123 »

Actually, now that I think of it, isn't jumping to a code segment with a higher DPL than the CPL illegal?
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

Crazed123 wrote:Actually, now that I think of it, isn't jumping to a code segment with a higher DPL than the CPL illegal?
No, not if you initialize the stack/heap first.
My OS is Perception.
Crazed123
Member
Member
Posts: 248
Joined: Thu Oct 21, 2004 11:00 pm

Post by Crazed123 »

Really? How so?
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post by iammisc »

Where? Where? i'll kill that article *shocked*
woops. my bad,i was thinking of this article on osdever.net http://osdever.net/tutorials/soft_ts.php
Post Reply