Unix Programming - How to send STDOUT and STDERR to the parent process?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2004 > How to send STDOUT and STDERR to the parent process?





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 How to send STDOUT and STDERR to the parent process?
Ramon F Herrera

2004-12-05, 2:48 am

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);
Barry Margolin

2004-12-05, 8:47 pm

In 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 ***
Andrey Simonenko

2004-12-16, 7:45 pm

Ramon 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().
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com