Professional Web Applications Themes

Question about pipes - UNIX Programming

Hi Just a basic question from someone who is new to pipes. The statement of the problem is simple : Instead of using popen(progname,"w"), I wish to use the elementary functions pipe, fork, dup2, etc. to get the same effect. You may well wonder why I am trying to do this - I am programming in another language (Fortran 95) where a library provides equivalents for pipe(), fork(), dup2() etc., but not for popen(). I do NOT wish to depend upon underscores in the object code and want to make my code pure Fortran 95. I would appreciate it if ...

  1. #1

    Default Question about pipes

    Hi

    Just a basic question from someone who is new to pipes.

    The statement of the problem is simple :

    Instead of using popen(progname,"w"), I wish to use the elementary
    functions pipe, fork, dup2, etc. to get the same effect.

    You may well wonder why I am trying to do this - I am programming in
    another language (Fortran 95) where a library provides equivalents for
    pipe(), fork(), dup2() etc., but not for popen().

    I do NOT wish to depend upon underscores in the object code and want
    to make my code pure Fortran 95.

    I would appreciate it if someone could give me a flowchart (with
    proper calls like pipe() with the arguments).

    Thanks,

    MS
    Madhusudan Guest

  2. #2

    Default Re: Question about pipes

    On Saturday 14 February 2004 21:37, Gianni Mariani (ws)
    held forth in comp.os.linux.misc (<c0mm1a$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).
    Madhusudan Guest

  3. #3

    Default Re: Question about pipes

    On Sunday 15 February 2004 15:09, Nick Landsberg (net) held
    forth in comp.os.linux.misc
    (<QlQXb.4905$ops.worldnet.att.net>):
     
    >
    > 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.
    >[/ref]

    Thanks !!!

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

    Madhusudan Guest

Similar Threads

  1. Bidirectional pipes
    By Mehmet Ekici in forum PERL Beginners
    Replies: 4
    Last Post: January 28th, 10:45 PM
  2. question about I/O and pipes
    By Jean-Baptiste in forum Ruby
    Replies: 7
    Last Post: December 24th, 02:12 AM
  3. help w/ pipes
    By Adam in forum UNIX Programming
    Replies: 20
    Last Post: December 17th, 10:40 PM
  4. get rid of whitespace around pipes??
    By Jeff 'Japhy' Pinyan in forum PERL Beginners
    Replies: 3
    Last Post: December 17th, 04:26 PM
  5. [PHP] shell_exec with pipes
    By Jay in forum PHP Development
    Replies: 6
    Last Post: November 16th, 03:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139