Unix Programming - Pipe version of a program

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2005 > Pipe version of a program





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Pipe version of a program
dgront

2005-07-15, 7:52 am

I wrote a simple program reading from stdin and writting some results:
../my_prog < in > out

Now I want to use a pipe. What should I change in my program to be able
to use it like this:
cat in_file | my_prog | wc

I read man pages for pipe(), but it doesn't help me much...

Dominik

Torsten Mueller

2005-07-15, 7:52 am

"dgront" <dgront@chem.uw.edu.pl> schrieb:

> I wrote a simple program reading from stdin and writting some
> results: ./my_prog < in > out
>
> Now I want to use a pipe. What should I change in my program to be
> able to use it like this: cat in_file | my_prog | wc


Nothing.

T.M.
Ulrich Hobelmann

2005-07-15, 7:52 am

dgront wrote:
> I wrote a simple program reading from stdin and writting some results:
> ./my_prog < in > out
>
> Now I want to use a pipe. What should I change in my program to be able
> to use it like this:
> cat in_file | my_prog | wc


Try it. It'll probably just work.

> I read man pages for pipe(), but it doesn't help me much...


pipe() is for setting up a communication channel between
processes, mostly before you fork(), so both processes own one end
of the pipe. That's what the shell does for you when you write
those "|" characters. ;)

--
XML is a prime example of retarded innovation.
-- Erik Meijer and Peter Drayton, Microsoft Corporation
Mr. Uh Clem

2005-07-15, 6:05 pm

dgront wrote:
> I wrote a simple program reading from stdin and writting some results:
> ./my_prog < in > out

^ ^
| \ Shell creates a new file named "out" and
| dups its file handle to your prog's stdout.
| Writes of stdout go to "out".
|
Shell opens file "in" and dups its file handle to
your prog's stdin. Reads of stdin come from "in".


> Now I want to use a pipe. What should I change in my program to be able
> to use it like this:
> cat in_file | my_prog | wc

^ ^
| \ Shell creates a pipe between my_prog's
| stdout and wc's stdin. my_prog writes
| to stdout go to wc's stdin.
|
Shell creates a pipe between cat's stdout and
my_prog's stdin. my_prog reads of stdin come
cat's stdout.

When I mention a program above, I really mean the process
that will exec the program. The programs themselves have
no knowledge of what is happening to their IO. (That's
why you don't have to change my_prog.) When I say stdin or
stdout, I really mean the file descriptors STDIN_FILENO or
STDOUT_FILENO, not the FILE structures.

--
Clem
"If you push something hard enough, it will fall over."
- Fudd's first law of opposition
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com