LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2004, 06:28 AM   #1
xailer
Member
 
Registered: Nov 2003
Posts: 77

Rep: Reputation: 15
Re-entrant functions


hi

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 ?

thank you

bye
 
Old 01-02-2004, 08:23 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
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.
 
Old 01-02-2004, 08:35 AM   #3
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
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) ?

thank you
 
Old 01-02-2004, 09:18 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.

Last edited by Hko; 01-02-2004 at 09:20 AM.
 
Old 01-02-2004, 10:44 AM   #5
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
But what do non re-entrant functions have ( or don't have) which makes them unsafe ?

And thank you very much for your post

bye
 
Old 01-02-2004, 11:52 AM   #6
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
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.
 
Old 01-02-2004, 06:09 PM   #7
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
thank you both for your help

bye
 
Old 01-02-2004, 07:55 PM   #8
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
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).
 
Old 01-03-2004, 07:07 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
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.
 
Old 01-03-2004, 04:02 PM   #10
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
hmm, thank you. learnt something there.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
g++ functions apreda07 Programming 4 07-22-2004 12:40 AM
overload functions in C? jpbarto Programming 14 05-20-2004 02:26 PM
help with functions starla827 Programming 1 04-19-2004 02:57 AM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration