Subject: Use of signal() Considered Harmful

From: Shlomi Fish <[email-masked]>
Date: Fri, 1 Jun 2001 05:37:11 +0300 (IDT)

Those who did not attend Guy Keren's Second Gtk+ Programming Lecture
(given to Haifux) missed a couple of really good jokes.

At one time, Guy presented a code demonstrating the use of GtkLabel.
(check http://linuxclub.il.eu.org/lectures/34+35/examples/gtk-label.c).
Now, since he had not covered buttons yet, he emulated the gtk main loop
by calling gtk_events_pending() and gtk_main_iteration() (two functions
that are not supposed to be used directly in gtk) and then set the label's
text after waiting for a while.

I asked him why he did not bind the label text setting routine to the
SIGUSR1 handler and sent the application a USR1 signal. So he said that
using signals resulted in an improperly written program. So I responded by
asking whether a program that opened the Gtk+ main loop was a properly
written program. And he said: "It is a properly written program because it
does not use signal()..."

The fact is that my Freecell Solver executable accepts some SIGUSR1 and
SIGUSR2 combinations on platforms that support it, so I don't find any
reason not to use them. (Making your application dependant on them is
something different, because Windows, for example, supports only a small
subset of UNIX' signals).

What is your opinion regarding this matter? Do you approve of binding
signal handlers to signals if it suits your application?

BTW, would it be possible that calling gtk_label_set_text from within a
signal handler would have confused Gtk+ or Xlib?

Regards,

	Shlomi Fish

----------------------------------------------------------------------
Shlomi Fish        [email-masked] 
Home Page:         http://t2.technion.ac.il/~shlomif/
Home E-mail:       [email-masked]

A more experienced programmer does not make less bugs. He just realizes
what went wrong more quickly.

---

From: mulix <[email-masked]>
Date: Fri, 1 Jun 2001 11:08:01 +0300 (IDT)

> signal handlers to signals if it suits your application?

you might be interested in the paper "delivering signals for fun and
profit", presented on bugtraq last week and available at
http://razor.bindview.com/publish/papers/signals.txt

i think the conclusion evident from the paper is that you should only
use signals if you have to, and NEVER do anything non trivial from the
signal handler. in other words, set a flag and deal with it elsewhere.

[ p.s. the paper whose name this one resembles "smashing the stack for
fun and profit", by aleph1, is also highly recommended if you have't
read it yet ]
-- 
mulix
http://www.advogato.com/person/mulix

linux/reboot.h: #define LINUX_REBOOT_MAGIC1 0xfee1dead