Code Comments
Programming Forum and web based access to our favorite programming groups.Hello:
I have been using for a long time the classical
procedure to fork a child and send it STDOUT to the
parent (see below).
This time, however, I need to send both stdout and stderr
to the parent process.
How can I do that??
-Ramon F Herrera
-------------------------------------------------------------
pipe(pipefd);
if (fork() == 0) { // Child
close(STDOUT);
close(pipefd[READ]);
dup(pipefd[WRITE]);
execv(decoder, arguments);
}
else { // Parent
close(STDIN);
close(pipefd[WRITE]);
dup(pipefd[READ]);
}
wait(NULL);
Post Follow-up to this messageIn article <c9bc36ff.0412042020.25b3e04f@posting.google.com>,
ramon@conexus.net (Ramon F Herrera) wrote:
> Hello:
>
> I have been using for a long time the classical
> procedure to fork a child and send it STDOUT to the
> parent (see below).
>
> This time, however, I need to send both stdout and stderr
> to the parent process.
>
> How can I do that??
>
> -Ramon F Herrera
>
> -------------------------------------------------------------
>
>
> pipe(pipefd);
> if (fork() == 0) { // Child
> close(STDOUT);
STDOUT should be STDOUT_FILENO.
> close(pipefd[READ]);
> dup(pipefd[WRITE]);
Replace that line with:
dup2(STDOUT_FILENO, pipefd[WRITE]);
dup2(STDERR_FILENO, pipefd[WRITE]);
close(pipefd[WRITE]);
> execv(decoder, arguments);
> }
> else { // Parent
> close(STDIN);
And that should be STDIN_FILENO.
> close(pipefd[WRITE]);
> dup(pipefd[READ]);
> }
>
> wait(NULL);
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Post Follow-up to this messageRamon F Herrera <ramon@conexus.net> wrote:
> Hello:
>
> I have been using for a long time the classical
> procedure to fork a child and send it STDOUT to the
> parent (see below).
>
> This time, however, I need to send both stdout and stderr
> to the parent process.
Use the sane technique. You can create a new pipe for stderr or dup2
stderr file descriptor number to the write end of the pipe used for
stdout.
In first case you will be able to distinguish stderr and stdout outputs
in second case you will see output of stderr and stdout gathered together
in the read end of the pipe, but, note, that output to stderr is not
buffered.
>
> pipe(pipefd);
> if (fork() == 0) { // Child
> close(STDOUT);
> close(pipefd[READ]);
> dup(pipefd[WRITE]);
> execv(decoder, arguments);
> }
By the way, there are three macro variables STDIN_FILENO, STDOUT_FILENO
and STDERR_FILENO.
> else { // Parent
> close(STDIN);
> close(pipefd[WRITE]);
> dup(pipefd[READ]);
> }
Check return code from fork().
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.