gotos

Chris Torek chris at mimsy.UUCP
Sat Apr 23 08:47:22 AEST 1988


>In article <2200 at louie.udel.EDU> new at udel.EDU (Darren New) writes:
[pseudocode with `goto handle_key']

In article <1075 at maynard.BSW.COM> campbell at maynard.BSW.COM (Larry Campbell)
suggests:

>Use a signal handler.

[code deleted]

Remember also to fcntl(fd, FSETFL, FASYNC) or ioctl(fd, FIOASYNC, &on),
or you will never get a signal.

>This is not only much clearer and easier to read, it's also considerably
>faster.  And doesn't use gotos.

It may be clearer and easier to read, but it is also more dangerous.
Doing I/O in a signal handler is considerably harder than polling for
input at `safe' places.  If the input is to do almost anything useful,
you have created a critical section, which requires blocking and
releasing signals.  The trivial if unclear `goto' variant has been
replaced by a more powerful, more efficient (perhaps*), but much harder
to code correctly, asynchronous variant.

-----
*If the signal blocking and releasing has much overhead, it may be
less efficient than the polling variant.  Fortunately one can cheat
and move the work into the io signal handler, using semaphore variables.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list