Change iostreams in child process

2020-04-11 14:09发布

问题:

Right now, I'm working on a project in which I need to start a child process to execute a new program in Linux using C++, and I need to redirect standard input and output (as in C++, they are cin and cout) to a file. This means that in the child process, the standard input and output are both files. The child process will read input from a file (whose name would be input.txt), and output to a file (whose name would be output.txt).

By using cin.rdbuf() and cout.rdbuf(), I can actually redirect cin and cout in the parent process. But it doesn't work when the child process starts an execl() command. It seems that after the child process executes the execl() command, the standard input and output return to normal.

Can anyone can help me with this problem? I've been confused for the past few days and can't find a way out.

Code is as follows:

//main.cpp

#include<sys/types.h>
#include<sys/time.h>
#include<sys/wait.h>
#include<sys/ptrace.h>
#include<sys/syscall.h>
#include<string>
#include"executor.cpp"
int main(int argc, char*argv[])
{
executor ex;
ex.set_path("/home/test");
ex.run_program();
}

//executor.cpp

#include<sys/types.h>
#include<sys/time.h>
#include<sys/wait.h>
#include<sys/ptrace.h>
#include<sys/syscall.h>
#include<string.h>
#include<unistd.h>
#include<iostream>
#include<fstream>

using namespace std;
class executor{
public:
void run_program()
{
    char p[50];
    strcpy(p,path.c_str());
    cpid = fork();
    if(cpid == 0)
    {
                    ifstream file("/home/openjudge/data.txt");
            if(!file) cout<<"file open failed\n";
            streambuf* x = cin.rdbuf(file.rdbuf());
        ptrace(PTRACE_TRACEME,0,NULL,NULL);
        execl(p,"test","NULL);
        cin.rdbuf(x);
        cout<<"execute failed!\n";
    }
    else if(cpid > 0)
    {
        wait(NULL);
        cout<<"i'm a father\n";
    }
}
void set_path(string p)
{
    path = p;
}
private:
int cpid;
string path;
};

P.S. /home/test is a simple program which reads from cin and outputs to cout;

回答1:

You need to redirect the file descriptors 0 (standard input) and 1 (standard output) after fork() your child:

switch (fork()) {
case 0: {
    close(0);
    if (open(name, O_RDONLY) < 0) {
        deal_with_error();
    }
    ...

You might want to open the files directed to in the parent process. Having the files readily open probably makes error handling easier. In this case you'd use dup2() to associate the correct file descriptor with the file.