c - 将一个命令的输出管道化为另一个命令的输入

标签 c pipe

<分区>

我有以下任务:

Write a C program to read the names of two (or more) executable programs, and redirect the output of the first program to the input of the second program, output of the second program to the input of the third program, and so on...

我至少了解 shell 中管道的基础知识。但是,我不明白如何使用 C 管道在 C 中实现此任务。我不知道如何将一个程序的输出作为另一个程序的输入等等。例如,在 shell 中:

ls | wc | ./add

此处ls列出文件,wc给出列出文件的计数,./add添加给出的数字>wc.

我从哪里开始在 C 程序中实现管道?

最佳答案

看来您需要创建一个程序来完成 shell 工作的一个简单案例:创建并执行命令管道,然后输出结果。

要做到这一点,您需要了解 SIGPIPE、子进程处理、输入/输出重定向、文件描述符、fork()exec()wait() 等。

This Linux Documentation Project article on creating pipelines应该可以帮助您走上正确的道路。

关于c - 将一个命令的输出管道化为另一个命令的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12032323/

相关文章:

C 编程温度转换

R : Using shortcuts to insert operators writes to a different file

linux - 管道 Bash 命令输出到标准输出和变量

c - 流水线来自同一 parent 的多个 child

c - fread 函数如何从 stdin 获取输入

c - 最近指针不为空时出现空指针取消引用错误

带有结构指针的 C 结构

c - RISCV 32 位基础和 64 位扩展

c - 使用 C 在 Linux Shell 中实现管道

c - 尝试在 C shell 中实现管道挂起并且未运行命令