Re: Question about pipes

From: Madhusudan Singh (spammers-go-here_at_yahoo.com)
Date: 02/16/04


Date: Sun, 15 Feb 2004 19:57:05 -0500

On Sunday 15 February 2004 15:09, Nick Landsberg (hukolau@att.net) held
forth in comp.os.linux.misc
(<QlQXb.4905$aH3.155532@bgtnsc04-news.ops.worldnet.att.net>):

>
>
> Madhusudan Singh wrote:
>> On Saturday 14 February 2004 21:37, Gianni Mariani (gi2nospam@mariani.ws)
>> held forth in comp.os.linux.misc (<c0mm1a$rlj@dispatch.concentric.net>):
>>
>> Thanks for the code. I would however prefer to see some clearer C code
>> (or even pseudo code). Speaking as a programmer wanting to port things
>> over to another language, and not as a specialist interested in OO code,
>> I find OO programming to be obfuscating the issue.
>>
>> I just need the translation of
>>
>> popen(progname,"w") -> pipe,fork,dup2 in C pseudo code (with the correct
>> progname calls, etc).
>>
>> I can figure out how to implement it in Fortran 95 on my own.
>>
>> Unravelling C++ code to get to what I need can be time consuming for me
>> (I haven't done any OO C++ programming for at least 6-7 years).
>
> It's actually a pretty complex operation and it's been years since
> I've done it so I may miss something along the way. This is pseudo-code
> with all the error checking left out just to be easier to follow.
> It presupposes that on your system, file descriptor zero refers
> to stdin, file descriptor 1 to stdout, file descriptor 3
> to stderr, but we're only interested in fd = 1 right now.
>
> int pipedes[2];
> pid_t childpid;
>
> pipe(&pipedes[2]); /* error checking left out */
>
> /* pipedes[0] now contains a file descriptor (not File *) to
> the read end of the pipe, pipedes[1] contains a file descriptor
> to the write end of the pipe */
>
> childpid = fork(); /* error checking left out */
>
> if (childpid >0) { /* parent process */
> close (pipedes[0]) ; /* error checking left out */
> /* close the read end of the pipe in the parent, assuming parent
> will be writing stuff down the pipe to child */
> ...
> some code to associate a File * with the file descriptor if
> this is C, etc.
> } else { /* child process */
> close (pipedes[1] ; /* error checking left out */
> /* close write end of the pipe, the parent will be writing
> to this descriptor */
> if( dup2(0,pipesdes[0] != 0 ) { /* attach pipe to stdin */
> /* it didn't attach to stdin */
> /* error code goes here */
> }
>
> /* at this point in the child, file descriptor 0
> is attached to the read end of the pipe. This
> presupposes that fd = 0 is the convention for
> standard input on your system. Other file descriptors
> not specifically closed are inherited from the
> parent */
>
> execlp("progname", "progname", "args" ... );
> /* or use your favorite variation of exec in
> section 3 */
>
> my_error_exit("Return from exec?");
> }
> /* now we're in the parent and can write to
> pipedes[1] to our heart's content.
> Note that reads and writes have to be
> synchronized to some extent in that
> the child may block if there is insufficient
> information in the pipe. For example, if
> the parent writes 128 bytes, but the child
> is reading in 512 byte chunks, the child
> will block until there are 512 bytes in the pipe.
> The parent may also block if the pipe size
> (implementation dependent) is exceeded
> and continue to block until the child reads
> some data from the pipe */
>
> As I said, I may have left something out since it's
> been so long since I've done this and I had
> to look up the man pages to refresh my memory
> on most of these calls, so there may be
> some errors up there. It should give you
> a start which will let you experiment, tho.
>
> Hope this helps.
>

Thanks !!!

One excellent, relevant response makes listening to all the rest worthwhile.
 



Relevant Pages

  • Re: Question about pipes
    ... >> (or even pseudo code). ... > It presupposes that on your system, file descriptor zero refers ... > is attached to the read end of the pipe. ... > the child may block if there is insufficient ...
    (comp.unix.programmer)
  • Re: what happens to Popen()s parent-side file descriptors?
    ... This means that somewhere a pipe file descriptor is opened on the ... parent side to read from the child's stdout. ... to the child process, until *all* data are sent and received. ...
    (comp.lang.python)
  • Re: Question about pipes
    ... Speaking as a programmer wanting to port things over to ... It presupposes that on your system, file descriptor zero refers ... the read end of the pipe, ... the child may block if there is insufficient ...
    (comp.os.linux.misc)
  • Re: Pipe and execl Error
    ... Child write  5000 bytes to pipe, after parent read 512 byte, child ... should be ever cause a "bad file descriptor". ...
    (comp.unix.programmer)
  • Re: Pthread fd and memory leak
    ... and closes the socket file descriptor. ... When you send an fd over a pipe, the fd is still open in the sending ... memory at the pointer value and free it without errors, ...
    (comp.programming.threads)