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
;