Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84 SMI; site sun.uucp Path: utzoo!linus!philabs!prls!amdimage!amdcad!decwrl!sun!guy From: guy@sun.uucp (Guy Harris) Newsgroups: net.unix-wizards Subject: Re: strange behaviour with background process. Message-ID: <2507@sun.uucp> Date: Sun, 28-Jul-85 00:37:21 EDT Article-I.D.: sun.2507 Posted: Sun Jul 28 00:37:21 1985 Date-Received: Tue, 30-Jul-85 04:50:13 EDT References: <114@myab.UUCP> Organization: Sun Microsystems, Inc. Lines: 41 > I am running on SVR2 and tried to make the following program to work. > ...whenever I press the interrupt key to stop some program, the interrupt > is caught by this program. ... > > Is this a bug or a feature ? > > The program: ... > main() { > if (fork()) return; > signal(SIGINT, interr); > while (1) { > sleep(10); > printf("done\n"); > } > } A feature. You told your program to catch the interrupt signal; why the surprise when it does? Running something in the background does not disconnect it from the terminal, unless you're running with a shell that supports job control (like the 4.xBSD C shell, the Gwyn/Natalie-modified Bourne shell, or the Korn shell). Processes run in the background are not affected by keyboard-generated interrupt (or quit) signals because the shell ignores those signals before running the program, not because those signals aren't sent to the process. The correct code is: int (*oldsig)(); oldsig = signal(SIGINT, SIG_IGN); if (oldsig != SIG_IGN) signal(SIGINT, interr); This code first tests whether SIGINT is being ignored, by getting the current value of the signal. If it is not being ignored, it then catches it. If this program is run in the foreground, SIGINT will not be ignored and it will catch it. If it is run in the background, SIGINT will be ignored and it will leave it that way. Guy Harris