Piping Question


 
Thread Tools Search this Thread
Top Forums Programming Piping Question
# 1  
Old 04-25-2008
Piping Question

I have a piping question, I am trying to implement piping on my own shell and am having some trouble...esentially I am trying to make something to do command|command|command.
I can get it to work fine if the last pipe command is not forked, but executes in the shell and then exits..but I need it to fork. I tryed the following, but it will hang..any suggestions? I have a feeling it has something to do with closing/opening descriptors but i don't know...thanks.:
Code:
int pipe1[2], pipe2[2];

        pipe(pipe1);//create first pipe
		pid_t PID=fork();
        if(PID==0){
                close(1);
                dup(pipe1[1]);
                close(pipe1[0]);
                close(pipe1[1]);
               	execvp(*argv,argv);
                printf("operation failed");

        } else {
                
                pipe(pipe2);
				pid_t PID2=fork();
                if (PID2==0) {
                        close(0);
                        dup(pipe1[0]);
                        close(1);
                        dup(pipe2[1]);
                        close(pipe1[0]);
                        close(pipe1[1]);
                        close(pipe2[0]);
                        close(pipe2[1]);
                        execvp(*argv2, argv2);
                } else {
                		pid_t PID3=fork();
   				waitpid(PID3,&status,0);<-----Not sure about this
                		if(PID3==0){
                        close(0);
                        dup(pipe2[0]);
                        close(pipe1[0]);
                        close(pipe1[1]);
                        close(pipe2[0]);
                        close(pipe2[1]);
                        execvp(*argv3, argv3);
                		}
                		
                	}
      	  }


Last edited by Yogesh Sawant; 04-25-2008 at 04:33 AM.. Reason: added code tags
# 2  
Old 04-25-2008
Ok, homework/coursework is not allowed here, but you appear to have a genuine effort so I am going to allow this thread to stay open.
# 3  
Old 04-26-2008
I figured it out, I did just have to close some of the pipe descriptors..thanks anyway thoughSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Piping commands

Hi I am tryin to undertand piping command1|command2 from what i learn output of cammand 2 is an intput for command 1 right? If so . What dose next sequence do cat f1 >> f2 | grep '^' I think it takes context of f1 and Concatenate's it to f2 and then looks for ....i don't know..... (7 Replies)
Discussion started by: iliya24
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'... (6 Replies)
Discussion started by: frymor
6 Replies

3. 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

4. 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

5. UNIX for Dummies Questions & Answers

Piping syntax question

There are are lots of examples of piping output FROM the 'ls' command TO another command, but how does one pipe output TO the 'ls -l' command? For example, use 'which' to find a file, then use 'ls -l' to view the permissions, groups, etc. in a single step: which <filename> | ls -l returns... (4 Replies)
Discussion started by: johne1
4 Replies

6. 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

7. Shell Programming and Scripting

question about grep, cut, and piping

Howdy folks, I am fairly new to scripting but have lost of expirience in c++, pascal, and a few other. I am trying to complete a file search script that is sent a file name containing data to search that is arranged like this "id","name","rating" "1","bob","7" etc and an argument to... (1 Reply)
Discussion started by: dyrt
1 Replies

8. 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

9. 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

10. 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
Login or Register to Ask a Question