Help with piping program


 
Thread Tools Search this Thread
Top Forums Programming Help with piping program
# 1  
Old 03-29-2007
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;
}

# 2  
Old 03-30-2007
# 3  
Old 03-30-2007
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  
Old 03-30-2007
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);
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

How to take awk result out (piping to other program)?

Hi! all here is my code which is working fine no errors but I want to know how to take result and input to other program awk 'FNR==1{i++}{LC=NR} {for(k=1; k<=NF; k++) A=$k} END{for (i=1;i<=LC;i++) { for(j=1;j<=LC;j++) if(A=='$UID' && A>='$MX'+A &&... (7 Replies)
Discussion started by: Akshay Hegde
7 Replies

2. Ubuntu

Piping with grep

Hi everybody, I have a big file with blast results (if you know what this means, otherwise look at it just as a text file with a specific form). I am trying to extract some ids from within this file, which have certain parameters. For example, some Of my IDs have the term 'No hit results'... (1 Reply)
Discussion started by: frymor
1 Replies

3. UNIX for Dummies Questions & Answers

Piping GREP

Hi, I need to use a double grep so to speak. I need to grep for a particular item say BOB and then for each successful result I need to grep for another item say SMITH. I tried grep "BOB" filename | grep "SMITH" but it does not seem to work. I can achieve my desired result using an... (12 Replies)
Discussion started by: mojoman
12 Replies

4. Shell Programming and Scripting

Piping in Perl

Hi All, I am trying to perform the below csh code in Perl, but i am unfamiliar with Perl. Can anybody give me some advice on it ? Csh Code: cat filename |grep AAA| grep BBB| awk '{print("already_appended")' (11 Replies)
Discussion started by: Raynon
11 Replies

5. Shell Programming and Scripting

Piping to ex from a script

Is anyone piping commands to ex from scripts? I.E. echo '%s/change this/to that/\nwq' | ex file.name I've been using it for years with AIX, Solaris, SGI, with variations ksh and Mandriva and others with pdksh. I've just started using CentOS with ksh and it no longer works. I've tried single... (2 Replies)
Discussion started by: mph
2 Replies

6. Shell Programming and Scripting

Piping / Executing

I've got a file with lots of commands I want to run in it. They're formatted like so: cp /path/to/file /path/to/new/file and on and on and on. Hundreds of them. Anyways, I'd like to execute them one at a time, then check what time it is, and repeat this process until 7am. I can... (3 Replies)
Discussion started by: ProFiction
3 Replies

7. UNIX for Dummies Questions & Answers

Piping in UNIX

All, I am a UNIX novice with a question that I hope you can help me with. I have a UNIX application called "Tole" that formats and displays specific information about customers. I can display the information for up to 30 customers by seperating customer IDs using commas in this format: Tole -c... (3 Replies)
Discussion started by: simo007
3 Replies

8. Shell Programming and Scripting

Piping from device?

Hi Long time since I did any shell scripting so please be gentle with me! :) Just wanted to know whether it is possible to take the streaming output from a dvb card /dev/dvb/adapter0/ and using named pipes and tee to pass the outputs to mplayer and mencoder so as to watch and record a telly... (0 Replies)
Discussion started by: gary101
0 Replies

9. Shell Programming and Scripting

piping

I am using pipes (specifically piping out) in Perl to put an array from one file into an array in a different file. I can't figure out how to transfer the array. I kow how to open the pipe : open (FILEHANDLE, "| file") or die~ but how do I transfer the array. I think it has something to do with... (1 Reply)
Discussion started by: lnatz
1 Replies

10. UNIX for Advanced & Expert Users

redirecting/piping

i want to fully undertand redirecting/piping. i know most of the basic stuff, like ls|less or cat file|grep hello etc etc. but there are a somethings like find / -name file 2>/dev/null. what's is the 2? i know it redirects the errors (at least Permission denied ones) to /dev/null (the unix... (3 Replies)
Discussion started by: crashnburn
3 Replies
Login or Register to Ask a Question