简体   繁体   中英

Cleaning/flushing of named pipe

I create FIFO to communicate between unrelated processes. In my project there is no way to break infinite loop in which the program runs. So, I can't unlink the FIFO.

I thought that I could remove and recreate with same name if the FIFO exist beforehand in writer process. But what if reader process open the FIFO before the write process has a chance to remove and recreate it? I could use sleep in this regard but it seems not efficient.

Moreover, I also thought that I could suspend the reader process until it receives a signal from the writer process but PID of the reader process is not known. Think them as two separate and different bash scripts. No sooner does OS start up than they start executions.

  • Is there a way to clean/flush the FIFO?
  • open() is used to open FIFO. If I close the FIFO and reopen it to write in a loop, is it cleaned/flushed? (guarantee?)

@Edit,

Checking inode always yields same inode number. I mean even though I use rm <fifo.name> via terminal, then recreate FIFO, same inode number is given.

FIFO = '/tmp/test.fifo'
fd = os.open(FIFO, os.O_RDONLY)

info = os.fstat(fd)
print("inf fstat " + str(info.st_ino))

statinfo = os.stat(FIFO)
print("inf stat " + str(statinfo.st_ino))

Output:

inf fstat 521406
inf stat 521406

stat information:

pi@raspberrypi:/tmp $ stat /tmp/test.fifo 
  File: /tmp/test.fifo
  Size: 0           Blocks: 0          IO Block: 4096   fifo
Device: b307h/45831d    Inode: 521406      Links: 1
Access: (0644/prw-r--r--)  Uid: ( 1000/      pi)   Gid: ( 1000/      pi)
Access: 2018-07-27 03:48:05.958234732 -0400
Modify: 2018-07-27 04:25:52.925375655 -0400
Change: 2018-07-27 04:25:52.925375655 -0400
 Birth -

As mentioned in a comment , here's a snippet of output from Ubuntu 16.04, previously uploaded to TextUploader :

soner@ubuntu:/tmp$ mkfifo test.fifo
soner@ubuntu:/tmp$ stat test.fifo 
  File: 'test.fifo'
  Size: 0           Blocks: 0          IO Block: 4096   fifo
Device: 801h/2049d  Inode: 2228362     Links: 1
Access: (0664/prw-rw-r--)  Uid: ( 1000/   soner)   Gid: ( 1000/   soner)
Access: 2018-07-26 23:41:31.482184467 +0300
Modify: 2018-07-26 23:41:31.482184467 +0300
Change: 2018-07-26 23:41:31.482184467 +0300
 Birth: -
soner@ubuntu:/tmp$ rm test.fifo 
soner@ubuntu:/tmp$ mkfifo test.fifo
soner@ubuntu:/tmp$ stat test.fifo 
  File: 'test.fifo'
  Size: 0           Blocks: 0          IO Block: 4096   fifo
Device: 801h/2049d  Inode: 2228362     Links: 1
Access: (0664/prw-rw-r--)  Uid: ( 1000/   soner)   Gid: ( 1000/   soner)
Access: 2018-07-26 23:41:46.766125062 +0300
Modify: 2018-07-26 23:41:46.766125062 +0300
Change: 2018-07-26 23:41:46.766125062 +0300
 Birth: -
soner@ubuntu:/tmp$ 

Before reading from the FIFO, the reader can call os.fstat(fd) and os.stat(filename) , and check whether the inode numbers are the same. If they're not, it means the writer has removed and recreated the FIFO, so it should reopen it before reading.

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