reenternet

Submitted by shankar
on February 26, 2007 - 3:46am

Hi ...
I wanted to know what is the re-enterent code in linux kernel.
and what is the link between re-enterent and critical section .
how we identify a critical section in the code with out locks.

if possible give me some examples also.

regards
shankar

Reentrant

Anonymous (not verified)
on
February 26, 2007 - 1:14pm

I'm still trying to wrap my brain around this one, but here goes...

Reentrant means that a section of code can be safely called recursively (can call itself) or from multiple processes.

Quote from Linux Device Drivers, 3rd edition:
"(...)reentrant--it must be capable of running in more than one context at the same time."

For a function to be reentrant it must:
- _only_ operate on data provided by the caller
- not hold static data
- not return a pointer to static data
- not call functions that, themselves, are not reentrant.

Examples
Not reentrant:

int var_global = 0;

int first_func() {
  var_global = var_global + 1; 
  return var_global;
}

int second_func() {
  return first_func() + 2;
}

int main()
{
  second_func();
  return 0;
}

The above "first_func()" is not reentrant because it uses a global variable (var_global).
"second_func()" is also not reentrant because it calls a non-reentrant function.

With regards to 'links' between reentrant code and critical section(s), there is none from what I can tell.
In fact they seem, almost, at cross purpose.
'Reentrant' ensures that data is safe to be used by other, possibly concurrent, contexts. Its controlled to make sure nothing is lost or stomped on.
Whereas 'critical section(s)' encapsulate accesses to shared resources that you don't want to be accessed by more than one thread at a time.
*Threads and contexts are not the same.

One does not _need_ to have the other.
But you will find examples of both in kernel code.
And you can code with both.

Can someone enlighten us a little more?
It would be really appreciated.

JY

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.