[comp.lang.c] what the heck is "reentrant"?

tedj@hpcilzb.HP.COM (Ted Johnson) (07/29/88)

What the heck is the definition of "reentrant" vs. "non-reentrant",
for subroutines and programs?

-Ted

gwyn@brl-smoke.ARPA (Doug Gwyn ) (07/30/88)

In article <650002@hpcilzb.HP.COM> tedj@hpcilzb.HP.COM (Ted Johnson) writes:
>What the heck is the definition of "reentrant" vs. "non-reentrant",
>for subroutines and programs?

A reentrant routine can be interrupted in the midst of operation,
reinitiated on behalf of the interrupt-processing thread of
execution, then resumed at the point of interruption with neither
invocation broken by the other.  A non-reentrant routine can't.

There is also the concept of "serially reusable" which means that
the routine can be executed from start to finish more than once
with no coupling between the invocations.  Most C functions are
written to be serially reusable; they don't modify data having
static storage duration.

orr@taux02.UUCP (Orr Michael ) (08/10/88)

   A simple way to understand this, (though not really a full definition) is
Like this:
   A piece of code is "Re-entrant" if two (or more) processes can execute
   its' code at the same time. "at the same time" means that each process
   can have its' program counter pointing into the same code area, so that
   when either process is made (by the OS) to run, an instruction of the same
   code will be executed next. This makes it necessary for Re-entrant code
   to be "pure", i.e. instructions only, no data, because each process excuting
   that code expects its' own data, though they are all running the same code.
   Example :
   say you have a sort program. if it is written so that 
   1. the data to be sorted is accessed only through pointers
   2. these pointers are initialized as part of the code

[Nit-pickers : I know both of the above are NOT essential, and not the only
 way to do this. this is just an example, hopefully clarifying my meaning]

   then 1 process may start using the program, get to the point where the
   data pointers are initialized and start sorting, and then, if another
   process starts running, and it wants to sort too, it can simply execute
   the same instructions the 1st process did. since the pointers will be
   initialized again, this time pointing to different data, all will be well,
   and both processes can co-exist.
   This is most usefull for programs in common use by many users at once,
   e.g. editors, shells, etc.
-- 
orr%@nsc.NSC.COM		  	      IBM's motto: Machines should work,
{amdhal,decwrl,hplabs,pyramid,sun}!nsc!orr             People should think.
                                 	      Orr's remark: Neither do.
Disclaimer: Opinions, come home. All is forgiven. Papa.