运行 来自单个终端的 fifo 管道
Running fifo pipe from single terminal
我想知道是否有 运行 两个使用命名管道(即 fifo)的程序只执行一个程序。例如,此处提到的解决方案 [在两个管道之间发送字符串][1] 是否可以 运行 仅使用一个终端?有没有通过 运行ning reader.c
从 reader.c 调用 writer.c 和 运行 整个程序
编辑:我删除了我的代码,因为它有很多问题。我在对它们一无所知的情况下使用了很多功能。
关闭。
在您的 reader 程序中使用 popen() 函数 运行 writer.py:
https://linux.die.net/man/3/popen
popen 函数 returns 一个 FILE *,然后您可以将其与任何 C 缓冲的 I/O 函数一起使用。例如:
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv) {
FILE *fp;
if((fp = popen("/path/to/writer.py", "r")) == NULL) {
// handle error in some way
perror("popen");
exit(1);
}
size_t numbytes;
char buffer[255];
// Here we read the output from the writer.py and rewrite it to
// stdout. The result should look the same as if you ran writer.py
// by itself.. not very useful. But you would replace this with code
// that does something useful with the data from writer.py
while((numbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
fwrite(buffer, 1, numbytes, stdout);
// should check for error here
}
pclose(fp);
exit(0);
}
PS: 我没有编译或 运行 这个程序,它只是一个给你想法的例子......但它应该工作。另外.. 我注意到你在一个地方说 writer.c 而在另一个地方说 writer.py。编写器用什么语言编写并不重要。只要传递给 popen() 的程序路径名导致输出被写入标准输出,它就可以工作。
我想知道是否有 运行 两个使用命名管道(即 fifo)的程序只执行一个程序。例如,此处提到的解决方案 [在两个管道之间发送字符串][1] 是否可以 运行 仅使用一个终端?有没有通过 运行ning reader.c
从 reader.c 调用 writer.c 和 运行 整个程序编辑:我删除了我的代码,因为它有很多问题。我在对它们一无所知的情况下使用了很多功能。
关闭。
在您的 reader 程序中使用 popen() 函数 运行 writer.py:
https://linux.die.net/man/3/popen
popen 函数 returns 一个 FILE *,然后您可以将其与任何 C 缓冲的 I/O 函数一起使用。例如:
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv) {
FILE *fp;
if((fp = popen("/path/to/writer.py", "r")) == NULL) {
// handle error in some way
perror("popen");
exit(1);
}
size_t numbytes;
char buffer[255];
// Here we read the output from the writer.py and rewrite it to
// stdout. The result should look the same as if you ran writer.py
// by itself.. not very useful. But you would replace this with code
// that does something useful with the data from writer.py
while((numbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
fwrite(buffer, 1, numbytes, stdout);
// should check for error here
}
pclose(fp);
exit(0);
}
PS: 我没有编译或 运行 这个程序,它只是一个给你想法的例子......但它应该工作。另外.. 我注意到你在一个地方说 writer.c 而在另一个地方说 writer.py。编写器用什么语言编写并不重要。只要传递给 popen() 的程序路径名导致输出被写入标准输出,它就可以工作。