[LINUX] A little more about FIFO

What is FIFO

The name of the pipe is FIFO. It's over. ls -l | grep keyword In, | is the pipe, and the file system global name given to it is the FIFO. The properties are the same as pipes. In the above example

  1. grep starts and blocks with open (O_RDONLY) of pipe
  2. ls opens the pipe with open (O_WRONLY)
  3. ls writes output to pipe ()
  4. grep is unblocked and the input is read ()
  5. ls close the pipe ()
  6. grep closes the pipe ()

It becomes a movement like. It blocks with open (), not with read () or write (). This is written in Japanese man pages, but it's hard to understand. In other words, this is it.

Files that cannot be searched (pipes and FIFOs) start reading from the current position, and if there is no data, it is regarded as EOF and 0 is returned.

Therefore, read () does not block. Normally, this is not a problem, but if you want to set up a thread for interprocess communication and wait for data to arrive in a loop, you need to block it with open () to prevent CPU waste. However, as a human being, I don't want to open () / close () in each loop. It's best if read () can block.

So message queue

The POSIX message queue is a queue that can store a finite number of fixed-size messages. Since the blocking attribute is added at the time of creation, even if the queue is received (if it is a blocking queue), it will block until the message arrives.

Therefore, it is suitable for processing that waits for the arrival of a message in a thread loop. It does not generate SIGPIPE like a pipe, and it does not lose data when both ends of the FIFO are closed (it retains data until shutdown because it has a kernel life).

Data exchange between processes

It seems that there are other methods of interprocess communication such as semaphores, signals, and shared memory, but if you want to exchange data, I think it will be a FIFO, a queue, or a domain socket. I think each has its advantages and disadvantages, so I'd like to summarize them in the future.

Recommended Posts

A little more about FIFO
A little more about references ~ Using Python and Java as examples ~
I thought a little about TensorFlow's growing API
A memorandum about matplotlib
A memorandum about Nan.
A note about __call__
A note about subprocess
A note about mprotect (2)
A little addictive information about Cliff, the CLI framework
Read a little more arch / arm / boot / compressed / Makefile
(For myself) Flask_2 (list and for, extends, and a little more)
A note about KornShell (ksh)
A little deeper story about blockchain, ticking the digital world
A memorandum about correlation [Python]
A note about TensorFlow Introduction
A memorandum about Python mock
A little stuck with chainer
A note about [python] __debug__
Let's use Watson IoT Platform Data Store Connector a little more
After researching the Python library, I understood a little about egg.info.
A refreshing story about Python's Slice
[Java] A little mysterious split behavior
Python: A Note About Classes 1 "Abstract"
A note about get_scorer in sklearn
A sloppy story about Python's Slice
I have a question about whitespace
A little script for malware self-defense
Make C compilation a little easier
A story about using Python's reduce
A note about mock (Python mock library)