Multitasking in C

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
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Multitasking in C

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

Post 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.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post by 01000101 »

I don't use paging atm.

im going to run a few more tests and then ill post back
xyzzy
Member
Member
Posts: 391
Joined: Wed Jul 25, 2007 8:45 am
Libera.chat IRC: aejsmith
Location: London, UK
Contact:

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

Post 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.
Last edited by t0xic on Thu Mar 27, 2008 2:14 pm, edited 1 time in total.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

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

Post 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?
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

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

Post 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.
Post Reply