Reentrant
|
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 users or separate processes. The key to designing a reentrant program is to ensure that no portion of the program code is modified by the different users/processes, and that process-unique information (such as local variables) is kept in a separate area of memory that is distinct for each user or process.
FreeBSD, a derivative of the 4.4BSD-Lite, utilizes a massively reentrant VFS. One of the goals of DragonFlyBSD, a new BSD derivative branched off of FreeBSD, is to replace FreeBSD's reentrant VFS API with a threaded messaging API. See DragonFlyBSD's goals page [1] (http://www.dragonflybsd.org/goals/vfsmodel.cgi) for more details.
Reentrant programming is key to many systems of multitasking. See thread-safe.
The kernel code or the code implementing synchronization like semaphore is generally not reentrant because it handles the shared memory.
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 to 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.
External links
- Article "Use reentrant functions for safer signal handling (http://www-106.ibm.com/developerworks/linux/library/l-reent.html)" by Dipak K. Jhade:Eintrittsinvarianz