OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 3:38 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: How to make a fully preemptable kernel ?
PostPosted: Thu Aug 02, 2001 11:00 pm 
I want my kernel to be fully preemptable as QNX(as I know),
but I don't know how it is achieved !
By not using memory-variables ?

Is the following function preemptable ?

void test_kernel_function()
{
int i=100000;

while(i--);
}

..if not,how shoudl I chage it to be ?

What I expect from the above function is that,when it is
preempted it will set i=100000 so when the preemted process
gains back controll it will have an undefined value in i !

Am I completly on the wrong way ?

I would also like to discuss microkernels in detail
(general concepts,message-passing,memeory managment,etc..).
If you also feel so(and have patience for me),
drop me an email: [email protected]

thanks in advance


Top
  
 
 Post subject: RE:How to make a fully preemptable kernel ?
PostPosted: Thu Aug 02, 2001 11:00 pm 
>Is the following function preemptable ?
>
>void test_kernel_function() {
> int i=100000;
> while (i--);
>}

Yep.

>What I expect from the above function is that,when it is
>preempted it will set i=100000 so when the preemted process
>gains back controll it will have an undefined value in i !
>
>Am I completly on the wrong way ?

Usually (pretty much always :) in a multitasking
environment each application has it's own memory
space. No other application is able to alter the
memory of another application, therefore your 'i'
variable is perfectly safe.

Some problems with multitasking involve the kernel
itself: what if two applications call the same
function? For example, two separate programs
tell the OS to read a sector from the HD. If that
OS function uses the same buffer for each read,
and is interrupted in the middle, you've got a
problem... the wrong data may be sent to the wrong
program.

Also, consider the following:

Program A is waiting for a responce from Program B
Program B is waiting for a responce from Program A

Both programs are at a stale mate, waiting for
responces from each other. The two processes are
effectively hung.

>I would also like to discuss microkernels in detail
>(general concepts,message-passing,memeory managment,etc..).
>If you also feel so(and have patience for me),
>drop me an email: [email protected]

I've got some knowledge on that stuff... not a whole
load, that's why I'm writting an OS in the first place -
to learn more about it :) I might be able to help
you with some stuff, though.

j.weeks


Top
  
 
 Post subject: RE:How to make a fully preemptable kernel ?
PostPosted: Fri Aug 03, 2001 11:00 pm 
>On 2001-08-03 23:17:19, J. Weeks wrote:


>>Is the following function preemptable ?
>>
>>void test_kernel_function() {
>> int i=100000;
>> while (i--);
>>}
>
>Yep.
>

Hi j.weeks !


To make my question cleaner(for myself too :))
If the C compiler stores a variable on stack,not global,
(I think all locals are stored that way if not explicitly overridden)
it is enough for that ?



>>What I expect from the above function is that,when it is
>>preempted it will set i=100000 so when the preemted process
>>gains back controll it will have an undefined value in i !
>>
>>Am I completly on the wrong way ?
>
>Usually (pretty much always :) in a multitasking
>environment each application has it's own memory
>space. No other application is able to alter the
>memory of another application, therefore your 'i'
>variable is perfectly safe.
>

Yeah,that was clear !!!

But test_kernel_function() is a function in kernel that
can be called from any app at any time !!

>Some problems with multitasking involve the kernel
>itself: what if two applications call the same
>function? For example, two separate programs
>tell the OS to read a sector from the HD. If that
>OS function uses the same buffer for each read,
>and is interrupted in the middle, you've got a
>problem... the wrong data may be sent to the wrong
>program.
>
>Also, consider the following:
>
>Program A is waiting for a responce from Program B
>Program B is waiting for a responce from Program A
>
>Both programs are at a stale mate, waiting for
>responces from each other. The two processes are
>effectively hung.
>



Isn't is this theme is a matter of deadlocks ?
I don't see relation to preemption !!




>>I would also like to discuss microkernels in detail
>>(general concepts,message-passing,memeory managment,etc..).
>>If you also feel so(and have patience for me),
>>drop me an email: [email protected]
>
>I've got some knowledge on that stuff... not a whole
>load, that's why I'm writting an OS in the first place -
>to learn more about it :) I might be able to help
>you with some stuff, though.
>

Is there a way to meet you more interactivly ?

>j.weeks


Top
  
 
 Post subject: RE:How to make a fully preemptable kernel ?
PostPosted: Sun Aug 05, 2001 11:00 pm 
>But test_kernel_function() is a function in kernel that
>can be called from any app at any time !!

Uhm... well... I said it could be pre-empted by any
task at any time.

It can be called from any app at any time _IF_ there's
only one instance of it currently running.

If there are two instances of this function running,
they may both be sharing the same 'i' variable address
space, and therefore may corrupt each other.

>Isn't is this theme is a matter of deadlocks ?
>I don't see relation to preemption !!

When you're talking about "preemption", you're talking
about pre-emptive multitasking, and these things must be
considered. These aren't issues with co-operative
multitasking.

>Is there a way to meet you more interactivly ?

Unfortunately, not really. Even if there were,
I'm at work 90% of the day :)

j.weeks


Top
  
 
 Post subject: RE:How to make a fully preemptable kernel ?
PostPosted: Mon Aug 06, 2001 11:00 pm 
>On 2001-08-03 20:36:53, mudi wrote:
>I want my kernel to be fully preemptable as QNX(as I know),
>but I don't know how it is achieved !


1. Use non-static local variables where possible.

2. Protect non-preemptible parts of the kernel
with critical sections (pushf; cli ... popf;
single-processor system) or spinlocks
(SMP system).

3. Some libc functions (e.g. strtok(), or any
function that uses "errno") are not re-entrant.
For the kernel, avoid these functions, or use
non-ANSI re-entrant alternative functions. I
don't know how user tasks handle this using a
shared libc DLL -- does each task get a copy
of the libc .data and .bss?

>Is the following function preemptable ?
>
>void test_kernel_function()

Yes.

>What I expect from the above function is that,when it is
>preempted it will set i=100000 so when the preemted process
>gains back controll it will have an undefined value in i !

Local variables live on the stack, and each
task/thread has its own stack. Sometimes
the compiler puts variables in registers,
but these are saved when switching tasks.
Either way, this code is preemptible.

>I would also like to discuss microkernels in detail
>(general concepts,message-passing,memeory managment,etc..).

Microkernel requires inter-process communication
(IPC), monolithic kernel does not. So, the
microkernel has a bigger "ante" -- it is more
difficult to write. I have read the source code
to microkernels and monolithic kernels, and the
monolithic kernels are easier for me to understand.


Top
  
 
 Post subject: Re: RE:How to make a fully preemptable kernel ?
PostPosted: Tue Mar 24, 2009 10:42 pm 
Offline

Joined: Thu Dec 04, 2008 1:32 am
Posts: 2
Guest wrote:
>On 2001-08-03 20:36:53, mudi wrote:
3. Some libc functions (e.g. strtok(), or any
function that uses "errno") are not re-entrant.
For the kernel, avoid these functions, or use
non-ANSI re-entrant alternative functions. I
don't know how user tasks handle this using a
shared libc DLL -- does each task get a copy
of the libc .data and .bss?


Yes, the kernel should allocate a new area of memory for that process's global data. The code sections can be shared as they don't (usually) change. Self modifying code could pose a problem, but this can easily be rectified by setting the page as readonly.

The problem with errno, and strtok() are that they are not safe if the process itself is multi threaded. In such a case the threads could cause your memory corruption; e.g.:
  • Thread A attempts to open file 'foo', and succeeds.
  • Thread B attempts to open file 'baz' and fails.
  • Thread A checks errno, GetLastError(), whatever, and get's Thread B's error code, and thinks that it did not open 'foo' successfully.

This is a bit of a contrived case (you should be checking to see if you get an invalid handle from your open function first), but it should illustrate the point with the problems with those functions.


Top
 Profile  
 
 Post subject: Re: How to make a fully preemptable kernel ?
PostPosted: Tue Mar 24, 2009 11:07 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 6:06 pm
Posts: 1437
Location: Vancouver, BC, Canada
You know this thread is eight years old, right? :mrgreen:

_________________
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!


Top
 Profile  
 
 Post subject: Re: How to make a fully preemptable kernel ?
PostPosted: Tue Mar 24, 2009 11:58 pm 
Offline

Joined: Thu Dec 04, 2008 1:32 am
Posts: 2
Yes, noticed that after I posted it. >_<

Oh well, hopefully it will clarify a point about multitasking for anyone who follows it from the Wiki as I did.


Top
 Profile  
 
 Post subject: Re: How to make a fully preemptable kernel ?
PostPosted: Wed Mar 25, 2009 2:44 am 
Offline

Joined: Tue Mar 24, 2009 10:59 am
Posts: 21
More information is never a bad thing :D :D :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 77 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group