The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-29-2007
PuppyHusher PuppyHusher is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 3
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 (permalink)  
Old 03-30-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
See Writing c code for who | sort | lp
  #3 (permalink)  
Old 03-30-2007
PuppyHusher PuppyHusher is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 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 (permalink)  
Old 03-30-2007
PuppyHusher PuppyHusher is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 3
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);
}
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0