Unix Programming - Signal Handlers - sort of mystified by this

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2005 > Signal Handlers - sort of mystified by this





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Signal Handlers - sort of mystified by this
Chad

2005-12-30, 5: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

SM Ryan

2005-12-30, 5: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.
Nils O. Selåsdal

2005-12-30, 5: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.

Casper H.S. Dik

2005-12-30, 5: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.
Rich Teer

2005-12-30, 5: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 ******************
joe@invalid.address

2005-12-30, 5: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
Markus Elfring

2005-12-30, 5:55 pm

> Why can't printf() be used inside a signal handler?

This function does not belong to the list of async-signal-safe functions.
See section "2.4.3 Signal Actions" from the document " 2.4 Signal Concepts".
http://www.opengroup.org/onlinepubs...ml#tag_02_04_03

Regards,
Markus


Barry Margolin

2005-12-30, 5: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 ***
loic-dev@gmx.net

2005-12-30, 5: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...ml#tag_02_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.

joe@invalid.address

2005-12-30, 8:50 pm

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
SM Ryan

2005-12-31, 2:51 am

Rich Teer <rich.teer@rite-group.com> wrote:

# signal handler, but in a relatively simple single threaded example
# program, what can go wrong?

How do I debug thee?
Let me count the ways.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I love the smell of commerce in the morning.
Chad

2005-12-31, 2:51 am

SM Ryan wrote:
> Rich Teer <rich.teer@rite-group.com> wrote:
>
> # signal handler, but in a relatively simple single threaded example
> # program, what can go wrong?
>
> How do I debug thee?
> Let me count the ways.
>
> --
> SM Ryan http://www.rawbw.com/~wyrmwif/
> I love the smell of commerce in the morning.


I found an older thread that talks about "threads vs fork/exec" at
http://groups.google.com/group/comp...1c853e5e42d0472

What makes it interesting is that in the first half of the book "Unix
Network Programming: The Sockets API", the author(s) seem to be using
the fork/exec model.

Chad

Valentin Nechayev

2005-12-31, 2:51 am


Fri, Dec 30, 2005 at 06:46:29, cdalten (Chad) wrote about "Signal Handlers - sort of mystified by this":

C> Why can't printf() be used inside a signal handler? If printf() can't
C> be used inside a signal handler, then how come Dr. W Richard Stevens
C> does it on page 133 of book "Unix Network Programming: The Sockets API"
C> (3rd edition)

It was typical even in mid-1990s to misuse non-async-signal-safe
functions in signal handlers.

On the other hand, if signal is generated asyncronously, but
delivered synchronously (e.g. signal mask denies signal processing
except of specially allowed windows), functions allowed in signal
handler aren't limited to async-signal-safe set. Programmer shall
know whether the signal is allowed to be delivered asynchronously,
to determine allowed operations in signal handler.


-netch-
Bjorn Reese

2005-12-31, 7:49 am

Barry Margolin wrote:

> But unless the particular example is new to the 3rd edition, this is
> irrelevant.


The example is also present in the 2nd edition (e.g. page 122 and 128).

What is rather unfortunate about this example is that one of the
captions says:

"Final (correct) version of sig_chld function that calles waitpid"

Of course, "correct" refers to the use of waitpid() rather than
wait(), but that may not be obvious to everybody.

--
mail1dotstofanetdotdk
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com