reentrant functions

K

Kaz Kylheku

What is a reentrant function? Are there any of these in c89 or c99?

Something that keeps going back into a newsgroup, without being aware
of any previous state.
 
G

Guest

What is a reentrant function? Are there any of these in c89 or c99?

one that can be re-entered. This really applies to threading or interupts neither of which the older C standards address. So we don't know if any function is re. But in fact most of them could fairly easily be made reentrant (eg. sin, strcpy etc.). Functions that hold internal state or have side effects (change the environment) likely aren't- strtok(), malloc(), rand(), anything to do with i/o
 
J

James Kuyper

one that can be re-entered. ...

A definition that uses another version of the same word being defined is
generally not very helpful.

A re-entrant function is one that can be safely be called while
execution of another call to that function is still in progress. One of
the most common barriers to re-entrancy is writing to memory with static
storage duration (in C2011, thread storage duration poses similar
problems) which is shared between the two copies of the function;
however, any other resource shared between them (such as files) could
also be a source of problems.
... This really applies to threading or interupts neither of which the older C standards address.

Reentrancy can also be an issue for directly or indirectly recursive
function calls, or for signal handlers, which were issues that could
come up even in C90. For instance, the fact that strtok() is not
re-entrant caused problems for some utility library code which I did not
write, but for which I was responsible. The calling function was in the
middle of a strtok() loop when it called my library routine which
contained it's own strtok() loop. I changed my function to use an
alternative approach to tokenizing the string it was parsing.
So we don't know if any function is re. But in fact most of them could fairly easily be made reentrant (eg. sin, strcpy etc.). Functions that hold internal state or have side effects (change the environment) likely aren't- strtok(), malloc(), rand(), anything to do with i/o

7.1.4p4: "The functions in the standard library are not guaranteed to be
reentrant and may modify objects with static or thread storage duration."

However, many of the optional functions described in Annex K, which are
new in C2011, are intended to address re-entrancy issues associated with
the older versions of those functions.
 
B

Bill Cunningham

James said:
A definition that uses another version of the same word being defined
is generally not very helpful.

A re-entrant function is one that can be safely be called while
execution of another call to that function is still in progress. One
of the most common barriers to re-entrancy is writing to memory with
static storage duration (in C2011, thread storage duration poses
similar problems) which is shared between the two copies of the
function; however, any other resource shared between them (such as
files) could also be a source of problems.


Reentrancy can also be an issue for directly or indirectly recursive
function calls, or for signal handlers, which were issues that could
come up even in C90. For instance, the fact that strtok() is not
re-entrant caused problems for some utility library code which I did
not write, but for which I was responsible. The calling function was
in the middle of a strtok() loop when it called my library routine
which contained it's own strtok() loop. I changed my function to use
an alternative approach to tokenizing the string it was parsing.


7.1.4p4: "The functions in the standard library are not guaranteed to
be reentrant and may modify objects with static or thread storage
duration."

However, many of the optional functions described in Annex K, which
are new in C2011, are intended to address re-entrancy issues
associated with the older versions of those functions.

Google didn't help me much on this. I was looking at posix functions.
Some reentrant some evidently not. These reentrant functions usually end in
*_r. I thought maybe the same thing existed in C and could help me
understand.

tfyh

B
 
B

Barry Schwarz

Google didn't help me much on this. I was looking at posix functions.
Some reentrant some evidently not. These reentrant functions usually end in
*_r. I thought maybe the same thing existed in C and could help me
understand.

Any function which limits the objects it updates to ones that have
automatic duration bounded by the life of the function and which calls
only functions which also observe this limitation is potentially
reentrent. The fact that most functions don't call themselves
recursively doesn't mean they couldn't.

So you must be looking for something else. Did you perhaps mean
thread-safe?
 
B

Bill Cunningham

Barry said:
Any function which limits the objects it updates to ones that have
automatic duration bounded by the life of the function and which calls
only functions which also observe this limitation is potentially
reentrent. The fact that most functions don't call themselves
recursively doesn't mean they couldn't.

So you must be looking for something else. Did you perhaps mean
thread-safe?

The man page. man (de)crypt did say anything about threads. But it did
mean static storage and non static storage and provided a struct to use if
one wanted reentrant. This is on my linux system but does seem to show on
linix.die.com.

B
 
J

jacob navia

Le 27/04/12 00:30, Bill Cunningham a écrit :
The man page. man (de)crypt did say anything about threads. But it did
mean static storage and non static storage and provided a struct to use if
one wanted reentrant. This is on my linux system but does seem to show on
linix.die.com.

WOW! What a sentence Bill. That's quite a show.

"The man page. man (de)crypt did say anything about threads."

Maybe

"The man page. man (de)crypt didn't say anything about threads."

or maybe

"The man page. man (de)crypt says something about threads."

Nobody knows. Let's go on.

"But it did mean static storage and non static storage"

Sure, either of those but which? You seem to be able to say something
and the contrary in the same sentence and it doesn't bother you at all.

So let's go on.

"and provided a struct to use if one wanted reentrant"

EUREKA, a sentence that doesn't have a contradiction. But what were you
TALKING ABOUT?

You forgot.

The man page? The decrypt function? The static storage? The not static
storage? The linix system? The linux system? The die.com domain?

Well, "linix.die.com" is YOUR domain Cunnigham. You should go there and
STAY there.
 
B

Bill Cunningham

jacob said:
Le 27/04/12 00:30, Bill Cunningham a écrit :

WOW! What a sentence Bill. That's quite a show.

"The man page. man (de)crypt did say anything about threads."

Maybe

"The man page. man (de)crypt didn't say anything about threads."

The above was correct.
[snip]
I have looked at decrypt and crypt both. I shall recheck for those
interested.

B
 
B

Bill Cunningham

Bill said:
jacob said:
Le 27/04/12 00:30, Bill Cunningham a écrit :

WOW! What a sentence Bill. That's quite a show.

"The man page. man (de)crypt did say anything about threads."

Maybe

"The man page. man (de)crypt didn't say anything about threads."

The above was correct.
[snip]
I have looked at decrypt and crypt both. I shall recheck for those
interested.

B
http://www.kernel.org/doc/man-pages/online/pages/man3/encrypt.3.html

Now this is the page I'm talking about.

B
 
M

Michael Angelo Ravera

What is a reentrant function? Are there any of these in c89 or c99?

A function is reentrant, if its result is independant of what else is going on on the system. In other words, if a call to it may be suspended or interupted, partly executed again, and the original resumed without altering the results of the first.

It is difficult, for instance, to write a time function that is reentrant. Usually, you have to stop other programs from running for a short while you grab data off of the clock.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top