重定向子进程的输出 [英] Redirecting the output of a child process

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

问题描述

有重定向子过程的输出的几种方法:

There are several ways of redirecting the output of a child process:


  1. 使用则freopen(3)

  2. 使用 DUP(3)

  3. 使用的popen(3)

  4. ...

你应该挑选一个,如果所有需要的则是执行一个子进程,并将它输出保存在一个给定的文件,pretty很像 LS> files.txt

What should one pick if all is wanted is to execute a child process and have it output saved in a given file, pretty much like the ls > files.txt works?

什么是通常使用的炮弹?

What is normally used by shells?

推荐答案

您可以发现什么你最喜欢的外壳使用由 strace的(1) ING你的shell。

You can discover what your favorite shell uses by strace(1)ing your shell.

在一个终端:

echo $$

在另一个终端:

strace -o /tmp/shell -f -p [PID from the first shell]

在再次第一终端:

ls > files.txt

在第二终端, ^ C 你的 strace的(1)命令,然后编辑的/ tmp /壳输出文件,看看它做作了重定向哪些系统调用。

In the second terminal, ^C your strace(1) command and then edit the /tmp/shell output file to see what system calls it made to do the redirection.

则freopen(3)操纵C标准IO FILE * 指针。所有这一切都将被丢弃在的execve(2)电话的另一边,因为它是保持的用户内存的。您可以使用此的之后的的的execve(2)电话,但是这将是别扭一般使用。

freopen(3) manipulates the C standard IO FILE* pointers. All this will be thrown away on the other side of the execve(2) call, because it is maintained in user memory. You could use this after the execve(2) call, but that would be awkward to use generically.

的popen(3)打开一个单向管(7)。这是有用的,但极其有限的 - 你无论是在标准输出描述符的的标准输入描述符。这将失败的类似 LS | grep的富|排序,其中输入和输出必须重定向。因此,这是一个糟糕的选择。

popen(3) opens a single unidirectional pipe(7). This is useful, but extremely limited -- you get either the standard output descriptor or the standard input descriptor. This would fail for something like ls | grep foo | sort where both input and output must be redirected. So this is a poor choice.

dup2(2)将管理文件描述符 - 内核实现的资源 - 因此它会持续整个的execve(2)通话的你可以设置尽可能多的文件描述符,因为你需要,这是很好的 LS>的/ tmp /输出2 - ;的/ tmp /错误或同时处理输入的的输出: LS |排序| uniq的

dup2(2) will manage file descriptors -- a kernel-implemented resource -- so it will persist across execve(2) calls and you can set up as many file descriptors as you need, which is nice for ls > /tmp/output 2> /tmp/error or handling both input and output: ls | sort | uniq.

有另一种机制: PTY(7)处理。在 forkpty(3) openpty(3),功能可以管理专门创建来处理新的伪终端设备另一个程序。该 UNIX环境高级编程,第二版书有一个非常好的 PTY 在其源$ C ​​$ C示例程序,但如果你无法理解为什么这将是有益的,看看在脚本(1)程序 - 它创建了一个新的伪终端,并使用它来所有的输入和输出记录,并从程序和存储的成绩单,以供以后播放或文档文件。你也可以用它来在互动节目脚本操作,类似于期待(1)

There is another mechanism: pty(7) handling. The forkpty(3), openpty(3), functions can manage a new pseudo-terminal device created specifically to handle another program. The Advanced Programming in the Unix Environment, 2nd edition book has a very nice pty example program in its source code, though if you're having trouble understanding why this would be useful, take a look at the script(1) program -- it creates a new pseudo-terminal and uses it to record all input and output to and from programs and stores the transcript to a file for later playback or documentation. You can also use it to script actions in interactive programs, similar to expect(1).

这篇关于重定向子进程的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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