The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



Thread: Piping Question
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 04-24-2008
mtobin1987 mtobin1987 is offline
Registered User
 

Join Date: Apr 2008
Posts: 2
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 12:33 AM. Reason: added code tags
Reply With Quote
Forum Sponsor