Page 1 of 3
Software Multitasking
Posted: Thu Jan 17, 2008 8:29 pm
by t0xic
Hey everyone!
I have finally ported JamesM's multitasking code to my kernel. I can call fork() and create new tasks, but they all have the same EIP. If I want them each to start a new function, i.e. function1(), function2(), and function3(), do I just set the EIP value in the task structure to the location of the function and then switch to the task?
Thanks,
Michael
Posted: Thu Jan 17, 2008 8:37 pm
by astrocrep
I to am quiet firmilar w/ his multi-tasking code...
Yes...
If you want to copy the fork code...
call it "start_task" and have it take a void *thread... then just assign the value of thread to the eip.
I don't have the exact syntax cause I am not at my home pc... but thats how ya do it, and it works for me!
Good Luck
-Rich
Posted: Thu Jan 17, 2008 8:40 pm
by t0xic
Thanks for the quick reply!
I am working on implementing the start_task() now!
Michael
Posted: Fri Jan 18, 2008 2:36 am
by JamesM
Fork() returns zero if you are the child process, and nonzero (The PID of the child process) if you are the parent. So, a start_task function could read something like:
Code: Select all
void start_task(void (*func)(void*), void *arg)
{
if (fork() == 0)
func(arg);
}
Pretty simple, really.
EDIT: I really should get round to writing the documentation for the multitasking code
I just haven't had time so far!
Posted: Fri Jan 18, 2008 7:00 am
by t0xic
I was changing the eip in the task structure. so I just need to do a function call? That's easier than I thought!
Thanks for the code anyhow. You documented it wonderfully, and it was very easy to port (just a few debugging errors in my kernel).
Thanks,
Michael
Posted: Fri Jan 18, 2008 7:13 am
by lukem95
i havnt seen this documentation/code, can somebody post a link to it?
i'm hoping to implement software multitasking as soon as i get binaries loading correctly (fingers-x'd by sunday night)
Posted: Fri Jan 18, 2008 7:29 am
by JamesM
I haven't released it to the public as yet, I was waiting until I'd written the accompanying tutorial. But *sigh* if you want it...
http://www.jamesmolloy.co.uk/downloads/ ... ing.tar.gz
Cheers,
JamesM
Posted: Fri Jan 18, 2008 8:39 am
by astrocrep
JamesM wrote:Fork() returns zero if you are the child process, and nonzero (The PID of the child process) if you are the parent. So, a start_task function could read something like:
Code: Select all
void start_task(void (*func)(void*), void *arg)
{
if (fork() == 0)
func(arg);
}
Pretty simple, really.
EDIT: I really should get round to writing the documentation for the multitasking code
I just haven't had time so far!
Wow... thats really a good idea... then you could just add the code to remove it from the scheduler right at the bottom... when ever func(arg) returns...
-Rich
Posted: Fri Jan 18, 2008 8:47 am
by t0xic
Yep,
Well right now, I'm not using the IRQ0/PIT scheduler. I just want to make sure that it will work. Whenever I call switch_task(), I get a page fault, so i still trying to debug this. I would post my ported code or bochs dump, but I'm not at home right now.
[Edit]: I already had to change some of the init_task() and fork() code because I was getting a page fault accessing the task structure. So I just identity mapped the structure with 0x7. [/Edit]
--Michael
Posted: Sun Jan 20, 2008 4:44 am
by razor-x
astrocrep wrote:JamesM wrote:Fork() returns zero if you are the child process, and nonzero (The PID of the child process) if you are the parent. So, a start_task function could read something like:
Code: Select all
void start_task(void (*func)(void*), void *arg)
{
if (fork() == 0)
func(arg);
}
Pretty simple, really.
EDIT: I really should get round to writing the documentation for the multitasking code
I just haven't had time so far!
Wow... thats really a good idea... then you could just add the code to remove it from the scheduler right at the bottom... when ever func(arg) returns...
-Rich
I used this method now and it seems to be working
If I create 3 seperate tasks, and place a loop in each task everything works ok, but when a task returns or finishes I get a page fault. Is this because the task isn't running anymore (and so, doesn't exisit?) and the scheduler wants switch to that task?
This is my task list:
Code: Select all
PID ESP EBP EIP PAGE
1 0x00000000 0x00000000 0x00000000 0x008100
2 0xdfffff74 0xdfffff9c 0x001000dc 0x008800
3 0xdfffff74 0xdfffff9c 0x00100106 0x008f00
but I get this page fault;
Code: Select all
Page fault! ( present read-only ) at 0x0x12bd890 - EIP: 0xc00830b
I don't understand this... why do I get page fault at this addres and EIP? It isnt the EIP of the task that just returned.. (If i place a loop at this task, everything is ok.)
How can I see if a task isn't running anymore?
(yes you're right.. I'm still a beginner, but I'm learning!
)
btw. Thanks for the great tutorials JamesM, I'm really learning alot from it!
Posted: Sun Jan 20, 2008 5:55 am
by JamesM
Hi,
That return address seems to be garbage, and so i reckon your task is finishing, returning, then the start_task function returns, and pops a garbage return address off the stack. Modify your function to look something like
Code: Select all
void start_task(void (*func)(void*), void *arg)
{
if (fork() == 0)
{
func(arg);
kill_task();
for(;;);
}
}
That way you can have a function called kill_task that removes th current task from the ready queue. the for(;;); afterwards is so the task spins at the current location until the scheduler runs, so it doesn't return to a garbage stack.
Have fun!
James
Posted: Sun Jan 20, 2008 6:49 am
by razor-x
Why didn't I think of that?
Thanks, I'm going to try it out!
Posted: Sun Jan 20, 2008 9:17 am
by t0xic
Razor, thanks for asking that question!
Posted: Sun Jan 20, 2008 10:34 am
by razor-x
t0xic wrote:Razor, thanks for asking that question!
np!
I still haven't got it right...
But I'll get there.... I hope
Posted: Tue Jan 22, 2008 4:39 pm
by lukem95
that looks like some mighty fine code you got there James, very portable, and easy to plug in your own scheduler.
can i ask how (if you've started) far you are into the documentation for it? i spent a good few hours exploring it and gave implementing it a go, however i have one tiny problem that may be either a bug (not likely) or a problem in my code.
anyway, when i set up current_directory and try to switch to it using the function defined in your paging code (and updated to have dir->physicalAddr), the computer just hangs. Does anyone know why this might be?
Again, very nice code though, im enjoying working with it immensly. looking forward to some documentation/companion tutorial.
Lukem