C shell question

Blair P. Houghton bph at buengc.BU.EDU
Sat Sep 23 20:47:48 AEST 1989


In article <233 at cmdfs2.intel.com> rpaul at sedona () writes:
>
>I am posting this question for a friend.  Does anyone know if it is possible
>for a process to figure out if it is running in the background vs.
>interactive?  All the "experts" at this site do not know.  If you have a way,
>please respond to 

Background and foreground are concepts related to the shell, not the
process, and not intrinsically to the unix OS.  I.e., the shell is the
_only_ one that "knows" the interactiveness of a job.

You have to get the program to "ask" the shell.

Any job put in the background has an entry in the 'jobs' listing;
so you'd think you could just match the process' pid with one
in the jobs-list.

The problem is in trying to communicate with the shell, which is the
_parent_ of the process with the question.  Job tables aren't
environment variables, and aren't exported.  You'd have to bust into
the memory space of the ppid process to get at it, provided you aren't
a process fork()'ed some levels deep in fork()'s (although a carefully
designed program would preserve the ppid of the process that ran
the top-level program).

The only way (I can think of) is to try some I/O, and catch the SIGTSTP
(SIGSTOP?) that the shell will raise (unless of course you've done
'stty -tostop'...  but doing a read from the console will certainly
make the shell try to stop the process).

Perhaps you could do some sort of hack to bg/fg involving aliasing and
setting of environment variables related to the pid of a backgrounded
process...

Not complete enough:  It won't be able to handle processes backgrounded
by the ampersand (&) on the command-line.

Maybe ksh can do it better.

				--Blair
				  "Say hi to marco, and tell
				   him I'm about to wipe his
				   tar's from my disks."



More information about the Comp.unix.questions mailing list