Command substitution and daemons - Request for help

Mark Rosenthal mbr at aoa.UUCP
Fri Aug 23 09:26:17 AEST 1985


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 <stdio.h>

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 <exiting>.  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



More information about the Comp.unix.wizards mailing list