通过文件进行进程间通信

时间:2012-06-16 09:54:52

标签: linux process ipc

当我在Linux中的某些任意位置(即echo > /tmp/file)回显文件时,某些正在运行的进程会响应。这个IPC是通过文件管道吗?

这是否意味着正在运行的进程始终打开要读取的文件?但是,如何编写文件,因为文件流是由自己的进程锁定的?

1 个答案:

答案 0 :(得分:7)

如果您想使用文件与其他流程进行通信,请查看man fifo

我将在这里报告第一行:

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.

我认为这就是你所需要的。

只需将其视为缓冲区。它必须通过不同的过程打开读取和写入。正在阅读的过程将被阻止,直到写入过程没有写入。当写入过程完成写入时,关闭文件,这是读取过程开始清空缓冲区的绿灯。它是一个FIFO,因此写入的第一行将是第一行读取。然后写作过程可以再次打开它们,然后重新开始。

您可以使用mkfifo创建FIFO。请查看man mkfifo

相关问题