The problem of pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting The problem of pipe
# 1  
Old 03-07-2011
The problem of pipe

Hi,guys:

I want to use c to implement a pipe. For example:

ps auxwww | grep fred | more


I forked three child processes. Each is responsible for each command, and pipe to next one.

Code:
for(i=0;i<2;i++)
     pipe(fd[i])

if(child==1)     // child 1
{

close(1)
dup2(fd[0][1],1)
close(fd[0][0])
}
else if (child==1)   // child 2
{
   close(0)
   close(1)
   dup2(fd[0][0],0)
   dup2(fd[1][1],1)
   close(fd[0][1])
   close(fd[1][0])
}

else if(child==2)   //child3
{
     close(0)
     dup2(fd[1][0],0)
     close(fd[1][1])
}



But it does not work, it seems like there is something wrong with child 2. Can anyone give me some suggestions?

Last edited by Franklin52; 03-07-2011 at 03:13 AM.. Reason: Please use code tags
# 2  
Old 03-08-2011
Code not reachable, same predicate in two if-else-if's.

One Pipe makes 2 fd, and you are making 4 and storing 3?

dup2 is paranoia, dup goes low.
# 3  
Old 03-08-2011
I'm assuming this is pseudocode due to the logic error and the near total lack of semicolons. If my fixes don't work I'd like to see the real code.

If it's hanging, it's probably because you left dangling pipe ends around. Remember that a pipe remains valid for as long as both its reading and writing ends are open. Ends your program aren't using still count, and you left both ends of entire pipes hanging around in child 1 and child 3. Ergo, programs reading or writing to those pipes will never ever quit.

I disagree that dup2 is overkill. Even if you don't need to be explicit about which pipe you're copying where, it's good to be clear about it -- if not for yourself, then the future generations that have to read and figure out your code...

Code:
for(i=0;i<2;i++)
     pipe(fd[i])

if(child==1)     // child 1
{
  // close is redundant if you're using dup2.
  // close(1)
  dup2(fd[0][1],1);

  // close ALL pipe ends.  You only need the copy.
  close(fd[0][0]); close(fd[0][1]); close(fd[1][0]); close(fd[1][1]);
}
else if (child==2)   // child 2
{
   // close is redundant if you're using dup2.
   //close(0);    close(1);
   dup2(fd[0][0],0);
   dup2(fd[1][1],1);

  // close ALL pipe ends.  You only need the copy.
  close(fd[0][0]); close(fd[0][1]); close(fd[1][0]); close(fd[1][1]);
}
else if(child==3)   //child3
{
  // you don't need to close if you're using dup2.
  //   close(0)
  dup2(fd[1][0],0)
  // close ALL pipe ends.  You only need the copy.
  close(fd[0][0]); close(fd[0][1]); close(fd[1][0]); close(fd[1][1]);
}

Quote:
...But it does not work, it seems like there is something wrong with child 2.
Can you be more specific about what "does not work" means? I've heard that description for anything from "slow" to "on fire".
# 4  
Old 03-14-2011
PS: Only one FD of the two returned by pipe90 is for writing, so be careful who gets which..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

pipe() and poll() problem in C

Hi all, Im trying to do a simple program which ask the user for a unix command with the arguments. The program fork and the two process communicate with pipes. The child process call execvp with the command and the father process read the result of the execvp via the pipe. This program works... (11 Replies)
Discussion started by: blackmamba21
11 Replies

2. UNIX for Dummies Questions & Answers

problem with pipe operator

hi i am having issues with extra pipe. i have a data file and i need to remove the extra pipe in the(example 4th and 7thline) in datafile. there are many other line and filed like this which i need to remove from files. The sample data is below: 270 31|455004|24/03/2010|0001235|72 271... (3 Replies)
Discussion started by: abhi_n123
3 Replies

3. Shell Programming and Scripting

problem using a pipe to grep

Hello ! I want to process a text file in order to extract desired data using sed and grep... Now I am facing a problem piping to grep... nothing happens.. The text consists of blocks of 3 lines that may (or not) contain the Desired data. the desired data is on each 2... (4 Replies)
Discussion started by: ShellBeginner
4 Replies

4. Programming

Pipe problem

Could anyone tell me whats wrong whit this piping? the commands that they execute are correct. the command I am trying is ls|wc. Both processes go to the right if statement. for(i=0;i<argc;i++){ if(i==0&&argc>1){//first command if(pipe(pipa1)==-1) ... (2 Replies)
Discussion started by: isato
2 Replies

5. Programming

Problem in read() from a pipe

Hi, Can any one please help me with this. Am struggling hard to get a solution. I am doing telnet through a C program and getting the stdout file descriptor of the remote machine to pipe. read() function is getting data, But whenl it receives SOH character ie. ^A ( Start of heading = Console... (2 Replies)
Discussion started by: JDS
2 Replies

6. UNIX for Dummies Questions & Answers

awk problem broken pipe

Hello everyone I got a school project that was due yesterday so i really would aprreciate some help the following is an example of the input file (bateriaTestes) 1 caeiro 2000 d 2 pessoa 100 w 3 campos 200 b 4 soares 500 w simple 4 field lines. lines separated by \n and fields by... (1 Reply)
Discussion started by: zemanel
1 Replies

7. Programming

Pipe Problem

Is there a way to know whether is pipe is opened in read or write mode.I mean is there any signal that is generated when a pipe is opened in read or write mode. If you have some solution .please let me know ........ (2 Replies)
Discussion started by: vivivo2000
2 Replies

8. Shell Programming and Scripting

Problem with pipe into sed

Basically I am trying to write a short script to report total space used on /u0? file systems. This is what I was trying to do:df -k /u0? | grep -v kbytes | awk '{ printf $2 "+" }' | sed s/.$// | bcBut it returns no output. This works however: > A=`df -k /u0? |grep -v kbytes | awk '{ printf $2... (2 Replies)
Discussion started by: 98_1LE
2 Replies

9. Shell Programming and Scripting

read after pipe problem OSX10.4

I use read often in scripts to filter the right part into a variable like: $ print "abc cde efg" | read k l ; print "k=$k, l=$l" k=, l= This works on linux and unix versions I work with. On OSX 10.4 this doesn't work. I found a workaround but would like to know why the original line... (5 Replies)
Discussion started by: relyveld
5 Replies

10. Programming

Wierd pipe problem

I have encountered a strange problem dealing with pipes and forking. The program basicaly does this: cat file | tbl | eqn | groff Now, I have a parent process that forks children that that exec the stuff that they should. The pipes defined in the parent are the ones used. The chain goes... (1 Reply)
Discussion started by: denoir
1 Replies
Login or Register to Ask a Question