Interprocess Communication via file

前端 未结 1 1344
梦如初夏
梦如初夏 2021-01-03 03:41

When I echo into files at some arbitrary locations in Linux, i.e. echo > /tmp/file, some running processes respond. Is this IPC via file pipe?

Does

1条回答
  •  伪装坚强ぢ
    2021-01-03 03:53

    If you want use a file to communicate with another process, you should have a look at man fifo.

    I'll report here just the first lines:

    NAME
           fifo - first-in first-out special file, named pipe
    
    DESCRIPTION
           A FIFO special file (a named pipe) is similar to a pipe, except that it
           is accessed as part of the file system.  It can be opened  by  multiple
           processes  for  reading or writing.  When processes are exchanging data
           via the FIFO, the kernel passes all data internally without writing  it
           to the file system.  Thus, the FIFO special file has no contents on the
           file system; the file system entry merely serves as a  reference  point
           so that processes can access the pipe using a name in the file system.
    

    I think this is what you need.

    Just think to it as a buffer. It must be opened both for reading and for writing by different process. The process who's reading will be blocked until the writing process doesn't write on it. When the writing process finish to write, close the file and that is the green light for the reading process to start empty the buffer. It's a FIFO, so the first line written will be the first line read. Then the writing process can open it again and they start again.

    You can create a FIFO with mkfifo. Have a look to man mkfifo.

    0 讨论(0)
提交回复
热议问题