LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2011, 08:16 AM   #1
longli
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Rep: Reputation: 0
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.



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?
 
Old 03-07-2011, 08:36 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
for(i=0;i<2;i++)
pipe(fd[i])

This is only going to run once with i=1...
 
Old 03-07-2011, 11:55 AM   #3
longli
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by szboardstretcher View Post
This is only going to run once with i=1...
What do you mean? How can change that ?


Thanks
 
Old 03-07-2011, 12:08 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
I'm not a programmer, but logically, this statement:

for(i=0;i<2;i++)

says, let i = 0, only run while less than 2, add one to i. So by the end of that line, i is already = to 1.

So, this will only run once, while i = 1.
 
Old 03-07-2011, 12:25 PM   #5
pgroover
Member
 
Registered: Sep 2005
Location: Colorado
Distribution: Ubuntu
Posts: 56

Rep: Reputation: 16
Actually, it will run twice. Once for fd[0] and once for fd[1]. You can change it in either of the following to resolve it:

Code:
for(i=0;i<3;i++)
{
  pipe(fd[i]);
}
or

Code:
for(i=0;i<=2;i++)
{
  pipe(fd[i]);
}
 
Old 03-07-2011, 05:05 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
I'm not a programmer, but logically, this statement:

for(i=0;i<2;i++)

says, let i = 0, only run while less than 2, add one to i. So by the end of that line, i is already = to 1.

So, this will only run once, while i = 1.
Not true. It will run with i==0 (i<2.) At the end of an iteration i++ and since i<2, it will run once more with i==1. After i++, however, i==2, so it won't run again. i++ happens at the end of the iteration; it wouldn't be logical to have it happen before.
Kevin Barry

Last edited by ta0kira; 03-07-2011 at 05:07 PM.
 
  


Reply

Tags
[c]



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to handle a broken pipe exception (SIGPIPE) in FIFO pipe? zyroot998 Programming 5 03-03-2011 08:10 PM
pipe problem? Thinking Programming 5 09-21-2005 10:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration