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.
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Software Multitasking

Post 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
User avatar
astrocrep
Member
Member
Posts: 127
Joined: Sat Apr 21, 2007 7:21 pm

Post 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
Mouse Pad - Coming in the distant future...
Kernel: Indigo Kernel - v0.0.1

Thanks to JamesM and BrokenThorn for there tutorials!
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

Thanks for the quick reply!
I am working on implementing the start_task() now!

Michael
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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!
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post 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
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Post 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)
~ Lukem95 [ Cake ]
Release: 0.08b
Image
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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
User avatar
astrocrep
Member
Member
Posts: 127
Joined: Sat Apr 21, 2007 7:21 pm

Post 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
Mouse Pad - Coming in the distant future...
Kernel: Indigo Kernel - v0.0.1

Thanks to JamesM and BrokenThorn for there tutorials!
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post 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
User avatar
razor-x
Posts: 6
Joined: Fri Jan 26, 2007 12:53 pm
Location: 's-Gravendeel - The Netherlands

Post 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! :)
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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
User avatar
razor-x
Posts: 6
Joined: Fri Jan 26, 2007 12:53 pm
Location: 's-Gravendeel - The Netherlands

Post by razor-x »

Why didn't I think of that? ;)

Thanks, I'm going to try it out!
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

Razor, thanks for asking that question!
User avatar
razor-x
Posts: 6
Joined: Fri Jan 26, 2007 12:53 pm
Location: 's-Gravendeel - The Netherlands

Post 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 ;)
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Post 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
~ Lukem95 [ Cake ]
Release: 0.08b
Image
Post Reply