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
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.
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).
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