Pipes

This is a discussion on Pipes within the C Programming forums, part of the General Programming Boards category; Hi, my program must have 3 commnads as arguments. For example ./program /bin/ls -l /bin/grep pattern /bin/wc -l. This program ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    Pipes

    Hi,

    my program must have 3 commnads as arguments. For example ./program /bin/ls -l /bin/grep pattern /bin/wc -l. This program execute these 3 commands which will be communicate over pipes: first commands output go to standard output and this standard output is standartd input of second command which output goes to standard output and this output are standard input of third command.
    So output of /bin/ls -l is input to /bin/grep pattern which output is input to /bin/wc -l command.

    How can I do that using pipes?

    My program is like:

    int main(int argc, char *argv[]) {

    pid_t process_id01, process_id02, process_id03;
    int status;
    int fds01[2], fds02[2];
    pipe(fds01); pipe(fds02);
    char* commands_list[3][2];

    process_id01 = fork();
    if ( process_id01 == 0 ) {

    close(1);

    dup(fds01[1]);
    close(fds01[0]);
    close(fds01[1]);

    execv(commands_list[0][0], commands_list[0]);

    .......

    }

    process_id02 = fork();
    if ( process_id02 == 0 ) {

    close(0);

    dup(fds01[0]);
    close(fds01[0]);
    close(fds01[1]);

    close(1);

    dup(fds02[1]);
    close(fds02[0]);
    close(fds02[1]);

    execv(commands_list[1][0], commands_list[1]);

    .......

    }

    process_id03 = fork();
    if ( process_id03 == 0 ) {

    close(0);

    dup(fds02[0]);
    close(fds02[0]);
    close(fds02[1]);

    close(1);

    dup(fds01[1]);
    close(fds01[0]);
    close(fds01[1]);

    execv(commands_list[2][0], commands_list[2]);

    .......

    } else {

    close(fds01[0]);
    close(fds01[1]);
    close(fds02[0]);
    close(fds02[1]);

    }

    return(0);

    }

    In commands_list is my commands with their arguments - this is ok I have function to put values into array and affter echo everything is ok. Problem is only in pipes.

    Thanks for help.
    Last edited by Martin Kovac; 03-31-2009 at 03:50 AM.

  2. #2
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    how about

    system("ls -l | grep "pattern" | wc -l");

    :P

    but i m sure thats not what you want...there are a host of system calls for that.(not really, mostly only fork() , pipe(), dup() ,dup2() and popen() )

    here are a few example that i had found helpful...

    Mapping UNIX pipe descriptors to stdin and stdout in C

    6.2.2 Creating Pipes in C
    Last edited by creeping death; 03-31-2009 at 03:19 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 01:47 PM
  2. Help - newbie playing with pipes
    By kotoko in forum C Programming
    Replies: 14
    Last Post: 10-21-2008, 04:41 PM
  3. pipes in c
    By ajal1 in forum C Programming
    Replies: 6
    Last Post: 10-29-2005, 03:29 PM
  4. Need some help with pipes please.
    By carrja99 in forum C Programming
    Replies: 1
    Last Post: 05-05-2004, 04:13 PM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 06:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21