Reentrancy

From COMP15212 Wiki
Depends on Atomicity

A fragment of code is “reentrant” if it can be executed correctly more than once, simultaneously.

Typically this means that the code uses only local variables and has no side-effects on any state visible externally. (A change in external state could be something like changing a file pointer having read in a character, so be careful!)

You have probably experienced re-entrant code already, in the form of recursive code. During recursive execution several copies of the relevant functions may be active, even though there is ever only one thread executing.

Puzzle: in the example fact.c (download it here), is the recursively called ‘factorial()’ function re-entrant code?

Answer

Answer I’d say: “yes”, as the ‘global’ variable is local to this single thread/process.

but

not if the function was used in (for example) a shared library where the values could interfere.

Re-entrancy is often important in operating system routines as (e.g.) the same system call can be made by multiple processes in an indeterminate order. On the other hand, sometimes sections of the code are not re-entrant and therefore need some form of access control.

Always think about cases when variables have to be private.


Also refer to: Operating System Concepts, 10th Edition: Chapter 9.3.4, pages 369-371


Articles on Concepts
About this resource • Application Binary Interface (ABI) • Arrays • Atomicity • Boot • Cache • Cacheability • Caching • Concepts • Containers • Context • Context Switching • Deadlock • Direct Memory Access (DMA) • Environment Variables • Exceptions • File Attributes • Fragmentation • Hypervisor • Interrupts • Operation Ordering • PATH • Pointers • Process Scheduling • Processes • Processor Privilege • Queues • Real Time • Reentrancy • Relocatable Code • Spooling and Buffering • Synchronisation • Thrashing • Threads • Virtual Memory • Virtualisation
Articles on Deadlock
Atomicity • Deadlock • File Locking • Locks • Multi Threading • Multiprocessors • Mutual exclusion • Operation Ordering • Race Conditions • Reentrancy • Semaphores • Starvation • Threads
Articles on Major concepts
Cache • Cacheability • Concepts • Context • Direct Memory Access (DMA) • Exceptions • Hypervisor • Metadata • Process Scheduling • Processor Privilege • Real Time • Reentrancy • Synchronisation • Virtualisation