ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Re-entrant means that function can be interrupted in the middle and called again by something else ? And then it can come back to the first call at the place where it was interrupted?
This is confusing , since I don't really see any significant difference between re-entrant function and recursive function . Why can any function be recursive ,but only few can be re-entrant ?
Why can any function be recursive ,but only few can be re-entrant ?
You never know when an interrupt comes. It could be really in the middle of a printf() call, leaving it unfinished, with only half of the data written, or none yet. Am interrupt can even occur inside a statement like:
Code:
double q = q + 1.0
leaving q partly updated with the new value. If q is a static or global variable, if the function is called another time while the first time it was interrupted while updating q in memory, then q will have an undefined value. So using q in calculations will produce wrong values.
Is that the only reason why some functions are considered unsafe in regards with re-entrance ? Because they operate on values that are either static or global ? Or they have statements that could in effect produce wrong results if interrupted (similar to example you provided) ?
As far as I know, it doesn't affect local var's. After the interrupt is handled, the function resumes exactly where it was interrupted. So the interrupted call will finish correctly. But a reentrant function should not do or change things that can affect things in other parts of the program, or the same function when it is called a second time while the first call is not yet finished. Most common "dangerous" things are changing global var's and i/o operations (like writing to a file). But also things like writing to shared memory and calling malloc() (not sure about the last one).
Changing global var's can often be handy for flagging that an interrupt arrived. This is OK if writing to the global var is done "atomically", which means that updating the var is done by one single machine (assembly) instruction, because one such instruction will not be interrupted half way, i.e. the value is assigned or not, but never half of it.
In Linux writing to a int is "atomical", so writing to a global int is save. But on other systems an int may not be good enough. To be portable you should only use globals of the type sig_atomic_t which, in Linux, will be an int. But on another system it maybe "unsigned char" for example.
The exact meaning of "re-entrant function" depends on the context. In application programming, the term usually refers to a function which can be called from different threads of execution safely. This either means it doesn't modify global variables, or that it modifies them in a safe way (i.e. it checks that they are not being modified by another call of the function at that time).
Alex
Last edited by llama_meme; 01-02-2004 at 11:53 AM.
stupid question: what languages support this concept of re-entrant functions?
another stupid question: if a lot of processes keep calling a "re-entrant" function, would running out of stack space happen? something like buffer overflow?
first time im hearing of this (either this wasnt taught, or i musta fallen asleep when it was).
Originally posted by h/w stupid question: what languages support this concept of re-entrant functions?
Every language that support multi-threading or signal-handling should. As llama_meme pointed out "thread safe" has a lot in common with "re-entrant".
Quote:
another stupid question: if a lot of processes keep calling a "re-entrant" function, would running out of stack space happen? something like buffer overflow?
I imagine that running out of stack could happen, but that can also happen in deep recursion. Anyways, running out of stack space is not the same as buffer overflow.
Running out of stack space is running out of stack space.
And buffer overflow is writing past stack space.
A buffer overflow can happen when there still a lot of stack space left.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.