473,666 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

query about printf

Hi Everyone,

I had a query reg printf(). I heard that it can't be used in ISR's
as it is non-re-enterant. I didn't get as to why
printf is non-re-enterant and why non-re-enterant library can't be
used in an ISR?

Thanks in advance ! ! !
Jun 27 '08 #1
10 4553
On 28 May 2008 at 12:27, Rahul wrote:
I had a query reg printf(). I heard that it can't be used in ISR's as
it is non-re-enterant. I didn't get as to why printf is
non-re-enterant and why non-re-enterant library can't be used in an
ISR?
I believe POSIX requires the functions that write to streams to be
implemented in a re-entrant way (certainly this will be true if you're
using threads)... In general I don't think the famous ANSI C Standard
(praise be upon it!) insists on it. It's easy to imagine an
implementation of printf() that used a static internal buffer to build
up the string before sending it to stdout, for example.

If you call a non-reentrant function from an ISR, what if the code you
interrupted was another call to the same function? You'd likely corrupt
both the interrupted function's data and the data in the function called
by your ISR.

In any case, calling printf() in an ISR is almost certainly a mistake,
reentrant or not. Latency considerations mean that you really don't want
to be calling big bloated functions like printf() inside an ISR - you
would be blocking further interrupts for far too long.

Jun 27 '08 #2
Antoninus Twink wrote:
On 28 May 2008 at 12:27, Rahul wrote:
>I had a query reg printf(). I heard that it can't be used in ISR's as
it is non-re-enterant. I didn't get as to why printf is
non-re-enterant and why non-re-enterant library can't be used in an
ISR?

I believe POSIX requires the functions that write to streams to be
implemented in a re-entrant way
<off-topicYou believe incorrectly. </off-topic>
(certainly this will be true if you're
using threads)...
<off-topicWrong again. </off-topic>
In general I don't think the famous ANSI C Standard
(praise be upon it!) insists on it.
In fact, there's an explicit warning to the contrary.
It's easy to imagine an
implementation of printf() that used a static internal buffer to build
up the string before sending it to stdout, for example.
The buffer needn't be static; its mere existence is
enough to doom re-entrancy.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #3
>
It's easy to imagine an
implementation of printf() that used a static internal buffer to build
up the string before sending it to stdout, for example.

The buffer needn't be static; its mere existence is
enough to doom re-entrancy.
What is the buffer is allocated on heap or declared as a local
varaible?
Jun 27 '08 #4
Rahul wrote:
>>It's easy to imagine an
implementatio n of printf() that used a static internal buffer to build
up the string before sending it to stdout, for example.
The buffer needn't be static; its mere existence is
enough to doom re-entrancy.

What is the buffer is allocated on heap or declared as a local
varaible?
(I guess you mean "what if.")

Wherever the buffer resides, characters destined for
output must pass through it. If it's statically allocated
or dynamically allocated, both the interrupted and the
interrupting activity need to deposit characters in it,
update a where-does-the-next-character-go indicator, and
decide whether to flush the contents. Unless all that can
be done uninterruptably , printf() is non-reentrant.

I hadn't considered an `auto' buffer, because it would
go out of existence when printf() returned to its caller.
It's possible that a printf() implementation could use such
a buffer, filling it during the call and draining it just
before the return. If it did so -- and if all the machinery
of draining the buffer to stdout was also re-entrant -- then
the worst that would happen would be that the output from
the interrupting printf() could appear in the middle of a
bunch of output from the interrupted printf():

Helloinvalid sense word 0xbadd
, world!

But that's an uncomfortably long chain of "ifs" to rely on,
especially since such a buffer is not the kind whose use is
suggested by the setbuf() and setvbuf() functions. (The
implementation need not "do anything" in response to those
calls, but their existence hints at a buffering regime that
uses longer-lived buffers.)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #5
Rahul <sa*****@yahoo. co.inwrote:
It's easy to imagine an
implementation of printf() that used a static internal buffer to build
up the string before sending it to stdout, for example.
The buffer needn't be static; its mere existence is
enough to doom re-entrancy.

What is the buffer is allocated on heap or declared as a local
varaible?
A non-static local variable shouldn't pose a problem. But
static (even local) variables aren't safe. If a call of
printf() gets interrupted by an ISR (or a signal or what-
ever asynchronous events there could be) and the handler
function itself uses printf() then a static buffer may
get overwritten by this new call and left in a state so
that it can't be used safely anymore by the original call
of printf() to which the program finally will get back.

But it's not only a problem with a static buffers.
printf() also could call malloc() and when, during the
execution of malloc() an asynchronous handler gets in-
voked that also calls malloc() or some related function
directly or indirectly, the whole memory allocation sys-
tem can get into a random state. Strange and nearly im-
possible to reproduce bugs would rather likely be the
result. So better check each and every function you use
in a handler for an asynchronous event that it is expli-
citely documented to be safe to use under such circum-
stances. That not means just thread-safety since in
threads the implementors can employ locking schemes etc.,
you need functions that are safe for use in signal hand-
lers. If a function isn't assume that it can't be used.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jun 27 '08 #6
On May 28, 6:52 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
Rahul wrote:
>It's easy to imagine an
implementati on of printf() that used a static internal buffer to build
up the string before sending it to stdout, for example.
The buffer needn't be static; its mere existence is
enough to doom re-entrancy.
What is the buffer is allocated on heap or declared as a local
varaible?

(I guess you mean "what if.")

Wherever the buffer resides, characters destined for
output must pass through it. If it's statically allocated
or dynamically allocated, both the interrupted and the
interrupting activity need to deposit characters in it,
update a where-does-the-next-character-go indicator, and
decide whether to flush the contents. Unless all that can
be done uninterruptably , printf() is non-reentrant.

I hadn't considered an `auto' buffer, because it would
go out of existence when printf() returned to its caller.
It's possible that a printf() implementation could use such
a buffer, filling it during the call and draining it just
before the return. If it did so -- and if all the machinery
of draining the buffer to stdout was also re-entrant -- then
the worst that would happen would be that the output from
the interrupting printf() could appear in the middle of a
bunch of output from the interrupted printf():

Helloinvalid sense word 0xbadd
, world!

But that's an uncomfortably long chain of "ifs" to rely on,
especially since such a buffer is not the kind whose use is
suggested by the setbuf() and setvbuf() functions. (The
implementation need not "do anything" in response to those
calls, but their existence hints at a buffering regime that
uses longer-lived buffers.)

--
Eric Sosman
esos...@ieee-dot-org.invalid
If i'm correct, the ISR runs as a part of the operating system (kernel
task) and other tasks might call printf function. Lets assume this
version of printf uses a static buffer or a global buffer. In any
case, wouldn't the printf get linked into both these tasks?
Wouldn't both these tasks have their own version of the static or
global variable?

If so, then i don't see any reason for conflict, (yes, it might be
a problem in multi-threaded task). Also that it wouldn't matter if
printf is implemented as a static or dynamic library?

Thanks in advance ! ! !
Jun 27 '08 #7
Rahul wrote:
>
If i'm correct, the ISR runs as a part of the operating system (kernel
task) and other tasks might call printf function. Lets assume this
version of printf uses a static buffer or a global buffer. In any
case, wouldn't the printf get linked into both these tasks?
Wouldn't both these tasks have their own version of the static or
global variable?
The situation is far outside the scope of the C language.
In C there is no such thing as an "ISR" and there is only one
"task." Your questions aren't about C, but about specific
environments in which C can be used. And the answers will
be different[*] in different environments: An ISR in a Linux
system will have different conventions and constraints than
an ISR in Windows or in OS/390 or in your cell phone, even
if all these ISRs are written in C.
[*] Well, maybe not. I predict, with a confidence level
exceeding ninety percent, that the answer to "Is it safe to
call printf() in an ISR" will be "No," even though the reasons
behind the "No" will differ.
If so, then i don't see any reason for conflict, (yes, it might be
a problem in multi-threaded task). Also that it wouldn't matter if
printf is implemented as a static or dynamic library?
C has no notion of a static library or a dynamic library.
All it knows about are functions and data, and about some kind
of "linkage" that pulls a bunch of them together into a running
program. If you need information about the details of how
different systems accomplish this, you are asking not about C
but about those systems.

--
Er*********@sun .com
Jun 27 '08 #8
[regarding reentrancy, or lack thereof, in printf implementations]

In article <MM************ *************** ***@comcast.com >,
Eric Sosman <es*****@ieee-dot-org.invalidwrot e:
Wherever the buffer resides, characters destined for
output must pass through it. If it's statically allocated
or dynamically allocated, both the interrupted and the
interrupting activity need to deposit characters in it,
update a where-does-the-next-character-go indicator, and
decide whether to flush the contents. Unless all that can
be done uninterruptably , printf() is non-reentrant.
In my stdio, fprintf() (and hence printf()) is indeed non-reentrant,
in general.
I hadn't considered an `auto' buffer, because it would
go out of existence when printf() returned to its caller.
It's possible that a printf() implementation could use such
a buffer, filling it during the call and draining it just
before the return.
In my stdio, fprintf() does exactly this under some circumstances,
as an efficiency hack. However, it remains non-reentrant. In
particular, calling (f)printf from a signal handler can result in
slightly odd behavior (exactly "how odd" depends on how much the
compiler rearranges the adjustment of various "FILE" elements,
which in turn depends on machine architecture -- normally the bad
cases occur when "fp->_w--" or "*fp->_p++" gets interrupted in the
middle of a load/adjust/store sequence, so the "nested" printf sees
an unadjusted value and winds up using the same position in the
buffer, so that you "lose" one character).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jun 27 '08 #9
On 28 May 2008 at 13:21, Eric Sosman wrote:
Antoninus Twink wrote:
>I believe POSIX requires the functions that write to streams to be
implemented in a re-entrant way

You believe incorrectly.
>(certainly this will be true if you're using threads)...

Wrong again.
Yeah, OK, s/reentrant/threadsafe/ I guess... And since this is such a
haunt of language lawyers, let's make clear that we're ignoring
functions that write to streams that are deliberately not threadsafe,
e.g. putc_unlocked() .

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3685
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example program #include <list>
14
3222
by: Bruce W...1 | last post by:
I do a query to MySQL using PHP. Well actually I do too many of them which is the problem, it's too slow. I think maybe an inner join or something would be better but I can't figure this out. The web page on which the data is displayed has a list of countries. Each country has a label heading under which websites are listed that have information on that country. Any given website may appear many times (under different countries) if it...
3
2643
by: IMS.Rushikesh | last post by:
Hi Friends, I need a XPath query, whcih ll return me subset of data.... check below xml stream <label id="MyExpenseDetails_lbl" xlink:role="terseLabel">Short Expense Details</label> <label id="MyExpenseDetails_lbl" xlink:role="displayLabel">Expense Details</label> <label id="InternalExpense_lbl" xlink:role="displayLabel">Internal
0
1383
by: krisan | last post by:
Hi, i am writing a program for ping operation which supports both Ipv4 and IpV6 ..first i wrote for both of them individually , but i am unable to mix them such that they support both the versions ... i would be very thankful if anyone could come out with a complete program .. Ipv6 prog --------- #include<stdio.h> #include<winsock2.h>
1
3580
by: Pradeep83 | last post by:
Hi All Problem : I am unable to retrieve the data from the table in postgres database using C application which i have written in solaris os. Query: How to check whether connection is there between postgres database and C application(which we have written for accessing the database) ? My code is comiling successfully if i run it then its giving o/p as following : output
18
1621
by: newbai | last post by:
printf("%d",(float)3/2); the output comes as zero...why not 1?even when u change it to printf("%d",(float)6/2); still the output is zero
13
5759
by: ptq2238 | last post by:
Hi, I have written this code to help me learn C but I'm not sure why gcc - Wall is giving me an error when I compile Basically I want to read in a character then a number and then manipulate the number. I've tried scanf such as scanf("%c%d",&letter,&number); but when I type in say 2 letters instead of 1 letter and 1 number, I get a zero for the 2nd value as it doesn't match what scanf is expecting so I'm
3
1697
by: kiritved | last post by:
hi, #include<stdio.h> #include<conio.h> int main() { int i,j,k; clrscr(); k=0; k++; i=0;
36
3795
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used to read text or handle arrays of arbitrary length. I don't have the expertise in C of many folks here so I feel like I'm offering a small furry animal for sacrifice to a big armour plated one... but will offer it anyway. Please do suggest...
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8352
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7378
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6189
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.