如何使该管道在 c++ 中工作?
我正在用 C++ 编写 shell。它需要能够将输出从一件事传送到另一件事。例如,在 Linux 中,您可以通过执行 cat textfile | 将文本文件通过管道传输到 more。更多
。
我将一件事通过管道传输到另一件事的函数是这样声明的:
void pipeinput(string input, string output);
我发送“cat textfile”作为输入,“more”作为输出。
在展示如何创建管道的 C++ 示例中,使用了 fopen()。我将什么作为输入发送给 fopen()
?我见过使用 dup2
进行管道传输且未起诉 dup2
的 C++ 示例。 dup2 是做什么用的?你怎么知道你是否需要使用它?
I am programming a shell in c++. It needs to be able to pipe the output from one thing to another. For example, in linux, you can pipe a textfile to more by doing cat textfile | more
.
My function to pipe one thing to another is declared like this:
void pipeinput(string input, string output);
I send "cat textfile" as the input, and "more" as the output.
In c++ examples that show how to make pipes, fopen() is used. What do I send as my input to fopen()
? I have seen c++ examples of pipeing using dup2
and without suing dup2
. What's dup2 used for? How do you know if you need to use it or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下 popen(3),这是避免 execvp 的一种方法。
Take a look at popen(3), which is a way to avoid execvp.
对于简单的两个命令管道,您建议的函数接口可能就足够了。对于N级管道的一般情况,我认为它不够灵活。
pipe()
系统调用用于创建管道。在上下文中,您将在分叉之前创建一个管道。两个进程之一将安排管道的写入端成为其标准输出(可能使用 dup2()),然后关闭管道最初返回的两个文件描述符()。然后它将执行写入管道的命令(示例中的cat textfile
)。另一个进程将安排管道的读取 enc 成为其标准输入(可能再次使用 dup2() ),然后关闭最初由 pipeline 返回的两个文件描述符( )。然后它将执行从管道读取的命令(在示例中为more
)。当然,仍然会有第三个进程 - 父 shell 进程 - 它派生出一个子进程来运行整个管道。如果您想跟踪管道中每个进程的状态,您可能会决定稍微改进机制;那么流程的组织就有点不同了。
For a simple, two-command pipeline, the function interface you propose may be sufficient. For the general case of an N-stage pipeline, I don't think it is flexible enough.
The
pipe()
system call is used to create a pipe. In context, you will be creating one pipe before forking. One of the two processes will arrange for the write end of the pipe to become its standard output (probably usingdup2()
), and will then close both of the file descriptors originally returned bypipe()
. It will then execute the command that writes to the pipe (cat textfile
in your example). The other process will arrange for the read enc of the pipe to become its standard input (probably usingdup2()
again), and will then close both of the file descriptor originally returned bypipe()
. It will then execute the command that reads from the pipe (more
in your example).Of course, there will be still a third process around - the parent shell process - which forked off a child to run the entire pipeline. You might decide you want to refine the mechanisms a bit if you want to track the statuses of each process in the pipeline; the process organization is then a bit different.
fopen()
不用于创建管道。它可以用来打开文件描述符,但不是必须这样做。管道是通过
pipe(2)
调用创建的,之前分叉进程。在执行命令之前,子进程需要进行一些文件描述符管理。请参阅pipe
文档中的示例。fopen()
is not used to create pipes. It can be used to open the file descriptor, but it is not necessary to do so.Pipes are created with the
pipe(2)
call, before forking off the process. The subprocess has a little bit of file descriptor management to do beforeexec
ing the command. See the example inpipe
's documentation.