Re: Signal Handlers - sort of mystified by this



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 ******************
.



Relevant Pages

  • Signal Handlers - sort of mystified by this
    ... Why can't printf() be used inside a signal handler? ... 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" ... The Sockets API", they use POSIX standards, ...
    (comp.unix.programmer)
  • Re: printf and mutex
    ... thread safe, and that it can cause problem when used in a signal ... you cannot use printf in a signal handler. ... to opinions held by my employer, Sun Microsystems. ...
    (comp.programming.threads)
  • Re: RT Signal Handling in Linux in a non-blocking way?
    ... > Using printf in a signal handler is unsafe. ... Why not tell the _reason_ that printf might cause trouble? ... it might allocate memory for a buffer, ... So call setbuf, ...
    (comp.os.linux.development.apps)
  • Re: Signal Handlers - sort of mystified by this
    ... > Why can't printf() be used inside a signal handler? ... > 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: ...
    (comp.unix.programmer)