Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How to send STDOUT and STDERR to the parent process?
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);

Report this thread to moderator Post Follow-up to this message
Old Post
Ramon F Herrera
12-07-04 09:13 AM


Re: How to send STDOUT and STDERR to the parent process?
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 ***

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Margolin
12-07-04 09:13 AM


Re: How to send STDOUT and STDERR to the parent process?
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().

Report this thread to moderator Post Follow-up to this message
Old Post
Andrey Simonenko
12-15-04 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:21 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.