![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Piping Question | mtobin1987 | High Level Programming | 2 | 04-25-2008 11:24 PM |
| Piping to ex from a script | mph | Shell Programming and Scripting | 2 | 10-11-2007 03:54 PM |
| Piping in UNIX | simo007 | UNIX for Dummies Questions & Answers | 3 | 05-23-2007 02:40 AM |
| piping | lnatz | Shell Programming and Scripting | 1 | 07-14-2006 02:30 AM |
| Help (Piping ls, tr, cut) | scan | Shell Programming and Scripting | 2 | 02-11-2006 08:40 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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;
}
|
|
||||
|
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);
}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|