简体   繁体   中英

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.

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. How can I solve it?

I want program to waits for the message when sentinel change to 0 then immediately stop the program.

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.

SO to avoid this open FIFO in non blocking mode by using O_NONBLOCK in open call.

Read more here . 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. In next while loop check, run will be 0 so it come out from this loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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