Reentrant routines and what is their use ?

Embedded C interview question

What are reentrant routines and what is their use?

Answer

Used to describe code, which can have multiple simultaneous, interleaved, or nested invocations, which will not interfere with each other. This is important for parallel processing, recursive functions or subroutines, and interrupt handling. 
It is usually easy to arrange for multiple invocations (e.g. calls to a subroutine) to share one copy of the code and any read-only data but, for the code to be re-entrant, each invocation must use its own copy of any modifiable data (or synchronized access to shared data). This is most often achieved using a stack and allocating local variables in a new stack frame for each invocation. Alternatively, the caller may pass in a pointer to a block of memory which that invocation can use (usually for outputting the result) or the code may allocate some memory on a heap, especially if the data must survive after the routine returns.

No comments:

Post a comment