read()'ing from a child's piped stdout 
Author Message
 read()'ing from a child's piped stdout

Hi all.

 I hope this question doesn't get disregarded as my earlier question today
did, simply because exec is "non standard"... I thought it was for some
reason. Oh well. Anyway, I have a parent which uses pipe to associate two
fileno's. It then forks a child who dup2's it's stdout to one of the
filenos. The parent then read()s on the other piped fileno, and basically
gets what it should as the child generates output. However, once the child
is done, read() seems to block when I call it. And my program hangs. I've
explicitly closed the fileno I dup2'd in the child. I've also tryed fcntl
to set O_NONBLOCK and O_NDELAY (I've tried this in the parent process,
should I do this in the child???) I'm at wits end. Why won't read give me
a 0 back to indicate eof??? What am I doing wrong?

--
Jason Simpson

  "And, of course, you have the commercials where savvy businesspeople  Get  Ahead  by using their MacIntosh computers to create the ultimate American  business  product: a really sharp-looking report." -- Dave Barry



Wed, 28 Jul 2004 08:35:04 GMT  
 read()'ing from a child's piped stdout

On Fri, 8 Feb 2002 at 16:35, Jason Simpson so generously added:

Quote:
> should I do this in the child???) I'm at wits end. Why won't read give me
> a 0 back to indicate eof??? What am I doing wrong?

It occurs to me that perhaops I should post a sample:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#define BUFFER_SIZE 10000
int main(int argc, char** argv)
{
    int fdout[2];
    int forkoutsize;    /* Size of buffer read from forked fd */
    int resultPid;      /* PID of fork */
    int status;
    char *buffer;
    if(pipe(fdout) > 0) {
        printf("pipe() failed...\n");
    }
    resultPid = fork();
    if(resultPid < 0)
    {
        /* Some error. Clean up */
        printf("fork() returned < 0...\n");
        close(fdout[0]);
    }
    if(resultPid == 0)
    {
        dup2(fdout[1], 1);
        system("ls");
        close(fdout[0]);
        close(fdout[1]);
        _exit(0);
    }
    printf("Parent: while\n");
    while((forkoutsize = read(fdout[0], buffer, BUFFER_SIZE)) > 0)
    {
        printf("Read: %s\n", buffer);
        printf("Size: %d\n", forkoutsize);
    }
    printf("Parent: closing\n");
    close(fdout[0]);
    close(fdout[1]);
    return 0;

Quote:
}

--
Jason Simpson

  "And, of course, you have the commercials where savvy businesspeople  Get  Ahead  by using their MacIntosh computers to create the ultimate American  business  product: a really sharp-looking report." -- Dave Barry



Wed, 28 Jul 2004 09:47:23 GMT  
 read()'ing from a child's piped stdout
On Fri, 8 Feb 2002 17:47:23 -0800, Jason Simpson

Quote:

> On Fri, 8 Feb 2002 at 16:35, Jason Simpson so generously added:

> > should I do this in the child???) I'm at wits end. Why won't read give me
> > a 0 back to indicate eof??? What am I doing wrong?

> It occurs to me that perhaops I should post a sample:

> #include <sys/types.h>

There is no C language header named sys/types.h

Quote:
> #include <sys/socket.h>

There is no C language header named sys/socket.h

Quote:
> #include <netinet/in.h>

There is no C language header named netinet/in.h

Quote:
> #include <stdlib.h>

Hurray, you actually included one C language standard header!!!  There
really is a stdlib.h header in C!!!

Quote:
> #include <fcntl.h>

There is no C language header named fcntl.h

Quote:
> #define BUFFER_SIZE 10000
> int main(int argc, char** argv)
> {
>     int fdout[2];
>     int forkoutsize;    /* Size of buffer read from forked fd */
>     int resultPid;      /* PID of fork */
>     int status;
>     char *buffer;
>     if(pipe(fdout) > 0) {

There is no C language function named pipe.

        [much more non-standard, platform specific code snipped]

Apparently you didn't read the entire context of Ben's extremely long
reply:

Quote:
> This is all system-dependent.  (In fact, exec() is not a standard
> function.)  You should ask it in a newsgroup that talks about
> your particular environment, possibly comp.unix.programmer.

So let's try again.  This newsgroup discusses the C language as
defined by the platform independent ANSI/ISO standard (in fact, the
very document that defines the C language).

Unix specific extensions are off-topic here.  As are Windows specific
extensions, Macintosh specific extensions, VMS specific extensions,
CP/M specific extensions, and the extensions for every other operating
system that C runs on.

There is, as Ben suggested, comp.unix.programmer for generic Unix and
clones programming questions.  There are also specific groups for
specific Unix variants like Solaris, etc.  And there are a large
number of newsgroups for Linux, if that is the variant you are using,

In the future please only post questions about the C language here,
not about your compiler or operating system's particular extensions.
If you don't know whether your question pertains to the language or to
your platform, use a reference book or the man pages and find out
first.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Wed, 28 Jul 2004 12:05:42 GMT  
 read()'ing from a child's piped stdout


Quote:

> Hi all.

>  I hope this question doesn't get disregarded as my earlier question today
> did, simply because exec is "non standard"... I thought it was for some
> reason. Oh well. Anyway, I have a parent which uses pipe to associate two
> fileno's. It then forks a child who dup2's it's stdout to one of the
> filenos.

Hello Jason,

Guess what else is nonstandard? :)
Your question would be better suited to comp.unix.programmer.
Good luck!

-Daniel



Wed, 28 Jul 2004 12:18:50 GMT  
 read()'ing from a child's piped stdout

On Fri, 8 Feb 2002 at 23:18, Daniel Fox so generously added:

Quote:

> >  I hope this question doesn't get disregarded as my earlier question today
> > did, simply because exec is "non standard"... I thought it was for some
> > reason. Oh well. Anyway, I have a parent which uses pipe to associate two
> > fileno's. It then forks a child who dup2's it's stdout to one of the
> > filenos.
> Hello Jason,
> Guess what else is nonstandard? :)
> Your question would be better suited to comp.unix.programmer.
> Good luck!

Well, thanks for not insulting me like Mr. Klein anyway. Will post in
comp.unix.programmer, (though I bet I could recreate this behavior on a
windows machine. Perhaps I should then crosspost to every possible
platform specific newsgroup on which I can recreate this problem?)

Thanks.

--
Jason Simpson

  "All programmers are playwrights and all computers are lousy actors."  -- Unknown



Thu, 29 Jul 2004 04:24:01 GMT  
 read()'ing from a child's piped stdout

Quote:

> On Fri, 8 Feb 2002 at 23:18, Daniel Fox so generously added:


[snip]

Quote:
> Perhaps I should then crosspost to every possible
> platform specific newsgroup on which I can recreate this problem?)

No?

PLONK comes to mind.

--
Thomas.

Approaching singularity.



Thu, 29 Jul 2004 05:05:58 GMT  
 read()'ing from a child's piped stdout

On Sat, 9 Feb 2002 at 21:05, Thomas Stegen so generously added:

Quote:
> > Perhaps I should then crosspost to every possible
> > platform specific newsgroup on which I can recreate this problem?)

> No?

> PLONK comes to mind.

Six years of off and on reading and posting, I imagine I made it in to a
few killfiles. Though I'd like to think I didn't. I was pointed to
comp.unix.programmer by some kind souls and there I was provided an
answer. I do, however, feel that there should be some intermediate level
for me to post at, as I can recreate this problem on Solaris, different
flavors of Linux and Windows. comp.unix.programmer didn't really seem
appropriate.

I also imagine that several people who spent the time spelling out "non
standard" could have simply sent me an email telling what I was doign
wrong. Anyway, thanks to everyone who directed me to comp.unix.programmer
and I'm really really sorry for posting non-ANSI-standard code in your
newsgroup. If anyone would like a formal written apology, please email me
with this request.

--
Jason Simpson

  "I also thank my reviewer---John Vlissides of Stanford University,  whose critical and insightful comments prodded me to rewrite the book  and Michael Tripoli, who checked the code for correctness. Naturally,  any remaining flaws are entirely their fault."  -- Cay S. Horstmann, "Mastering C++"



Thu, 29 Jul 2004 23:57:23 GMT  
 read()'ing from a child's piped stdout

Quote:

... snip ...

> I also imagine that several people who spent the time spelling out
> "non standard" could have simply sent me an email telling what I
> was doign wrong. Anyway, thanks to everyone who directed me to
> comp.unix.programmer and I'm really really sorry for posting
> non-ANSI-standard code in your newsgroup. If anyone would like a
> formal written apology, please email me with this request.

Jack Klein, to whose reply you objected, also directed you there.
On the way he pointed out several things.  Meanwhile you have
discovered a place to vet code that should not have any system
dependencies.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Fri, 30 Jul 2004 02:41:45 GMT  
 read()'ing from a child's piped stdout


Wed, 18 Jun 1902 08:00:00 GMT  
 read()'ing from a child's piped stdout
[Regarding pipes and EOF, or the lack thereof...]



Quote:
>... I was pointed to comp.unix.programmer by some kind souls and
>there I was provided an answer.

In other words, using the right group got you the right answer?
:-)  (This is, of course, the real point of redirection, even if
some who do the redirecting are less than polite about it.  Those
in the right newsgroup will not only know the answer, but also be
able to correct wrong answers.)

Quote:
>I do, however, feel that there should be some intermediate level
>for me to post at, as I can recreate this problem on Solaris, different
>flavors of Linux and Windows. comp.unix.programmer didn't really seem
>appropriate.

The group comp.unix.programmer is pretty much general to POSIX, and
Solaris and Linux both make use of ("conform to" is probably too
strong) POSIX.  In other words, any question whose answer is likely
to be general to all POSIX systems belongs in comp.unix.programmer.
The word "Unix"(R) in the newsgroup name is merely a holdover from
the past.

I would be somewhat surprised if the problem and its POSIXy solution
actually occur and work on (most?) Windows systems, because as I
understand it, Windows lacks pipes and has to simulate them with
files.  But someone in a Windows-specific newsgroup is quite likely
to know better than I -- which again suggests that, to get a good
answer, you need to pick the right newsgroup.  If some version(s)
of Windows now sport(s) a POSIX layer, the readers of Windows-specific
groups are likely to know the details.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)





Fri, 30 Jul 2004 05:34:26 GMT  
 read()'ing from a child's piped stdout


Wed, 18 Jun 1902 08:00:00 GMT  
 read()'ing from a child's piped stdout
On Saturday, in article


Quote:
>On Fri, 8 Feb 2002 at 23:18, Daniel Fox so generously added:

>> >  I hope this question doesn't get disregarded as my earlier question today
>> > did, simply because exec is "non standard"... I thought it was for some
>> > reason. Oh well. Anyway, I have a parent which uses pipe to associate two
>> > fileno's. It then forks a child who dup2's it's stdout to one of the
>> > filenos.
>> Hello Jason,
>> Guess what else is nonstandard? :)
>> Your question would be better suited to comp.unix.programmer.
>> Good luck!

>Well, thanks for not insulting me like Mr. Klein anyway. Will post in
>comp.unix.programmer, (though I bet I could recreate this behavior on a
>windows machine.

Possibly, but the code to do it would be different and Windows-specific.

Quote:
>Perhaps I should then crosspost to every possible
>platform specific newsgroup on which I can recreate this problem?)

If you want to post Unix-specific code to recreate this problem to
Unix newsgroups, Windows-specific code to Windows newsgroups, etc. etc.
that's up to you but it would be inappropriate to cross-post Windows-
specific code to non-Windows related newsgroups etc. or any platform-
specific code to comp.lang.c.

--
-----------------------------------------


-----------------------------------------



Fri, 30 Jul 2004 05:20:14 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Reading child pgm's stdout?

2. Q:Frequent malloc'ing and free'ing

3. Buffers vs child pipe to stdout

4. how to prevent 'free'-ing memory twice

5. Q!!'scanf'ing strings with char**s

6. trouble with realloc'ing array of char *'s

7. Quickie on free()'ing malloc()'d mem

8. typedef'ing iterators in MSVC's STL (not the HP STL)

9. CComboBox 'Sort'ing Under Program Control?

10. 'new'ing >64K Objects in BC++

11. single DoDataExchange 'ing'

12. CComboBox 'Sort'ing Under Program Control?

 

 
Powered by phpBB® Forum Software