Strange SUN behaviour.

Paul Johnson yh87 at mrcu
Wed Oct 18 19:49:55 AEST 1989


A couple of weeks ago I posted a notice about the death of the parent
shell when certain programs exited.  Here is a summary of the mailed
responses.  Thanks to Steve Nuchia of Houston Public Access, Seth
Robertson of Columbia University, Larry Allen of the OSF, Casper Dik
of the University of Amsterdam and David DiGiacomo of Sun
Microsystems.  To anyone else who has sent a reply before reading
this, thank-you for your time.  The problem is now solved.

My program was using fcntl(F_SETFL,...) to set the FNDELAY flag on
descriptor 0 (non-blocking mode).  In this mode, if there are no
characters waiting to be read, a read() call returns 0 or -1
(depending on your version of Unix) instead of
blocking.  This is what I had intended.  What I had not intended was
that this should affect the parent shell.  When my program terminated
abnormally (say from a kill -9), it left this flag set.  I had assumed
that something like this would be local to the process which set the
flag.  The man page on fcntl says something about affecting all
descriptors which point to that device, but I thought it meant only
those descriptors in the same process.  I still think this is
something of a mis-feature in Unix.

The solution suggested by Steve Nuchia was to include a fork in my
program.  The child would execute the main area of the program and the
parent would wait for the child to terminate and then reset the
descriptor.  I did not like this, but had I been desparate I would
probably have done it.

Seth Robertson mentioned that ksh gets around the problem and
described a program called "background" which seems to implement Steve
Nuchia's solution (or something similar).

Larry Allen agreed with me that this is a bug in Unix, and remarked
that it would be a win to fix the csh.

The solution I adopted was to abandon the use of non-blocking mode.
Instead I use select() to determine if the descriptor has any
characters waiting and then read them one by one.  The only problem I
can see with this is the possibility of race conditions if two such
programs are trying to read from the same terminal: I think both would
get a SIGIO, both would be told by select() that characters are
available, and then one would get the character and the other would
block.  However I am not too worried by this.  Another solution I
considered was to set/reset FNDELAY around every call to read().  I
may yet do this.

Paul.

-- 
-----------------------------------------------------------------------------
Send replies to paj at uk.co.gec-rl-hrc, not the address above.  Thanks.
-----------------------------------------------------------------------------
GEC is not responsible for my opinions (says my shrink).



More information about the Comp.unix.wizards mailing list