LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-20-2007, 02:12 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
chained pipe example


Hi,

I would like to program a C example that execute the following command:

command 1 | command 2 | command n

I don't know how to do it. Can anyone give me an example?

Thanks,
 
Old 10-20-2007, 02:46 PM   #2
henk1420
LQ Newbie
 
Registered: Aug 2007
Location: Europe
Distribution: Debian
Posts: 5

Rep: Reputation: 0
Quote:
Originally Posted by pedrosacosta View Post
Hi,

I would like to program a C example that execute the following command:

command 1 | command 2 | command n
The trick is that command 1 writes to stdout and command 2 reads from stdin and writes to stdout. Etc.

fprintf(stdout, format, data...); // In command 1

fgets(line_of_text, length_of_line_of_text, stdin); or fscanf(stdin, format, pointers_to_data); // In command 2.

Regards,
Henk
 
Old 10-20-2007, 05:31 PM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
But the problem is that, I want to execute to commands.

command1 | command2

To execute them I need to use some function like execl. But, I don't know how to use the stdout from command one and pass to stdin from command2.

Any help?
 
Old 10-20-2007, 07:20 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, Pedrosacosta:

1. In Unix and Linux, each file you open gets a consecutively numbered file descriptor. The first three descriptors come automatically when the program is created: "stdin" is "0", "stdout" is "1", and "stderr" is "2". The first file or socket you open after that will be "3", and so on.

2. The way I/O redirection works is to *close* some file (e.g. "stdin") and *open* another file. The new file will get the old number.

3. "dup()" lets you "clone" an existing descriptor.

"dup2()" saves you the trouble of doing a separate "close()" (the "close" and "open" become part of the same, atomic action.

4. Here's a link that might make it all a bit clearer (look down in the presentation for item #3: I/O redirection):

http://www.comp.nus.edu.sg/~cs2281/2...ls9.colour.pdf
 
Old 10-21-2007, 05:39 AM   #5
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
paulsm4, I've saw the example, but I'm still very confusing about it.


Code:
1void piping(char* cmd[], int nr)
2{
3  int i;
4  int thisin;
5  int pid[2];
6
7  thisin = fileno( stdin );// get stdin descriptor
8
9  for ( i=0; i<nr; i++)
10    {
11     pipe( pid );
12
13      if ( fork() == 0 )
14        {
15          dup2( thisin, fileno( stdin ));
16
17          if ( i != (nr-1) )// i isn't the last example
18            dup2( pid[1], fileno ( stdout ));
19
20          close(pid[0]);
21          close(pid[1]);
22
23         execlp ( cmd[i], cmd[i], NULL);
24        }
25      else
26        {
27          dup2( pid[0], thisin);
28
29          close(pid[0]);
30          close(pid[1]);
31        }
32    }
33}
- why line 17 is needed? The last command don't have to write in the stdout?
- Why this example only works to pipes with 3 programs execution? Eg. "ls -la | sort | wc". And doesn't work with 2, 4, 5, ..., n program executions?

I'm really confused about the functioning of the pipes. I've already googled up, read books ("Advanced Programming in Unix environments", and I'm still very mixed up.

I don't have any friend that program C at this level, so I can't get any help from them.
 
  


Reply



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
What route to access daisy chained 2d router 192.168.1.1 after 192.168.0.1 (subnets?) Emmanuel_uk Linux - Networking 6 05-05-2006 01:47 AM
how to create a chained js web form ? graziano1968 Programming 2 11-12-2004 03:55 AM
mounting a daisy chained firewire drive jamida Linux - Newbie 1 05-30-2004 09:08 PM
Daisy Chained Parallel Devices in Linux? JockVSJock Linux - Hardware 2 03-29-2004 08:58 PM
Daisy-chained || printer beckwith Linux - Hardware 0 08-28-2003 02:50 AM

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

All times are GMT -5. The time now is 10:12 AM.

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