|
A computer program or routine is described as reentrant if it can be safely called recursively or from multiple processes. To be reentrant, a function must hold no static data, must not return a pointer to static data, must work only on the data provided to it by the caller, and must not call non-reentrant functions. A computer program is a collection of instructions that describe a task, or set of tasks, to be carried out by a computer. ...
In computer science, a subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code. ...
A common method of simplification is to divide a problem into subproblems of the same type. ...
Despite a common misconception, this is not the same as being designed in such a way that a single copy of the program's instructions, in memory, can be shared. 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. A kernel connects the application software to the hardware of a computer. ...
A semaphore is a protected variable (or abstract data type) and constitutes the classic method for restricting access to shared resources (e. ...
A priority queue is an abstract data type in computer programming, supporting the following three operations: add an element to the queue with an associated priority remove the element from the queue that has the highest priority, and return it (optionally) peek at the element with highest priority without removing...
Multiprocessing is traditionally known as the use of multiple concurrent processes in a system as opposed to a single process at any one instant. ...
Examples In the following piece of C code, neither functions f nor g are reentrant. C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ...
int g_var = 1; int f() { g_var = g_var + 2; return g_var; } int g() { return f() + 2; } int main() { g(); return 0; } 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. [But, in the above C code example, two different processes should have two different copies of the global variable, why will there be any conflict? ] In computer programming, a global variable is a variable that is accessible in every scope. ...
These slightly-altered versions are reentrant: int f(int i) { int priv = i; priv = priv + 2; return priv; } int g(int i) { int priv = i; return f(priv) + 2; } int main() { g(1); return 0; } See also Thread-safety is a computer programming concept applicable to multi-threaded programs. ...
External links - Article "Use reentrant functions for safer signal handling" by Dipak K Jha
- Writing Reentrant and Thread-Safe Code
|