0% found this document useful (0 votes)
31 views3 pages

C++ - Is It Possible To Redirect Child Process's Stdout To Another File in Parent Process - Stack Overflow

The child process's stdout can be redirected to a file in the parent process by using the POSIX dup2 function between the fork and exec calls in the child process to duplicate the child's standard file descriptors over file descriptors that are opened to the target redirection file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

C++ - Is It Possible To Redirect Child Process's Stdout To Another File in Parent Process - Stack Overflow

The child process's stdout can be redirected to a file in the parent process by using the POSIX dup2 function between the fork and exec calls in the child process to duplicate the child's standard file descriptors over file descriptors that are opened to the target redirection file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Is it possible to redirect child process's stdout to another file in

parent process?
Asked 5 years, 7 months ago Modified 2 months ago Viewed 7k times

A child process runs a bin file, which is provided by Qualcomm. The child process is invoked by my
parent process, which is developed by me.
1
When the child process is running, it always prints lots pieces of logs in shell command.

So, am I able to redirect Qualcomm's outstream from stdout to another file in the parent process?

As you know, it's nearly impossible to push Qualcomm to update this bin file.

c++ linux

Share Improve this question Follow edited Oct 23, 2022 at 2:45 asked Aug 30, 2018 at 3:06
ggorlen Perlman Soong
51k 7 90 132 111 1 9

Is that really an appropriate duplicate? I think this question is about sending the child's output to an actual file, not
processing it in the parent's memory. – aschepler Aug 30, 2018 at 3:19

What about this stackoverflow.com/a/40667558/2193968 (replacing /dev/null with some other file) – Jerry Jeremiah
Aug 30, 2018 at 3:56

@JerryJeremiah Thanks. But in the demo code , the redirecting is involved in child process... – Perlman Soong
Aug 30, 2018 at 4:34

@aschepler yes, to redirect child process's outstream to an actual file . But the bin file running in child process can
not be modified, so is it possible for parent process to control the redirection of child process? – Perlman Soong
Aug 30, 2018 at 4:40

Would something like system(“./qualcommbinary > out.txt”) suffice? – Jeremy Friesner Aug 30, 2018 at 5:00

3 Answers Sorted by: Highest score (default)

The key piece here is the POSIX function dup2 , which lets you essentially replace one file descriptor
with another. And if you use fork (not system ), you actually have control of what happens in the child
5 process between the fork and the exec* that loads the other executable.

#include <cstdlib>
extern "C" {
#include <fcntl.h>
#include <unistd.h>
}
#include <stdexcept>
#include <iostream>

pid_t start_child(const char* program, const char* output_filename)


{
pid_t pid = fork();
if (pid < 0) {
// fork failed!
std::perror("fork");
throw std::runtime_error("fork failed");
} else if (pid == 0) {
// This code runs in the child process.
int output_fd = open(output_filename, O_WRONLY | O_CREAT |
O_TRUNC,S_IRUSR | S_IWUSR);
if (output_fd < 0) {
std::cerr << "Failed to open log file " << output_filename << ":"
<< std::endl;
std::perror("open");
std::exit(1);
}
// Replace the child's stdout and stderr handles with the log file
handle:
if (dup2(output_fd, STDOUT_FILENO) < 0) {
std::perror("dup2 (stdout)");
std::exit(1);
}
if (dup2(output_fd, STDERR_FILENO) < 0) {
std::perror("dup2 (stderr)");
std::exit(1);
}
if (execl(program, program, (char*)nullptr) < 0) {
// These messages will actually go into the file.
std::cerr << "Failed to exec program " << program << ":"
<< std::endl;
std::perror("execl");
std::exit(1);
}
}
return pid;
}

Share Improve this answer Follow edited Feb 18 at 14:49 answered Aug 30, 2018 at 12:12
Amjad Abdelrahman aschepler
3,482 1 14 20 71.7k 10 110 167

Small fix: O_CREATE should be O_CREAT I believe you also need to use STDOUT_FILENO, STDERR_FILENO
and not stdout, stderr directly. – Xavier Leclercq May 23, 2020 at 12:49

@XavierLeclercq Thanks, fixed those (plus missing #includes). – aschepler May 25, 2020 at 22:43

It is possible, for POSIX, because the POSIX shells do this. Executing a program has two steps, for
POSIX. First use fork to clone the parent process to create the child process. Then have the child
2 process use one of the exec family of system calls to execute the chosen program instead of the
program of the parent. In between those two steps the code executing for the child process can do
additional operations, which will affect the environment of the program to be executed. In particular, the
code could open a file descriptor to the file to be redirected to, close the stdout file descriptor, then
duplicate the file's file descriptor to the value (1) used for stdout.

Share Improve this answer Follow answered Aug 30, 2018 at 5:38
Raedwald
47.8k 46 153 243

You could create own pipes and attach them to the child process.

1. Create 3 pipes. they are going to replace stdin, stdout, stderr of the child.
0
2. fork()

3. In subprocess close() the parent end of the pipes. Close stdin,stdout and stderr.
4. The parent process close() the child end of the pipes.

5. dup2() the pipe ends in the child process that are intended to work as the new stdin,out,err

6. exec() the child.

Now you got all Output from the child to the pipe in the parent. Ofcourse you need to read from the pipes
that come from the child or it will block on any write to the stdout/stderr. For this you could use a
select() , poll() , epoll() multiplexing algorithm.

See

https://linux.die.net/man/2/pipe

https://linux.die.net/man/2/dup2

https://linux.die.net/man/2/execve

https://linux.die.net/man/2/fork

Share Improve this answer Follow answered Aug 30, 2018 at 6:59
schorsch_76
810 7 19

You might also like