Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-22-2004, 11:36 PM   #1
LQ Newbie
Registered: Jan 2004
Posts: 18

Rep: Reputation: 0
redirectoring standard output

My program will do the pixel processing for output of other program.
So, I need redirect the output of the program from standard Output to my specified pipe
I do as this way in program:
But it cannot work, I think execlp take place the my program, so dup2 cannot do its work in new process.

So, how to do?

if use the buffer, /dev/tty, how to do?
Old 02-23-2004, 12:14 AM   #2
Registered: Aug 2003
Location: michigan
Distribution: slackware
Posts: 59

Rep: Reputation: 15
Perhaps an easier implementation might be just to let the shell handle the redirection. If you are trying to grab the standard out of the first program, write your program to take input from standard in and just execute:

first_program | your_program

If you need or want to handle redirection youself, and have access to the code in the first program, create a pipe with the pipe(int*) call in the first program, fork, change the correct file descriptors in both the parent and the child, then run exec in the child to execute the second program.
Old 02-23-2004, 12:25 AM   #3
LQ Newbie
Registered: Jan 2004
Posts: 18

Original Poster
Rep: Reputation: 0
I cannot reach the first programs code and i wish the first program is any specified one.

maybe you siad right, the shell way is easiest way.
Old 02-23-2004, 02:02 AM   #4
Registered: Feb 2004
Location: Vienna/Austria
Distribution: Debian Unstable
Posts: 36

Rep: Reputation: 15
If you want really good buffered communication between two processes:
- Create a fifo file (mkfifo)
- freopen stdout to the fifo

That works very well for me, I do it with a client-server calculation application. I post a little example if you want
Old 02-23-2004, 01:46 PM   #5
LQ Newbie
Registered: Jan 2004
Posts: 18

Original Poster
Rep: Reputation: 0
need your sample

do you means create a FIFO out the program, then let "first program > fifo"
and " my program" open and read fifo it. Sounds good. if possible, your sample code is appreiciate
Old 02-23-2004, 06:35 PM   #6
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
not sure if that is just in the example, but you are duplicating the read end of the pipe to standard output. assuming that the program you want to run is named "that_prog" and your program is named "my_prog", you would have the following:

in my_prog:

create a set of pipes
fork() a child process
[child] dup2 the WRITE end of the pipe to STDOUT
[child]close READ end of pipe
[child] execlp("that_prog", args, NULL)
back in parent
close the WRITE end of the pipe
read from the READ end of pipe to get that_prog's output
Old 02-25-2004, 02:32 AM   #7
LQ Newbie
Registered: Jan 2004
Posts: 18

Original Poster
Rep: Reputation: 0
i do the same way

yes, i think i did the same way, but it cannot work. I think the execlp will take place the curent child process, so dup2 cannot effect


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
standard output to screen and ffile hypexr Linux - General 2 10-27-2005 10:10 AM
how to restore standard output user_linux Linux - Newbie 2 06-17-2005 02:29 PM
standard output/system mail? gmon72 Linux - Newbie 1 10-13-2004 02:11 PM
Standard output - Tcl/Tk widget liguorir Linux - Software 0 06-03-2004 01:15 PM
how to redirecting the standard output in program phil123 Linux - General 3 02-23-2004 12:02 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:56 PM.

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