现在,我正在做一个项目,我需要在 Linux 中使用 C++ 启动一个子进程来执行一个新程序,我需要重定向标准输入和输出(在 C++ 中,它们是 cin
和 cout
) 到一个文件。这意味着在子进程中,标准输入和输出都是文件。子进程将从文件(名称为 input.txt
)读取输入,并输出到文件(名称为 output.txt
)。
通过使用 cin.rdbuf()
和 cout.rdbuf()
,我实际上可以重定向 cin
和 cout
在父进程中。但是当子进程启动一个execl()
命令时它不起作用。看起来子进程执行完execl()
命令后,标准输入输出恢复正常。
谁能帮我解决这个问题?这几天我一直很迷茫,找不到出路。
代码如下:
// 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();
}
//执行器.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;
};
附言/home/test
是一个简单的程序,它从cin
读取并输出到cout
;
最佳答案
您需要在 fork()
之后重定向文件描述符 0
(标准输入)和 1
(标准输出):
switch (fork()) {
case 0: {
close(0);
if (open(name, O_RDONLY) < 0) {
deal_with_error();
}
...
您可能想要打开在父进程中指向的文件。轻松打开文件可能会使错误处理更容易。在这种情况下,您将使用 dup2()
将正确的文件描述符与文件相关联。
关于c++ - 更改子进程中的 iostream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13736990/