Re: Capture stdout and stderr from child process
From: Barry Margolin (barry.margolin_at_level3.com)
Date: 08/29/03
- Next message: Derk Gwen: "Re: Capture stdout and stderr from child process"
- Previous message: Peter Tan: "Capture stdout and stderr from child process"
- In reply to: Peter Tan: "Capture stdout and stderr from child process"
- Next in thread: Derk Gwen: "Re: Capture stdout and stderr from child process"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 29 Aug 2003 19:24:51 GMT
In article <151c6c1d.0308291106.56788cbb@posting.google.com>,
Peter Tan <shiuyuan@yahoo.com> wrote:
> I am trying to capture standard output and error from unix shell
>running as a child process. I understand that to do so, I need to use
>pseudo terminal. But the problem is the slave side combine stdout and
>stderr nad print them out together on the terminal. My question is if
>there is a way to separate stderr and stdout?
You don't need to use a pseudo terminal for this, just use two pipes.
int stdout_pipe[2];
int stderr_pipe[2];
pipe(stdout_pipe);
pipe(stderr_pipe);
switch (fork()) {
-1: /* handle fork error */
break;
0: /* child */
close(stdout_pipe[0]);
dup2(STDOUT_FILENO, stdout_pipe[1]);
close(stdout_pipe[1]);
close(stderr_pipe[0]);
dup2(STDERR_FILENO, stderr_pipe[1]);
close(stderr_pipe[1]);
execl("/bin/sh", ...);
break;
default:
close(stdout_pipe[1]);
close(stderr_pipe[1]);
/* Now you can read from stdout_pipe[0] and stderr_pipe[0] to get the
child's stdout and stderr */
...
}
-- Barry Margolin, barry.margolin@level3.com Level(3), Woburn, MA *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups. Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
- Next message: Derk Gwen: "Re: Capture stdout and stderr from child process"
- Previous message: Peter Tan: "Capture stdout and stderr from child process"
- In reply to: Peter Tan: "Capture stdout and stderr from child process"
- Next in thread: Derk Gwen: "Re: Capture stdout and stderr from child process"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|