Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/18/84; site aoa.UUCP
Path: utzoo!watmath!clyde!bonnie!akgua!whuxlm!harpo!decvax!genrad!panda!talcott!harvard!bbnccv!bbncca!aoa!mbr
From: mbr@aoa.UUCP (Mark Rosenthal)
Newsgroups: net.unix-wizards
Subject: Command substitution and daemons - Request for help
Message-ID: <257@aoa.UUCP>
Date: Thu, 22-Aug-85 19:26:17 EDT
Article-I.D.: aoa.257
Posted: Thu Aug 22 19:26:17 1985
Date-Received: Sun, 25-Aug-85 02:25:45 EDT
Distribution: net
Organization: Adaptive Optics Assoc., Cambridge, Mass. USA
Lines: 97

Although our site has been receiving articles for the past few months,
articles posted to the net have not been getting distributed until recently.
The following is a reposting of an article I sent out quite some time ago.
I believe it never made it to the net at large.  Apologies if you have seen
this before.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


I am having trouble using comand substitution from either /bin/sh or csh.
I can find no mention in the man pages for either shell of the interaction
of `` and programs that place themselves in the background.

I am trying to create a set of C programs which can be called from a shell
script.  There is a daemon ('dmn') which controls a device, and a user
interface ('ifc') which sends commands to 'dmn' via a queue.  I want 'dmn'
to be able to put the queue identifier into the environment where
'ifc' can find it.  In other words, the script should look like:

	setenv ID `dmn`
	    or
	ID=`dmn`; export ID
	 .
	 .
	 .
	ifc

'ifc' is simply coded to look in the environment variable ID.

The following code is the relevant section of 'dmn'.  Note that it writes
the queue identifier to stdout, before it backgrounds itself.

- - - - - - - - - - - - - - - - - -beg dmn- - - - - - - - - - - - - - - - - - -
#include 

main()
{
    printf("01\n");
    fflush(stdout);

    background();

    fprintf(stderr, "Background pid = %d\n", getpid());
    fflush(stderr);

    for (;;)		/* Simulate computation in the background */
	{ }
}

background()
{
    int pid;

    pid = fork();
    if (pid < 0)	/* fork failed */
    {
	fprintf(stderr, "background(): Fork failed\n", 0);
	fflush(stderr);
	exit(-1);
    }
    if (pid > 0)	/* parent exits */
    {
	fprintf(stderr, "Foreground exiting, pid = %d\n", getpid());
	fflush(stderr);
	exit(0);
    }
    /* child gets inherited by init */
}
- - - - - - - - - - - - - - - - - -end dmn- - - - - - - - - - - - - - - - - - -

Now for the problem.  When I run 'dmn' from either shell as:

	% dmn

it operates as expected.  It outputs "01" and leaves a process running
whose parent is init (process 1).  The foreground process is gone.
However when I try:

	% setenv ID `dmn`
	    or
	$ ID=`dmn`
	    or
	% echo `dmn`
	    etc.

it hangs.  Running 'ps' shows the background process with init as its parent
(as before), but now the foreground process shows as .  Anybody
know what the shells are waiting for?  Is the problem that they think they're
dealing with a pipeline?  I don't have source available to check for myself.

Additional (possibly relevant) info: this is on a Masscomp MC500, which
runs Sys V w/some 4.2 enhancements.  Please send mail.  When responses
dwindle, I'll post whatever answer(s) appear to be correct.

-- 

	Mark of the Valley of Roses
	...!{decvax,linus,ima,ihnp4}!bbncca!aoa!mbr