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.