5

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

这是否意味着正在运行的进程总是打开要读取的文件?但是,既然文件流被自己的进程锁定,那么如何写入文件呢?

4

1 回答 1

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. 看看man mkfifo

于 2012-06-16T10:01:22.243 回答