在子进程中更改iostream [英] Change iostreams in child process

查看:99
本文介绍了在子进程中更改iostream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

通过使用cin.rdbuf()cout.rdbuf(),我实际上可以在父进程中重定向cincout.但是,当子进程启动execl()命令时,它将不起作用.子进程执行execl()命令后,标准输入和输出似乎恢复正常.

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.

代码如下:

//main.cpp

//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

//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是一个简单的程序,它从cin读取并输出到cout;

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

推荐答案

您需要在孩子fork()之后重定向文件描述符0(标准输入)和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();
    }
    ...

您可能要打开在父进程中定向的文件.随时打开文件可能会使错误处理更容易.在这种情况下,您可以使用dup2()将正确的文件描述符与文件相关联.

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.

这篇关于在子进程中更改iostream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆