292 views

Which of the following is true?

1. A re-entrant procedure can only be called a fixed number of times
2. A re-entrant procedure can be called even before the procedure has not returned from it's previous call
3. Re-entrant procedures can not be called recursively
4. none of the above

### 1 comment

I dont understand how option B is false.

Recursive code is an example of reentrant code. Now if I call fibonacci(6), fibonacci(5) would return value before fibonacci(6) and so on. So here fib(5) has returned value before fib(6) as fib(6) is the last function to return value...plz explain @arjun.

a procedure is reentrant if it can be interrupted in the middle of its execution, and then be safely called again ("re-entered") before its previous invocations complete execution. The interruption could be caused by an internal action such as a jump or call, or by an external action such as a hardware interrupt or signal. Once the reentered invocation completes, the previous invocations will resume correct execution.

A subroutine that is directly or indirectly recursive should be reentrant. This policy is partially enforced by structured programming languages. However a subroutine can fail to be reentrant if it relies on a global variable to remain unchanged but that variable is modified when the subroutine is recursively invoked.

Where we encounter reentrant procedures:-

• The routine is recursive (or mutually-recursive with some other set of routines).
• It gets called by another thread.
• It gets called by an interrupt.

If any of these happen, and the routine is modifying a global (or C static local), then the new execution could potentially wipe out the changes the first execution made. As an example, if that global was used as a loop control variable, it might cause the first execution, when it finally gets to resume, to loop the wrong number of times.

1
120 views