Path: utzoo!mnetor!uunet!husc6!rutgers!clyde!cbosgd!mandrill!hal!ncoast!allbery From: allbery@ncoast.UUCP (Brandon Allbery) Newsgroups: comp.unix.wizards Subject: Re: signal handling,c-shell,hp840 Message-ID: <6812@ncoast.UUCP> Date: 17 Dec 87 22:50:43 GMT References: <7649@eddie.MIT.EDU> Reply-To: allbery@ncoast.UUCP (Brandon Allbery) Followup-To: comp.unix.wizards Organization: Cleveland Public Access UN*X, Cleveland, Oh Lines: 38 As quoted from <7649@eddie.MIT.EDU> by eric@eddie.MIT.EDU (Eric Van Tassell): +--------------- | in other words foo.csh merely calls foo. Why does the parent c-shell catch | the ^C and terminate foo instead of letting mysighndlr handle it? How do I | enable my application to handle the signal? Please email. Oh yeah the system | involved is an HP9000/840 running HP-UX. +--------------- I'm posting because it's a generic problem -- all shells have it, including the V7 Bourne shell -- and because it puzzled me until I figured it out, back when I was just starting out. The reason for the behavior is that the Unix tty driver sends a SIGINT when it receives your (bsd? t_intrc: c_cc[VINTR]) -- sends it to *all* your processes, not just the "current" process. (Having authored a "w" for System V Release 2, I've had it quite forcefully driven home that Unix doesn't *have* the notion of a "current" process. This is a boon to process managing, but a bust for status monitors.) Since your "csh" is a process, it also receives the SIGINT, which aborts the wait() done by csh and causes the csh to exit. Your program ends up orphaned in the background, and probably dies due to parental neglect. ;-) The way to prevent this is to add a statement to your csh script before it runs "foo": "onintr -", which tells csh to ignore SIGINT. However, your program must then trap SIGINT regarless of whether it is being ignored or not -- a cardinal sin of signal handling, according to all my Unix programming books. For the Bourne shell, the proper statement is "trap '' 2"; actually, the Bourne shell can do better with some fancy footwork: "trap 'trap : 2' 2" will prevent /bin/sh from processing the signal without passing a SIG_IGN on to the application. (This is one reason why I prefer /bin/sh or ksh to csh.) I personally think that shells should have a way of ignoring signals for the duration of a command executing in the foreground. However, I don't write shells (well, not *those* shells...) so my opinion is worthless. -- Brandon S. Allbery necntc!ncoast!allbery@harvard.harvard.edu {hoptoad,harvard!necntc,cbosgd,sun!mandrill!hal,uunet!hnsurg3}!ncoast!allbery Moderator of comp.sources.misc