Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > re-entrant function????????

Reply
Thread Tools

re-entrant function????????

 
 
thesushant@rediffmail.com
Guest
Posts: n/a
 
      01-11-2005
hi,

can anyone of C masters siting out there suggest any 1 example of
re-entrnt function just to show what is the significance of that
property and how we can exploit it ....

sushant

 
Reply With Quote
 
 
 
 
Yan
Guest
Posts: n/a
 
      01-11-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> hi,
>
> can anyone of C masters siting out there suggest any 1 example of
> re-entrnt function just to show what is the significance of that
> property and how we can exploit it ....
>
> sushant
>


that becomes an issue when using/writing multi-threading code. If a
function relies on static data, and is called while another thread has
already called it and is executing, that static data may be changed by
different threads and produce unexpected results
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      01-11-2005
(E-Mail Removed) wrote:
>
> can anyone of C masters siting out there suggest any 1 example of
> re-entrnt function just to show what is the significance of that
> property and how we can exploit it ....


#include <stdio.h>

int putword(unsigned long w, FILE *f)
{
if (w > 9)
if (0 > putword(w / 10, f)) return EOF;
return putc((w % 10) + '0', f);
} /* putword */

/* --------------- */

#ifdef TESTING

#include <stdlib.h>

int main(void)
{
int i;

for (i = 0; i < 10; i++) {
putword(i, stdout);
putc(' ', stdout);
putword(rand(), stdout);
putc('\n', stdout);
}
return 0;
} /* main */
#endif


--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Trent Buck
Guest
Posts: n/a
 
      01-11-2005
Up spake (E-Mail Removed):
> can anyone of C masters siting out there suggest any 1 example of
> re-entrnt function just to show what is the significance of that
> property and how we can exploit it ....


This is re-entrant

int
f (int x)
{
int y;

y = x * x;
return y;
}

This is not

int y;

int
f (int x)
{
y = x * x;
return y;
}

Another example of re-entrant vs. non-reentrant functions is strtok_r
vs. strtok.

As the OP said, it becomes an issue when you have multiple threads
(lightweight processes) in a single process, because (some) state is
shared between multiple threads of execution.

The canonical example is

int x;
void f (void) { x++; }

with two threads executing f(). The ++ operation (typically) resolves
into three assembly instructions: copy-memory-to-register,
increment-register, and copy-register-to-memory.

Consider, for example, the situation where first thread executes the
first two instructions, then the second thread executes all three, then
the first thread executes the last instruction. The second thread's
change will be lost, and the contents of memory at that address will be
incorrect.

This class of bugs are a PITA to locate, because symptoms are
intermittent and therefore difficult to reproduce.

--
-trent
How, Joe wondered, can a man have courage without faith, without belief?
Burroughs believed in nothing, and yet there he sat stubborn as Luther.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      01-11-2005
CBFalconer wrote:
>
> (E-Mail Removed) wrote:
> >
> > can anyone of C masters siting out there suggest any 1 example of
> > re-entrnt function just to show what is the significance of that
> > property and how we can exploit it ....

>
> #include <stdio.h>
>
> int putword(unsigned long w, FILE *f)
> {
> if (w > 9)
> if (0 > putword(w / 10, f)) return EOF;
> return putc((w % 10) + '0', f);
> } /* putword */


Standard library functions aren't guaranteed to be reentrant, so,
a function which calls putc can't be guaranteed to be reentrant.

--
pete
 
Reply With Quote
 
Thomas Stegen
Guest
Posts: n/a
 
      01-11-2005
pete wrote:
> CBFalconer wrote:
>
>>(E-Mail Removed) wrote:
>>
>>>can anyone of C masters siting out there suggest any 1 example of
>>>re-entrnt function just to show what is the significance of that
>>>property and how we can exploit it ....

>>
>>#include <stdio.h>
>>
>>int putword(unsigned long w, FILE *f)
>>{
>> if (w > 9)
>> if (0 > putword(w / 10, f)) return EOF;
>> return putc((w % 10) + '0', f);
>>} /* putword */

>
>
> Standard library functions aren't guaranteed to be reentrant, so,
> a function which calls putc can't be guaranteed to be reentrant.
>


More importantly imo is that putc has a side effect on the file and so
the function is not reentrant even on an implementation where putc is
reentrant.

--
Thomas.
 
Reply With Quote
 
dandelion
Guest
Posts: n/a
 
      01-11-2005

"Trent Buck" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
<snip>

> As the OP said, it becomes an issue when you have multiple threads
> (lightweight processes) in a single process, because (some) state is
> shared between multiple threads of execution.

<snip>

Minor nitpick:

Reentrancy is not just a multi-threading (lightweight processes) issue, but
arises whenever two threads of execution have access to some common
function. Apart from the example given, this may also happen in Interrupt
Service Routines (ISR's) or multitasking environments wich allow concurrent
access to a shared resource.


 
Reply With Quote
 
Eltee
Guest
Posts: n/a
 
      01-11-2005
dandelion wrote:
> "Trent Buck" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> <snip>
>
>>As the OP said, it becomes an issue when you have multiple threads
>>(lightweight processes) in a single process, because (some) state is
>>shared between multiple threads of execution.

>
> <snip>
>
> Minor nitpick:
>
> Reentrancy is not just a multi-threading (lightweight processes) issue, but
> arises whenever two threads of execution have access to some common
> function. Apart from the example given, this may also happen in Interrupt
> Service Routines (ISR's) or multitasking environments wich allow concurrent
> access to a shared resource.


Pardon me but ... are reentrancy, multi-threading, ISR's and multitasking really
a part of ISO C specification?
 
Reply With Quote
 
dandelion
Guest
Posts: n/a
 
      01-11-2005

"Eltee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> dandelion wrote:
> > "Trent Buck" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > <snip>
> >
> >>As the OP said, it becomes an issue when you have multiple threads
> >>(lightweight processes) in a single process, because (some) state is
> >>shared between multiple threads of execution.

> >
> > <snip>
> >
> > Minor nitpick:
> >
> > Reentrancy is not just a multi-threading (lightweight processes) issue,

but
> > arises whenever two threads of execution have access to some common
> > function. Apart from the example given, this may also happen in

Interrupt
> > Service Routines (ISR's) or multitasking environments wich allow

concurrent
> > access to a shared resource.

>
> Pardon me but ... are reentrancy, multi-threading, ISR's and multitasking

really
> a part of ISO C specification?


Reentrancy is, for the simple reason that the standard lib is not guaranteed
to be reentrant (and that is, IIRC, part of the standard). A further
discussion of "reentrancy" brings on the other subjects.

So while not being part of the standard, they (multithreading, ISR's and
shared resources) are topical in this limited treatment.


 
Reply With Quote
 
Eltee
Guest
Posts: n/a
 
      01-11-2005
dandelion wrote:
> "Eltee" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>dandelion wrote:
>>
>>>"Trent Buck" <(E-Mail Removed)> wrote in message
>>>news:(E-Mail Removed)...
>>><snip>
>>>
>>>>As the OP said, it becomes an issue when you have multiple threads
>>>>(lightweight processes) in a single process, because (some) state is
>>>>shared between multiple threads of execution.
>>>
>>><snip>
>>>
>>>Minor nitpick:
>>>
>>>Reentrancy is not just a multi-threading (lightweight processes) issue,

>
> but
>
>>>arises whenever two threads of execution have access to some common
>>>function. Apart from the example given, this may also happen in

>
> Interrupt
>
>>>Service Routines (ISR's) or multitasking environments wich allow

>
> concurrent
>
>>>access to a shared resource.

>>
>>Pardon me but ... are reentrancy, multi-threading, ISR's and multitasking

>
> really
>
>>a part of ISO C specification?

>
>
> Reentrancy is,


Would you point some fingers, please? Where can I find the ISO C specification
and, specificaly, where does it say anything about reentrancy?

> for the simple reason that the standard lib is not guaranteed
> to be reentrant (and that is, IIRC, part of the standard). A further
> discussion of "reentrancy" brings on the other subjects.
>
> So while not being part of the standard, they (multithreading, ISR's and
> shared resources) are topical in this limited treatment.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments