What do you exactly mean by Reentrant function? - Page 2

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Re: What do you exactly mean by Reentrant function?

Quoted text here. Click to load it
function.
Quoted text here. Click to load it

If you use the right version of the library. Consider e.g. errno -
when errno is stored as static variable (and not local for a thread or
a task) just doing a simple operation and checking errno can result in
a mess.

Andreas
--
It's not the things you don't know what gets you into trouble. It's
the things you do know that just ain't so.
We've slightly trimmed the long signature. Click to see the full one.
Re: What do you exactly mean by Reentrant function?

Quoted text here. Click to load it

rand(), strtok(), tmpnam(), ctime(), localtime() ?

Re: What do you exactly mean by Reentrant function?
On Thu, 14 Apr 2005 10:00:25 -0700, the renowned Kevin D. Quitt

Quoted text here. Click to load it
function.
Quoted text here. Click to load it

The C99 standard specifically states that they are not guaranteed to
be reentrant.


Best regards,
Spehro Pefhany
--
"it's the network..."                          "The Journey is the reward"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: What do you exactly mean by Reentrant function?
On Thu, 14 Apr 2005 14:27:32 -0400, Spehro Pefhany


Quoted text here. Click to load it

This does not contradict what I wrote.  C is designed against an abstract
machine, and has no
concept of threads or interrupts; therefore it cannot and does not address those
issues.


--
#include <standard.disclaimer>
 _
Kevin D Quitt  USA 91387-4454         96.37% of all statistics are made up
We've slightly trimmed the long signature. Click to see the full one.
Re: What do you exactly mean by Reentrant function?

Quoted text here. Click to load it

Hmm... --- so what do you think <signal.h> and the "volatile" keyword
are all about?

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Re: What do you exactly mean by Reentrant function?
Quoted text here. Click to load it

But then most compilers aren't C99 complient :-)

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England    /\/\/\/\/\
/\/\/ snipped-for-privacy@phaedsys.org       www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/




Re: What do you exactly mean by Reentrant function?
On Fri, 15 Apr 2005 15:09:05 +0100, the renowned Chris Hills

Quoted text here. Click to load it
< snipped-for-privacy@interlogDOTyou.kn
Quoted text here. Click to load it

C99 doesn't *require* the libraries to be non-reentrant. ;-)


Best regards,
Spehro Pefhany
--
"it's the network..."                          "The Journey is the reward"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: What do you exactly mean by Reentrant function?

Quoted text here. Click to load it

No.  I, for one, definetely refuse to consider referring to an
external variable or having a local variable declared static "going
out of my way".  Those are bog-standard language features.  That
they're not exactly advisable to be used if re-entrant code is the
design goal is a different issue.

The problem is not that the C programmer would have to go out of their
way to write non-reentrable code strictly according to the definition
you gave earlier --- it's that it's way too *easy* to do so.

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Re: What do you exactly mean by Reentrant function?
Quoted text here. Click to load it

No so. In many compilers for 8 bit systems (the most common on the
planet) functions are NOT reentrant. You have to use a keyword to make
them reentrant. This is for reasons of space and efficiency. Especially
where you only have 128 or 256 bytes of data space.

Quoted text here. Click to load it

No so.

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England    /\/\/\/\/\
/\/\/ snipped-for-privacy@phaedsys.org       www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/




Re: What do you exactly mean by Reentrant function?

Quoted text here. Click to load it

They are the same. A re-entrant function is simply one that can be called
more than once at a time and still work properly. The classic example of
this is a recursive function which calls itself. The other common example
is one a multi-tasking system where it can be called by more than one task
at the same time.

Quoted text here. Click to load it

Do take time to thoroughly understand the underlying hardware and
microprocessor/controller

Do learn the assembly language of the micro

Do think long and hard about your algorithms before you code them in C
because you do not have unlimited resources (either of memory or execution
time).

Don't use floating point unless it is absolutely necessary.

Do make sure you have a proper requirements specification and if anything in
it is not clear ASK.

Do document the design as it proceeds. Don't leave it till the end.

Do comment you code fully and sensibly.


That'll do for starters.

Ian
Quoted text here. Click to load it

--
Ian Bell

Re: What do you exactly mean by Reentrant function?
Here's an example of a function that is not reentrant:

//  A function to decode a structure (for output), and return
//  a pointer for use in printf.

struct foo {
    int a;
    int b;
};

//  used, e.g.:
//    struct foo *p;
//    ...
//    printf("foo=[%s]\n", decode_foo(p));

char *
decode_foo(const struct foo *p)
{
    static char buf[40];

    sprintf(buf, "a=%d, b=%d", p->a, p->b);
    return buf;
}


The function is not reentrant as it uses a static buffer.  If the
thread of execution changes and a new thread itself calls decode_foo,
then the buffer will be overwritten with the new contents.  On return
to the original thread, the contents will be incorrect.


Here's the reentrant version:

//  used, e.g.:
//    struct foo *p;
//    char buf[80];
//    ...
//    printf("foo=[%s]\n", decode_foo(buf, p));

char *
decode_foo(char *buf, const struct foo *p)
{
    sprintf(buf, "a=%d, b=%d", p->a, p->b);
    return buf;
}


Here, the caller is providing the buffer.  This function, using no
static storage, can be called simultaneously from multiple threads and
generate the correct result (assuming sprintf is reentrant).

Regards,
Paul.


Re: What do you exactly mean by Reentrant function?
Hello Taran,

Quoted text here. Click to load it

Reentrant functions are made in a way that they must not use static memory
locations. Due to the kind especially C-compilers for 8-bit microcontrollers
generate code there is often a special preprocesser directive to be used in
order not to make the compiler use static memory locations for (normally)
automatic variables, which would in fact result in a non-reentrant code
produced from a reentrant looking C source. This is due to the fact that
most 8 bit microcontrollers are very limitted in stack space (often only a
few 10 bytes), so "wasting" stack space for local variables is no good idea.

Quoted text here. Click to load it

Reentrant code must consist purely of code which is reentrant, and it also
must not include calls to non-reentrant functions. So a single (write)
access to a static memory location can make the whole code non-reentrant.

Quoted text here. Click to load it

The best way is to read the manual of the C compiler you use and to have the
limittations of the hardware in mind. "Embedded microcontrollers" are
anything from a 8051 CPU (with a max. of sometimes only 128 bytes which are
shared between registers, stack and variables, 2kByte ROM for program code -
even a printf()-routine in 8051 assembler ist larger) to an ARM or POWER PC
risc machine with 512 MBytes RAM and 1 GByte Flash memory. Also the
architectural differences are enormous: On a 8051 CPU, you have two timers,
one you will waste when you are using a RS-232 interface. On a XC167, you
have nearly 20 different timers, packed in Groups of 2 to 5 timers each,
plus extra registers for capture/compare purposes. The TriCore and MACH
architectures of microcontrollers have timer-coprocessors, which are more
complex than a single 8051 microcontroller. So there are no "do's and
dont's" fitting for all architectures you want to use.

Greetings, Axel
--------------------------------------------------
Für Antworten bitte ohne Punkt im Namen!



Site Timeline