Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!bu-cs!buengc!bph From: bph@buengc.BU.EDU (Blair P. Houghton) Newsgroups: comp.unix.questions Subject: Re: C shell question Message-ID: <4331@buengc.BU.EDU> Date: 23 Sep 89 10:47:48 GMT References: <233@cmdfs2.intel.com> Reply-To: bph@buengc.bu.edu (Blair P. Houghton) Followup-To: comp.unix.questions Distribution: usa Organization: Boston Univ. Col. of Eng. Lines: 43 In article <233@cmdfs2.intel.com> rpaul@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."