Need help with pipe to redirect input/ output

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Need help with pipe to redirect input/ output


    I have created two child processes. I am trying to tell one process to
    write a command to the pipe, and I want the other child to read from the
    pipe and print it out to the stdout. I want first child to write the
    result of ps command with argument -aef to pipe, and the second child to
    look at the pipe's output and run grep command to print all processes
    running csh. I have used execl("usr/bin/ps -aef", "ps",0); to write the
    result of ps to the pipe, but I don't know how to make the second child
    read it and pick processors with csh??????????

    I would appreciate your help.

    int pipefd;
    pipe(pipefd);
    switch(pid2=fork())// This one writes to pipe
    case -1: //error
    case 0: close(1);
    dup(pipefd[1]);
    close(pipefd[0]);
    execl("usr/bin/ps -aef", "ps",0);

    switch(pid3=fork())// This one reads from pipe and prints to stdout
    case -1: //error
    case 0: close(0);
    dup(pipefd[0]);
    close(pipefd[1]);
    //Need to read output from pipe and print to stdout???????

    --
    Posted via [url]http://dbforums.com[/url]
    azi Guest

  2. Similar Questions and Discussions

    1. redirect output to a file
      Hi everybody I was wondering I've made a little script to make the 'w'-command a bit nicer to work with.. ---~/bin/w--- #/bin/bash cd w -sh...
    2. can't redirect output to a file?!
      i wrote a simple program that read data from STDIN but redirected in a pipe from a file parse it and wrote output to STDOUT,here is the code: ...
    3. How do I detect if input is from a pipe or not?
      I'm writing a utility that can get input from a file or from a pipe'd output from another program: myprogram -infile test.txt or type...
    4. 813:cannot write to pipe for output(no reading message)
      kevin wrote: Again - you need to provide us with enough information to help you. Platform? Version? What tool is doing the 'print'? ...
    5. Redirect output of include() for mailing
      Hello, Suppose I have a php file that output certain HTML given certain parameters. So I can call it likt http://localhost/myscript.php?foo=bar ...
  3. #2

    Default Re: Need help with pipe to redirect input/ output

    azi <member34294@dbforums.com> wrote:
    > I have created two child processes. I am trying to tell one process to
    > write a command to the pipe, and I want the other child to read from the
    > pipe and print it out to the stdout. I want first child to write the
    > result of ps command with argument -aef to pipe, and the second child to
    > look at the pipe's output and run grep command to print all processes
    > running csh. I have used execl("usr/bin/ps -aef", "ps",0); to write the
    > result of ps to the pipe, but I don't know how to make the second child
    > read it and pick processors with csh??????????
    > I would appreciate your help.
    > int pipefd;
    This must be an array of 2 ints.
    > pipe(pipefd);
    > switch(pid2=fork())// This one writes to pipe
    > case -1: //error
    > case 0: close(1);
    > dup(pipefd[1]);
    > close(pipefd[0]);
    > execl("usr/bin/ps -aef", "ps",0);
    This should be

    execl( "/usr/bin/ps", "ps" "-aef", NULL );

    And check if 'ps' is really living in /usr/bin, on some systems it's
    in /bin or even /sbin and if you get it wrong execl() will return
    and not run ps.
    > switch(pid3=fork())// This one reads from pipe and prints to stdout
    > case -1: //error
    > case 0: close(0);
    > dup(pipefd[0]);
    > close(pipefd[1]);
    > //Need to read output from pipe and print to stdout???????
    What's the problem? Just use read(2) to read on the pipe and whatever
    you fancy to write it to stdout. Here's a working version of your
    program:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    int main( void )
    {
    int pipefd[ 2 ];
    char buffer[ 80 ];
    ssize_t size;

    if ( pipe( pipefd ) < 0 )
    exit( EXIT_FAILURE );

    switch( fork( ) )
    {
    case -1 :
    exit( EXIT_FAILURE );

    case 0 :
    close( 1 );
    dup( pipefd[ 1 ] );
    close( pipefd[ 0 ] );
    execl( "/bin/ps", "ps" "-aef", NULL );
    exit( EXIT_FAILURE );
    }

    switch( fork( ) )
    {
    case -1 :
    exit( 1 );

    case 0 :
    close( 0 );
    dup( pipefd[ 0 ] );
    close( pipefd[ 1 ] );
    while ( ( size = read( 0, buffer, 80 ) ) > 0 )
    write( 1, buffer, size );
    }

    return EXIT_SUCCESS;
    }
    Reagrds, Jens
    --
    _ _____ _____
    | ||_ _||_ _| [email]Jens.Toerring@physik.fu-berlin.de[/email]
    _ | | | | | |
    | |_| | | | | | [url]http://www.physik.fu-berlin.de/~toerring[/url]
    \___/ens|_|homs|_|oerring
    Jens.Toerring@physik.fu-berlin.de Guest

Posting Permissions

  • You may not post new threads
  • You may 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