 |
|
 |
|
|
 |
Signal Handlers - sort of mystified by this |
 |
 |
|
|
12-30-05 10:55 PM
This question stems from a comment made on comp.lang.c
The thread is at the following:
http://groups.google.com/group/comp...c618fb0448b7643
Why can't printf() be used inside a signal handler? If printf() can't
be used inside a signal handler, then how come Dr. W Richard Stevens
does it on page 133 of book "Unix Network Programming: The Sockets API"
(3rd edition)
Going on. Is this by any chance related to the fact that in the book
Unix Network Programming: The Sockets API", they use POSIX standards,
while on comp.lang.c, they use ANSI standards?
Thanks in advance
Chad
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Signal Handlers - sort of mystified by this |
 |
 |
|
|
12-30-05 10:55 PM
# Why can't printf() be used inside a signal handler? If printf() can't
Because stdio is allowed to be non-rentrant.
# be used inside a signal handler, then how come Dr. W Richard Stevens
# does it on page 133 of book "Unix Network Programming: The Sockets API"
# (3rd edition)
If the signal doesn't occur while running stdio or anything
nonretrant it depends on, it is safe. Otherwise it's a signal
handler waiting to fail.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Mention something out of a Charleton Heston movie, and suddenly
everybody's a theology scholar.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Signal Handlers - sort of mystified by this |
 |
 |
|
|
12-30-05 10:55 PM
SM Ryan wrote:
> # Why can't printf() be used inside a signal handler? If printf() can't
>
> Because stdio is allowed to be non-rentrant.
It's not just reentrancy, but async-safety too.
> # be used inside a signal handler, then how come Dr. W Richard Stevens
> # does it on page 133 of book "Unix Network Programming: The Sockets API"
> # (3rd edition)
>
> If the signal doesn't occur while running stdio or anything
> nonretrant it depends on, it is safe. Otherwise it's a signal
> handler waiting to fail.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Signal Handlers - sort of mystified by this |
 |
 |
|
|
12-30-05 10:55 PM
"Chad" <cdalten@gmail.com> writes:
>Why can't printf() be used inside a signal handler? If printf() can't
>be used inside a signal handler, then how come Dr. W Richard Stevens
>does it on page 133 of book "Unix Network Programming: The Sockets API"
>(3rd edition)
Because printf() manipulates global state (may call malloc(), update
FILE *s, etc) and a signal may arrive while in printf() and starting
a second printf at that point may confuse things.
If Stevens does something which is wrong, it is still wrong.
(It will work most of the time, but not all of the time; and it's
the "all of the time" which is important because next thing that
will happen is that you have a 100,000 line C program and it core dumps)
>Going on. Is this by any chance related to the fact that in the book
>Unix Network Programming: The Sockets API", they use POSIX standards,
>while on comp.lang.c, they use ANSI standards?
No. It's a bug in Stevens.
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Signal Handlers - sort of mystified by this |
 |
 |
|
|
12-30-05 10:55 PM
On Fri, 30 Dec 2005, Chad wrote:
> Why can't printf() be used inside a signal handler? If printf() can't
Because printf and many other functions aren't reenterant and/or may
alter global state.
> be used inside a signal handler, then how come Dr. W Richard Stevens
> does it on page 133 of book "Unix Network Programming: The Sockets API"
> (3rd edition)
Sometimes when writing example programs, making them 100% technically
correct, complete with all error checking, can obfuscate the point the
example is tryng to illustrate. When you know what you're doing, then
it is possible to bend the rules. Yes, printf shouldn't be used in a
signal handler, but in a relatively simple single threaded example
program, what can go wrong?
The trick is to be aware of the technicalities, and apply them when
necessary.
> Going on. Is this by any chance related to the fact that in the book
> Unix Network Programming: The Sockets API", they use POSIX standards,
> while on comp.lang.c, they use ANSI standards?
Nope.
--
Rich Teer, SCNA, SCSA, OpenSolaris CAB member
. * * . * .* .
. * . .*
President, * . . /\ ( . . *
Rite Online Inc. . . / .\ . * .
.*. / * \ . .
. /* o \ .
Voice: +1 (250) 979-1638 * '''||''' .
URL: http://www.rite-group.com/rich ******************
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Signal Handlers - sort of mystified by this |
 |
 |
|
|
12-30-05 10:55 PM
"Chad" <cdalten@gmail.com> writes:
> Why can't printf() be used inside a signal handler? If printf()
> can't be used inside a signal handler, then how come Dr. W Richard
> Stevens does it on page 133 of book "Unix Network Programming: The
> Sockets API" (3rd edition)
Just a comment in addition to what others have said. The third edition
wasn't done by Stevens, he passed away some years ago. His name is on
the book because the authors were modifying the second edition, which
he did write.
Joe
--
Gort, klatu barada nikto
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: Signal Handlers - sort of mystified by this |
 |
 |
|
|
 |
|
 |
|
|
 |
Re: Signal Handlers - sort of mystified by this |
 |
 |
|
|
12-30-05 10:55 PM
In article <m31wzutrsq.fsf@invalid.address>, joe@invalid.address wrote:
> "Chad" <cdalten@gmail.com> writes:
>
>
> Just a comment in addition to what others have said. The third edition
> wasn't done by Stevens, he passed away some years ago. His name is on
> the book because the authors were modifying the second edition, which
> he did write.
But unless the particular example is new to the 3rd edition, this is
irrelevant.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Signal Handlers - sort of mystified by this |
 |
 |
|
|
12-30-05 10:55 PM
Hi Chad,
> Why can't printf() be used inside a signal handler? If printf() can't
> be used inside a signal handler, then how come Dr. W Richard Stevens
> does it on page 133 of book "Unix Network Programming: The Sockets API"
> (3rd edition)
As pointed out by a poster, the 3rd edition has not been written by
Stevens, which passed away in 1999, but by Fenner & Rudoff.
Note that on page 133, it is mentionned:
<copy>
Warning: Calling standard I/O functions such as printf() in signal
handler is not recommended, for reasons that we will discussed in
Section 11.18.
</copy>
Actually, the rules are:
1) It is safe to call any async-signal-safe function in signal handler.
The list of async-signal-safe function can be found at the following
URL:
http://www.opengroup.org/onlinepubs...2_
04_03
2) Troubles may occur when a non async-signal safe function has been
interrupted by a signal, and is called again in the signal handler. For
instance, if you were right in the middle of a printf() when your
signal handler is executed, and this signal handler call printf()
again, then you might run into big troubles.
HTH,
Loic.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Signal Handlers - sort of mystified by this |
 |
 |
|
|
12-31-05 01:50 AM
Barry Margolin <barmar@alum.mit.edu> writes:
> In article <m31wzutrsq.fsf@invalid.address>, joe@invalid.address wrote:
>
>
> But unless the particular example is new to the 3rd edition, this is
> irrelevant.
Which is why it was "just a comment".
Joe
--
Gort, klatu barada nikto
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 09:27 PM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|