Page 1 of 1

Multitasking in C

Posted: Thu Mar 27, 2008 12:30 pm
by 01000101
hi, I just implemented some multitasking in my OS using the tutorial from http://code.google.com/p/onyxkernel/wik ... ltitasking.

I can switch inbetween tasks, but sometimes (usually) the first task (PID 0) does not execute for some reason, and provides no error or exception for me to find in bochs. Also, the outcome of the multitasking varies greatly on whether i am running in bochs or on real hardware.

I implemented a small scheduler function that prioritized the processes, but nothing runs at all.

It seems as if something very minute is off in the stack or something like that, as it is not a constant problem, just usually.

Also, I substitued the kmalloc_a for just kmalloc as I could not find the prototype or OSS function for it.

Posted: Thu Mar 27, 2008 12:35 pm
by JamesM
Could kmalloc_a be a version of kmalloc that returns a page aligned address? That's what it is in my OS, anyway and if required it seems likely it might break something if substituted for the normal version.

Posted: Thu Mar 27, 2008 1:03 pm
by 01000101
I don't use paging atm.

im going to run a few more tests and then ill post back

Posted: Thu Mar 27, 2008 1:06 pm
by xyzzy
You can have stacks not page-aligned but they really should be for performance reasons. The official POSIX name for such a function (kmalloc_a) is memalign.

Posted: Thu Mar 27, 2008 2:07 pm
by t0xic
Woah! Thanks for the plug to my site. Can't believe someone used it :D

The task setup in 0 should be an idle task. It doesn't seem to execute because it doesn't really execute. It copies the current kernel stack to task 0's stack so that you can continue to run the currently running task (kernel before multitasking is enabled)

It's this line that causes this when the scheduler is run for the first time.

Code: Select all

tasks[pid].stack = context;      // save the old context into current task
And yes, kmalloc_a is page boundary aligned. It shouldn't really matter though.

Posted: Thu Mar 27, 2008 2:13 pm
by 01000101
nice tutorial btw

yeah, I finally got that part (the null process), and am currently using it as a process daemon to manage (start/stop) other threads. I still don't know why every time I mess with (even test) the task structure items that the task doesnt work after.

t0xic, you should deffinately add more text to your tutorial going more indepth of why you did certain things, there is alot of very useful code, but with very little explanation as to why it is there.

Posted: Thu Mar 27, 2008 2:15 pm
by t0xic
Ok, I'll add some more code in the near-future (busy this week).

edit: Thanks!
would you like more line-by-line comments/explanations, or more in the code then explanation style?

Posted: Thu Mar 27, 2008 4:05 pm
by 01000101
I think a more structured approach would be more beneficial, I was thinking more along the lines of

Code: Select all

(code-block explanation -> code-block -> review -> repeat
if you gave an 'intro' to the code-block explaining what it does through abstraction that would be great as well as a trailing wrap-up explaining how it ties in with the rest of the code blocks.

I fixed the issues I was having and now I am having a performance issue with my two RTL8139D's as processes, but I have a feeling it is my polling function that is at fault.

Pe@cE

Posted: Thu Mar 27, 2008 4:28 pm
by t0xic
Ok, thanks for the suggestions. If you want more stability in your processes, you could always write code to pause the scheduler until parts of the code complete.