![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Piping Question | mtobin1987 | High Level Programming | 2 | 04-25-2008 08:24 PM |
| Piping to ex from a script | mph | Shell Programming and Scripting | 2 | 10-11-2007 12:54 PM |
| Piping in UNIX | simo007 | UNIX for Dummies Questions & Answers | 3 | 05-22-2007 11:40 PM |
| piping | lnatz | Shell Programming and Scripting | 1 | 07-13-2006 11:30 PM |
| Help (Piping ls, tr, cut) | scan | Shell Programming and Scripting | 2 | 02-11-2006 05:40 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Help with piping program
Hi, I am trying to write a program that will pipe any number of programs together like in the linux shell. As an example, the below code tries to execute "cat data | grep int | cut -b 1-10." The problem is that the programs never get executed for some reason. It seems like the first program executes, then nothing. Does anyone know what is wrong with this? Thanks.
Code:
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
void run_pipe(char **command);
int pipes[4], pipeline;
int main(void){
char* cmd1[]={"cat", "data", NULL};
char* cmd2[]={"grep", "int" , NULL};
char* cmd3[]={"cut", "-b", "1-10", NULL};
pipeline = 0;
pipe(pipes);
pipe(pipes + 2);
run_pipe(cmd1);
run_pipe(cmd2);
run_pipe(cmd3);
}
void run_pipe(char **command){
int status, j;
if (!command) return;
switch (pipeline){
case 0: dup2(pipes[1], 1); break;
case 1:
pipe(pipes + 2);
dup2(pipes[0], 0);
dup2(pipes[3], 1);
break;
case 2:
pipe(pipes);
dup2(pipes[2], 0);
dup2(pipes[1], 1);
break;
} for (j=0; j<4; j++)
close(pipes[j]);
if (fork() == 0){
execvp(*command, command);
}
wait(&status);
pipeline++;
if (pipeline == 3) pipeline = 1;
}
|
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
#3
|
|||
|
|||
|
That is useful code, but my application needs to do any number of pipes, not just 2. So a loop needs to be implemented, but I don't know why my implementation is not working. Maybe you can spot something amiss? Thanks.
|
|
#4
|
|||
|
|||
|
Never mind, I figured it out after some more searching and trying different things. Here's the working code, it can easily be changed to add more processes.
Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
void run_pipe(char **path, int read_fd, int write_fd);
int main (int argc, char **argv){
int i;
int pipe_count = 2;
int proc_count = 3;
int (*pipes)[2] = malloc(sizeof(*pipes) * pipe_count);
char *cmd1 [] = {"cat", "test2.c", NULL};
char *cmd2 [] = {"grep", "printf", NULL};
char *cmd3 [] = {"cut", "-b", "1-6", NULL};
for (i = 0; i < pipe_count; i++) pipe(pipes[i]);
for (i = 0; i < proc_count; i++) {
if (i == 0){
run_pipe(cmd1, 0, pipes[i][1]);
}else if (i < proc_count - 1){
run_pipe(cmd2, pipes[i-1][0], pipes[i][1]);
}else{
run_pipe(cmd3, pipes[i-1][0], 0);}
}
}
void run_pipe(char **path, int read_fd, int write_fd){
int pid = fork();
if (pid){ //parent
if (read_fd) close(read_fd);
if (write_fd) close(write_fd);
return;
}
if (read_fd) dup2(read_fd , 0);
if (write_fd) dup2(write_fd, 1);
execvp(*path, path);
}
|
|||
| Google The UNIX and Linux Forums |