通过文件进行进程间通讯 [英] Interprocess Communication via file

查看:111
本文介绍了通过文件进行进程间通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 this mean a running process always open the file to be read? But so, how can the file be written, since the file stream is locked by by its own process?

推荐答案

如果要使用文件与另一个进程进行通信,则应查看man fifo.

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.

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

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.

您可以使用mkfifo创建FIFO.看看man mkfifo.

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

这篇关于通过文件进行进程间通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆