OSDev.org
https://forum.osdev.org/

How to make a fully preemptable kernel ?
https://forum.osdev.org/viewtopic.php?f=1&t=812
Page 1 of 1

Author:  mudi [ Thu Aug 02, 2001 11:00 pm ]
Post subject:  How to make a fully preemptable kernel ?

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

Author:  J. Weeks [ Thu Aug 02, 2001 11:00 pm ]
Post subject:  RE:How to make a fully preemptable kernel ?

>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

Author:  mudi [ Fri Aug 03, 2001 11:00 pm ]
Post subject:  RE:How to make a fully preemptable kernel ?

>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

Author:  j.weeks [ Sun Aug 05, 2001 11:00 pm ]
Post subject:  RE:How to make a fully preemptable kernel ?

>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

Author:  Guest [ Mon Aug 06, 2001 11:00 pm ]
Post subject:  RE:How to make a fully preemptable kernel ?

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

Author:  Kelmar [ Tue Mar 24, 2009 10:42 pm ]
Post subject:  Re: RE:How to make a fully preemptable kernel ?

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.

Author:  Colonel Kernel [ Tue Mar 24, 2009 11:07 pm ]
Post subject:  Re: How to make a fully preemptable kernel ?

You know this thread is eight years old, right? :mrgreen:

Author:  Kelmar [ Tue Mar 24, 2009 11:58 pm ]
Post subject:  Re: How to make a fully preemptable kernel ?

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.

Author:  ThymeCypher [ Wed Mar 25, 2009 2:44 am ]
Post subject:  Re: How to make a fully preemptable kernel ?

More information is never a bad thing :D :D :D

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/