[英]How to stop fgets reading from FIFO file
I have two processes, first reading from stdin and writing to FIFO file, second is waiting for message from FIFO file. 我有两个过程,第一个过程是从stdin读取并写入FIFO文件,第二个过程是等待FIFO文件中的消息。
I am using this program structure: 我正在使用此程序结构:
while(run)
{
fgets(string, sizeof(string), fp);
}
and when the sentinel (run) change to 0 to stop process, it didn't really stop because it stuck in fgets()
function. 当哨兵(运行)更改为0以停止进程时,它并没有真正停止,因为它卡在了fgets()
函数中。 How can I solve it? 我该如何解决?
I want program to waits for the message when sentinel change to 0 then immediately stop the program. 我希望程序在前哨更改为0时等待消息,然后立即停止程序。
I think here your fifo is opened in Blocking mode , So in fgets()
in read call if data is not available that call will be blocked till fifo has some new data. 我认为您的fifo是在“ 阻止”模式下打开的,因此,在fgets()
中,如果没有可用数据,则在read调用中调用将被阻止,直到fifo有一些新数据为止。
SO to avoid this open FIFO in non blocking mode by using O_NONBLOCK
in open call. SO通过在打开调用中使用O_NONBLOCK
来避免在非阻塞模式下打开此FIFO。
Read more here . 在这里阅读更多。 http://www.tldp.org/LDP/lpg/node19.html http://www.tldp.org/LDP/lpg/node19.html
This way if no data is available fgets with return with error instead of stucking that call. 这样,如果没有可用数据,则fgets将返回错误,而不是阻塞该调用。 In next while loop check, run
will be 0 so it come out from this loop. 在下一个while循环检查中, run
将为0,因此它将从此循环中出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.