The salient difference between my test program and yours is that
I didn’t “background” the subprocess with an ampersand. While I
must say that probably fits the description of a “daemon” process
best, the ampersand kind of makes the example trivial with respect
to the fgets(), because the direct subprocess of the top program
then of course exits immediately, while its child continues.
What I found interesting (and this is what the “it actually doesn’t”
was about) was that if a non-ampersanded subprocess like your daemon
(but without the procmgr_daemon() call) closes its stdout and then
sleeps, the Linux implementation of fgets() returns immediately,
while the Neutrino one doesn’t, in either 6.2.1B or 6.3.0. My
feeling is that Neutrino should do this too.
dB
John Garvey wrote ~ Thu, 24 Jun 2004 11:51:00 +1200:
David Bacon wrote:
Yes, this happens in Linux too, although in Linux if the daemon
takes the trouble to close its standard output before sleeping or
whatever, the fgets will unblock. In both QNX and Linux, however,
blocking will occur on the pclose until the daemon exits, even if
there has been no attempt to read anything from it.
I have to admit I prefer the Linux behaviour on the fgets. (John
Garvey’s message has just shown up, and I think he must also
prefer it, as he seems to assume that’s what happens in Neutrino
too, though it actually doesn’t!)
He did only say he wanted to unblock the fgets(), which is what closing
the write end of the pipe will do (or are you trying to say it
doesn’t?!). Yes, plcose() will do a waitpid(), which is blocking (but
at this point we are beyond the fgets (the original question); you can
also stack popen calls, so there is no/little harm to not pclose() this
one instantiation if necessary). Then it also depends on what the
“daemon” does; if it makes a suitable procmgr_daemon() call (eg, without
PROCMGR_DAEMON_NOCLOSE) then that will both unblock the fgets() and the
waitpid() of pclose. I tested QNX6.3 with this example below (where
popen exits immediately, not after 30 seconds). So I fail to see what
you are saying or assuming or what “it actually doesn’t” means?
— popen.c —
int main(int argc, char *argv[])
{
FILE *file;
int rc;
char out[128];
file = popen("/home/jgarvey/testcode/daemon &",“r”);
while (fgets(out,sizeof(out),file) != NULL) {
printf("%s", out);
}
rc = pclose(file);
printf(“result = %04x\n”, rc);
return(0);
}
— daemon.c —
int main(int argc, char *argv[])
{
procmgr_daemon(0, 0);
sleep(30);
return(0);
}