Reentrant

From Free net encyclopedia

(Redirected from Re-entrant)
For the military term, see Salients, re-entrants and pockets.

A computer program or routine is described as reentrant if it is designed in such a way that a single copy of the program's instructions in memory can be shared by multiple, separate users, object classes, or processes. The key to designing a reentrant program is to ensure that no portion of the program code is modified by the different users/objects/processes, and that the user/object/process unique information (such as local variables) is kept in a separate area of memory that is distinct for each user, object, or process.

Reentrant programming is key to many systems of multitasking.

The kernel code or the code implementing synchronization like semaphore is generally not reentrant because it handles the shared memory (i.e., the 'environment,' of which there is normally only one instance). Note that multiple levels of 'user/object/process priority' and/or multiprocessing greatly complicate the control of reentrant code.

Examples

In the following piece of code, both functions f and g are not reentrant.

int g_var = 1;

int f()
{
  g_var = g_var + 2;
  return g_var;
}

int g()
{
  return f() + 2;
}

In the above, f depends on a global variable g_var; thus, if two processes execute it and access g_var concurrently, then the result varies depending on the timing of the execution. Hence, f is not reentrant. Neither is g; it calls f, which is not reentrant.

See also

External links

fr:Réentrance it:Codice rientrante ru:Реентерабельность