考虑:
fifo.c#include
#include
#include
#include
int main (void)
{
// Array to send
int arr[] = {2,4,6,8};
int len = 4;
// Create FIFO
char filename[] = "fifo.tmp";
int s_fifo = mkfifo(filename, S_IRWXU);
if (s_fifo != 0)
{
printf("mkfifo() error: %d\n", s_fifo);
return -1;
}
FILE * wfd = fopen(filename, "w");
if (wfd < 0)
{
printf("open() error: %d\n", wfd);
return -1;
}
// Write to FIFO
for (int i=0; i
{
int s_write = fprintf(wfd, "%d ", arr[i]);
if (s_write < 0)
{
printf("fprintf() error: %d\n", s_write);
break;
}
}
// Close and delete FIFO
fclose(wfd);
unlink(filename);
}
先进先出.py
^{pr2}$
首先运行writer(c),它将一直阻塞,直到读卡器(python)打开并读取数据。然后运行读取器,两个进程都将终止。在$ python fifo.py
[2, 4, 6, 8]
注意事项:一些更好的错误处理可能是有益的(例如,如果命名的fifo存在,因为c程序没有干净地退出)。在
这有点效率低下,因为您要将整数值转换为字符串表示形式并发送它。我使用它是因为空格分隔符很容易使用,但是您可以考虑发送整数值本身,并在读取器端使用固定宽度的解析。在