我想知道是否还有使用命名管道运行两个程序,即fifo,只执行一个程序。例如,这里提到的解决方案[在两个管道之间发送字符串] [1]只能使用一个终端运行吗?无论如何,从reader.c调用writer.c并只运行reader.c运行整个程序。
编辑:我删除了我的代码,因为它有很多问题。我在使用很多功能而对它们一无所知。CLOSED。
答案 0 :(得分:1)
使用popen()函数从您的阅读器程序中运行writer.py:
https://linux.die.net/man/3/popen
popen函数返回一个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()的程序路径名导致输出被写入stdout,它就会起作用。