nonreentrant function 
Author Message
 nonreentrant function

What's reentrant and nonreentrant functions? Why strtok is called non-reentrant
function?

--
 +-------------------------------------------------------------------------+
 |  Raymond Cheung      (University of Houston, Computer Science Dept)     |




 |  Home page:                         http://www.*-*-*.com/ ~ycheung      |
 +-------------------------------------------------------------------------+



Sat, 17 Apr 1999 03:00:00 GMT  
 nonreentrant function

Re-entrant code can be shared concurrently between users and processes.
strtok has internal data storage to remember where you were from last call.

--

Bugs in my code says I am not perfect.




Quote:

> What's reentrant and nonreentrant functions? Why strtok is called
non-reentrant
> function?

> --

+-------------------------------------------------------------------------+
Quote:
>  |  Raymond Cheung         (University of Houston, Computer Science Dept)     |




>  |  Home page:                           http://www.cs.uh.edu/~ycheung      |

+-------------------------------------------------------------------------+


Sat, 17 Apr 1999 03:00:00 GMT  
 nonreentrant function

Quote:

>Re-entrant code can be shared concurrently between users and processes.

False. Strtok is not reentrant, yet multiple processes in a UNIX system can
happily use it without conflict, because the global variable used by strtok()
lives in a separate environment associated with each process.

Reentrancy is a slippery eel which doesn't lend itself to easy definition.
The reentrancy of a function depends on the environment. It's probably
better to talk about the reentrancy of an entry point with respect to
a particular subsystem.

Here are my personal definitions:

a _reentrant_ procedure is one which references only
variables in its own activation record and in the activation records of
the invoking procedures.

A _thread-safe_ procedure is one which references only variables that
are not shared by multiple threads, or accesses shared variables in
a safe manner, using proper synchronization and exclusion mechanisms.
Such a procedure is not necessarily reentrant, because it may obviously
refer to variables other than those permitted to a _reentrant_ procedure.

A reentrant procedure is automatically thread safe, because variables inside
the activation chain are not shared among threads.

Good to have these answers ready for a technical interview.

When it comes to mulitiple processes in an operating system like UNIX,
you might as well be talking about separate machines. Each process runs
in a virtual machine with virtual resources. Non-reentrant routines
in one process have no bearing on another, unless they access resources
that are shared between processes (e.g. a procedure which updates a file
is non-reentrant in a sense because a file is basically a shared variable).

Given these definitions, it's clear that strtok() can never be reentrant,
because its _semantics_ as defined by the C language standard clearly call
for the preservation of some state between successive invocations of strtok()
which clearly does not reside in the activation chain (call it a stack if you
will, but the C language standard doesn't talk about stacks). The only way to
make a reentrant strtok() is to rewrite a function that does the same thing but
in a different way (you break the interface and have to fix code that uses
strtok() to use the new function).

The LinuxThreads library provides a _potentially_ reentrant strtok() int the
form of strtok_r() which has a different syntax. This function takes as an
extra parameter the pointer to the character pointer that is to retain the
state between invocations. Why do I say _potentially_ reentrant? Because the
reentrancy can be spoiled if we aim this pointer at static storage.  This is
one of the reasons why reentrancy is hard to define: the reentrancy of a
function may depend on the nature of the arguments. If a function accepts
pointers, and these pointers reference something other than auto variables in
the activation chain, that _invocation_ of it is not reentrant. Perhaps
reentrancy should be talked about as a kind of behavior of a procedure
activation rather than a static attribute of a piece of code.

You can also make a thread-safe strtok() which is non-reentrant, by having a
thread-specific variable in which strtok() can preserve its state information.
Or you can make a thread-safe version by changing the interface to include the
locking and releasing of a mutex around a strtok() session.  The former method
preserves compatibility with existing code, at the cost of efficiency. The
latter is not worth it because if you are going to have to fix the client code,
you might as well just rewrite a reentrant function. Plus it creates a
potential bottleneck since threads cannot call strtok() in parallel.

This is why reentrancy is such a good thing: a procedure whose invocations
are reentrant is automatically thread-safe. A procedure whose invocations
are _necessarily_ reentrant is even a better thing, since the arguments
cannot thwart its reentrancy, and hence it needs not ever be suspected of being
unsafe.



Sat, 17 Apr 1999 03:00:00 GMT  
 nonreentrant function

: Re-entrant code can be shared concurrently between users and processes.
: strtok has internal data storage to remember where you were from last call.

: --

: Bugs in my code says I am not perfect.




: >
: > What's reentrant and nonreentrant functions? Why strtok is called
: non-reentrant
: > function?
: >
: > --
: >
: +-------------------------------------------------------------------------+
: >  |  Raymond Cheung       (University of Houston, Computer Science Dept)     |




: >  |  Home page:                         http://www.cs.uh.edu/~ycheung      |
: >
: +-------------------------------------------------------------------------+
: >

--
**************begin r.s. response*******************

        in a system supporting multiple
        processes (e.g. unix)
        if
        multiple processes
        execute the same code,
        possibly these can execute the same
        binary and need not have individual
        copies of this...
        then,
        only data segments need be
        independently kept...
        when such is achievable...
        such code is reentrant...

**************end r.s. response*********************
Ralph Silverman



Sat, 17 Apr 1999 03:00:00 GMT  
 nonreentrant function


Quote:

>What's reentrant and nonreentrant functions? Why strtok is called non-reentrant
>function?

I suppose you are programming on a unix-system.

If a process catches a signal the normal run of will be interrupted, and
the signal catching function is called. After the signal catcher has done
the work the normal run will be continued.
This signal catcher does'nt know what the process has done at time it recieves
the signal. What is if the process has called malloc(), and the signal catcher
calls malloc() too? If the first call of malloc() hasn't completed the work
(updating internal list of free memory) the second call can cause a desaster.
Or if you call some function wich saves information in static variables, and
your signal catcher calls the same function? The information of the first call
perhaps will be lost. E.g. strtok() saves a pointer to the next token in a static
variable. Such functions are called non-reentrant. POSIX defines a couple of
reentrant functions.

Hope this helps you.

If not, read e.g. W. Richard Stevens: "Advanced Programming in the UNIX
Environment", 1992. This is a standard book about programming on unix. There
is also a list of reentrant functions in the chapter about signals.

Marcus

+--------------------------------------------------------------------------+
  Marcus Alexander Dietz, Institut fuer Angewandte Matematik - Uni Hamburg

  URL...: http://www.math.uni-hamburg.de/home/dietz/



Sun, 18 Apr 1999 03:00:00 GMT  
 nonreentrant function

: Re-entrant code can be shared concurrently between users and processes.
: strtok has internal data storage to remember where you were from last call.

: --

: Bugs in my code says I am not perfect.




: >
: > What's reentrant and nonreentrant functions? Why strtok is called
: non-reentrant
: > function?
: >
: > --
: >
: +-------------------------------------------------------------------------+
: >  |  Raymond Cheung       (University of Houston, Computer Science Dept)     |




: >  |  Home page:                         http://www.cs.uh.edu/~ycheung      |
: >
: +-------------------------------------------------------------------------+
: >

--
***************begin r.s. response******************

        also,
        relatedly as regarding the
        issue of reentrancy in
        multi-process os (e.g.  unix)
        ,
        in multi-threaded environments
        (languages)
                simula
                modula-2
                ada
         etc.
        non-reentrancy may invalidate
        operation of
                coroutines,
        where such are used...

***************end r.s. response********************
Ralph Silverman



Sun, 18 Apr 1999 03:00:00 GMT  
 nonreentrant function

Sorry, Ralph.  Correction below.



[ snipped:  What is a reentrant function?  Why is strtok non-reentrant? ]

Quote:
> **************begin r.s. response*******************

>    in a system supporting multiple
>    processes (e.g. unix)
>    if
>    multiple processes
>    execute the same code,
>    possibly these can execute the same
>    binary and need not have individual
>    copies of this...
>    then,
>    only data segments need be
>    independently kept...
>    when such is achievable...
>    such code is reentrant...

> **************end r.s. response*********************

Suppose a thread of execution (thread1) shares the same
data address space as another thread (thread2), and that
thread1 might be suspended at any time while thread2 is
allowed to run.  If a function is designed such that its
execution by thread1 can be suspended, then allow thread2
to use it (re-enter it), without disturbing the result that thread1
will obtain (when it is resumed), then we say the function is
"reentrant".  

Even in nominally single threaded UNIX systems, use of
the signal() function can lead to re-entry of functions.

The separation of data segments (RS terminology) is
separate from reentrancy.

For its operation, strtok() relies on static data that it keeps
from one invocation to another.  If it is re-entered, that
data can be modified.  In general, reentrant functions use
only auto variables or heap allocations that are somehow
specific to the individual calling threads.

--
Larry Brasfield        My views are my own, at least.



Sun, 18 Apr 1999 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Call a function in a function with name of a function as parameter

2. functions, functions, and more functions!

3. How to use (pointer to function), and function and pointer to (pointer to function)

4. Call C++ member function from C function ?

5. Class function pointer does not evaluate to a function

6. declaring functions inside other functions

7. function of a function and variable declaration...

8. Question : A generic function to retrieve the call parameters of a function

9. function returning a function ptr

10. Function returning a function-pointer - how to prototype ?

11. Intialize the list of functions with runtime library common function

12. Is there a similar function in C# to the shell function in VB

 

 
Powered by phpBB® Forum Software