简体   繁体   English

C - 重定向子进程的IO

[英]C - Redirecting IO of Child Process

I am trying to redirect the IO of a child process (after fork() ) into a file, and I can't figure out why it isn't working. 我试图将子进程的IO(在fork()之后fork()重定向到一个文件中,我无法弄清楚它为什么不起作用。

Here's what I've done: 这就是我所做的:

if(fork() == 0){
    execv(exe, (char*[]){ exe, "> temp.exe" });
    ...

And the executable runs, but it doesn't redirect to the file. 可执行文件运行,但它不会重定向到该文件。 I would appreciate it if anyone could explain what am I doing wrong, and how I should do it. 如果有人能解释我做错了什么,以及我该怎么做,我将不胜感激。 I'm getting a feeling I need to redirect before the execv() but I have no idea how to. 我有一种感觉,我需要在execv()之前重定向,但我不知道如何。

Thanks in advance! 提前致谢!

Shell redirections (like > file ) are implemented by the shell. Shell重定向(如> file )由shell实现。 By using execve() , you are bypassing the shell; 通过使用execve() ,你绕过shell; the child process will see "> temp.exe" in argv , and will attempt to process it as an argument. 子进程将在argv看到"> temp.exe" ,并将尝试将其作为参数进行处理。

If you want to redirect output to a file, the easiest approach will be to implement that redirection yourself by opening the file after forking and using dup2() to move its file descriptor to standard output: 如果要将输出重定向到文件,最简单的方法是通过在分叉后打开文件并使用dup2()将其文件描述符移动到标准输出来自己实现重定向:

if (fork() == 0) {
    int fd = open("temp.exe", O_CREAT | O_WRONLY, 0666);
    if (fd < 0) { handle error... exit(255); }
    dup2(fd, 1);
    close(fd);
    execv(exe, ...);
}

The execX() family of calls does not have the same flexibility as, say system() or popen(). execX()系列调用与system()或popen()没有相同的灵活性。 These latter methods call shell to do the interpretation of the command. 后面这些方法调用shell来执行命令的解释。

The arguments to the execX call are the exact path of the program you want to run and the arguments you want to give to that program. execX调用的参数是您要运行的程序的确切路径以及要为该程序提供的参数。 Any "shell" features such as redirection you have to implement yourself before calling execX. 任何“shell”功能,例如重定向,你必须在调用execX之前自己实现。

Alternatively, you can let shell actually do the work, execp("sh","sh",myexe+" >test.txt"); 或者,你可以让shell实际完成工作, execp("sh","sh",myexe+" >test.txt"); , but that is lazy, and then why not just use system anyway? ,但这是懒惰,然后为什么不只是使用系统?

Two very useful methods are pipe() and dup2(): pipe allows you to create pipes to your host program; 两个非常有用的方法是pipe()和dup2():pipe允许你为宿主程序创建管道; dup2 lets you set a scenario where the program being executed thinks that it is writing to stdout (1), or reading from stdin (0), but is actually writing or reading to a file or pipe that you created. dup2允许您设置一个场景,其中正在执行的程序认为它正在写入stdout(1),或从stdin(0)读取,但实际上正在写入或读取您创建的文件或管道。

You will get a long way by reading the man pages for pipe and dup2, or in google looking for exec pipe and dup2, so I won't take your enjoyment away by writing a full implementation here. 通过阅读管道和dup2的手册页,或者在谷歌中查找exec管道和dup2,您将获得很长的路要走,因此我不会在此处编写完整的实现来消除您的乐趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM