c++ - 更改子进程中的 iostream

标签 c++ linux fork iostream

现在,我正在做一个项目,我需要在 Linux 中使用 C++ 启动一个子进程来执行一个新程序,我需要重定向标准输入和输出(在 C++ 中,它们是 cincout) 到一个文件。这意味着在子进程中,标准输入和输出都是文件。子进程将从文件(名称为 input.txt)读取输入,并输出到文件(名称为 output.txt)。

通过使用 cin.rdbuf()cout.rdbuf(),我实际上可以重定向 cincout 在父进程中。但是当子进程启动一个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/

相关文章:

c++ - 使用std::find和std::string数组时出现的问题

c++ - 制作代码 "forwarding referencable"

linux - 如果不是当前文件,则重命名文件

c - 带有 fork 的空 for 循环导致奇怪的行为

c++ - 返回类型是否被视为右值?

c++ - QCreator : how to create debug build configuration

PHP 执行 linux 命令终止得太快?

linux - 使用 BASH 从一行中取出最后一个单词并将其添加到开头

c - 管道排序并返回主

boost - 使用boost进程库防止子进程继承父进程打开的TCP端口