[net.unix-wizards] HELP - Background jobs in csh

gda@creare.uucp (gda) (04/05/85)

	Can anyone suggest a way for a program to know if it's running
	in background ? We have a program that is partly interactive,
	but when run in background we want it to skip the interactive
	part. We tried checking if STDIN was a tty, but that didn't help.
	We think it could call up "ps" and look for itself, but there must
	be an easier way...

	Thanks for your help (in advance).

						Gray Abbott
						Creare Inc.
						{...dartvax!creare!gda}

faustus@ucbcad.UUCP (04/08/85)

> 	Can anyone suggest a way for a program to know if it's running
> 	in background ? 

You can trap SIGTTOU signals and then try to write to the terminal...

	Wane

anton@ucbvax.ARPA (Jeff Anton) (04/08/85)

Background and foreground jobs are determined by process group.
A background process (job) will have a different process group
than the terminal.  You get the terminal process group with TIOCGPGRP
as described in tty(4).  Compare this with getpgrp(2).
-- 
C knows no bounds.
					Jeff Anton
					U.C.Berkeley
					ucbvax!anton
					anton@berkeley.ARPA

chris@umcp-cs.UUCP (Chris Torek) (04/11/85)

To find out whether your program is in the foreground:

	#include <sys/ioctl.h>

	foregroundp()
	{
		int tpgrp;	/* short in 4.1, int in 4.2 */

		if (ioctl(0, TIOCGPGRP, &tpgrp))
			return 0;
		return tpgrp == getpgrp(0);
	}

Unfortunately, the program's status can change between the ioctl
and the getpgrp.  If you don't mind races, the above is sufficient.
If you mind them, then you have to hold SIGTSTP, SIGTTIN, and
SIGTTOU until you know what you're going to do.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland