The UNIX and Linux Forums  

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
question about grep, cut, and piping dyrt Shell Programming and Scripting 1 02-28-2008 07:12 PM
Piping to ex from a script mph Shell Programming and Scripting 2 10-11-2007 12:54 PM
Piping in UNIX simo007 UNIX for Dummies Questions & Answers 3 05-22-2007 11:40 PM
piping lnatz Shell Programming and Scripting 1 07-13-2006 11:30 PM
Help (Piping ls, tr, cut) scan Shell Programming and Scripting 2 02-11-2006 04:40 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2008
Registered User
 

Join Date: Apr 2008
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
  #2 (permalink)  
Old 04-25-2008
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,439
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 04-25-2008
Registered User
 

Join Date: Apr 2008
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
I figured it out, I did just have to close some of the pipe descriptors..thanks anyway though
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
"inappropriate ioctl for device" 421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix interview questions unix simulator unix.com vi select all vi substitute vi+substitute+end+of+line+character while loop within while loop shell script


All times are GMT -7. The time now is 04:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101