简体   繁体   English

在FIFO管道为空之前如何使C程序阻塞?

[英]How to make C program block until FIFO pipe is empty?

I'm doing IPC using named (FIFO) pipes and I would like to coordinate that program can only write into the pipe when program reading the pipe has read the previously written data out from the pipe. 我正在使用命名(FIFO)管道进行IPC,我想协调该程序只能在读取管道的程序读取管道中先前写入的数据时写入管道。 So I would like to block the write until the the pipe is empty. 所以我想阻止写入,直到管道为空。 Is this possible? 这可能吗?

One option that I though is that write function blocks when the pipe is full. 我可以选择的一个选项是当管道已满时写入功能会阻塞。 But I would like to do this to much smaller amounts of data than the pipe size in Linux. 但我想这样做的数据量比Linux中的管道大小要少得多。 Eg I would like that program can only write 20 bytes and then it waits until other end has read the data. 例如,我希望该程序只能写入20个字节然后等待,直到另一端读取数据。 I think you can not shrink named pipes to be so small. 我认为你不能缩小命名管道这么小。 (Minimum size seems to be page file size (4096 bytes)?) (最小大小似乎是页面文件大小(4096字节)?)

Thanks! 谢谢!

A possible solution is to have the reading process sending a signal to the writing process when it has read some data. 一种可能的解决方案是让读取过程在读取一些数据时向写入过程发送信号。 You can do this using kill() to send SIGTERM to the writer, since SIGTERM can be catched and handled. 您可以使用kill()将SIGTERM发送到编写器,因为可以捕获和处理SIGTERM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM