Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Reentrant function in c? [duplicate]

Tags:

c

I have been searching for the definition and the use of reentrant function.But I couldn't understand the definition given in other web pages.If any on knows pls explain it simply?

like image 323
adhi .b Avatar asked Jan 13 '16 05:01

adhi .b


People also ask

What is a reentrant function?

A function is said to be reentrant if there is a provision to interrupt the function in the course of execution, service the interrupt service routine and then resume the earlier going on function, without hampering its earlier course of action. Reentrant functions are used in applications like hardware interrupt handling, recursion, etc.

Do Re-entrant functions trample on each other in C?

Re-entrant functions have to follow certain guidelines (such as no use of static variables in C) lest different instances of them trample on each other. Show activity on this post.

What is the difference between a mutex and a reentrant?

For example, a function could be wrapped all around with a mutex (which avoids problems in multithreading environments), but if that function is used in an interrupt service routine, it could starve waiting for the first execution to release the mutex. The key for avoiding confusion is that reentrant refers to only one thread executing.

Which two functions are non-reentrant functions?

Here these two functions are non-reentrant. The first one is using one global variable, so it is non-reentrant. The second one is calling one non-reentrant function, so these also is not reentrant function. Now these two functions are reentrant functions.


3 Answers

Wikipedia has quite nice article on re-entrancy.

Function is called reentrant if it can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution

What makes one function not being re-entrant? Check the article further, but roughly:

  • Do not use static or global variables in your function since those may be changed by time your function resumes

  • Function must not modify its own code (e.g. some low level graphic routines may have "habit" to generate itself)

  • Do not call any function that does not comply with the two rules above

  • When to use re-entrant function? Here are some examples:

    • Functions executed in interrupt context must be re-entrant.

    • Functions that will be called from multiple threads/tasks must be re-entrant.

like image 186
Ivan Angelov Avatar answered Oct 22 '22 04:10

Ivan Angelov


A re-entrant function is one that can be interrupted (typically during thread context-switching), and re-entered by another thread without any ill-effect.

Functions that rely on a local variable are considered re-entrant due to the fact that their variables are safely encapsulated between threads.

Consider the case of a non-re-entrant function that uses a global variable.

Thread A is in function X, and incrementing a global variable. Thread B interrupts Thread A, enters function X and also increments the same global variable.

Thread A's behaviour and state has been changed (most likely incorrectly) by another thread and therefore the function it was in cannot be considered re-entrant.

like image 30
14 revs, 12 users 16% Avatar answered Oct 22 '22 06:10

14 revs, 12 users 16%


Reentrancy is applicable in concurrent programming. A reentrant function guarantees it’s functionality even when the function is invoked (reentered) from concurrent multiple threads. Also have look on what is reentrant function?

like image 22
Mohan Avatar answered Oct 22 '22 05:10

Mohan