通过写入文件进行IPC?
我有一个关于操作系统中进程间通信的问题。
两个进程可以通过打开同一个文件(该文件是在两个进程之前创建的,因此两个进程都有文件处理程序)然后通过写入该文件进行通信来相互通信吗?
如果是,这个方法属于什么?我听说 IPC 的两种主要方式是共享内存和消息传递。该方法属于以下哪一项? 原因是,我不确定它是否属于共享内存,因为该文件没有映射到任何这些进程的地址空间。而且,根据我的理解,在共享内存中,共享内存区域是两个进程的地址空间的一部分。
假设进程以某种预先商定的协议/格式写入文件,因此两个进程都可以毫无问题地知道另一个进程在何处写入以及何时写入等。这种假设只是为了理解。但在现实世界中,这可能过于严格而无法成立等。
如果不是,这种情况有什么问题?如果两个不同的进程打开同一个文件,那么第一个进程所做的更改不会刷新到持久存储中供其他进程查看,直到该进程终止吗?或者其他什么?
来自 Windows 和 Linux 的任何现实世界示例也应该有用。
谢谢,
I have a question about Inter-process-communication in operating systems.
Can 2 processes communicate with each other by both processes opening the same file (which say was created before both processes, so both processes have the file handler) and then communicating by writing into this file?
If yes, what does this method come under? I have heard that 2 major ways of IPC is by shared-memory and message-passing. Which one of these, this method comes under?
The reason, I am not sure if it comes under shared-memory is that, because this file is not mapped to address space of any of these processes. And, from my understanding, in shared-memory, the shared-memory-region is part of address space of both the processes.
Assume that processes write into the file in some pre-agreed protocol/format so both have no problem in knowing where the other process writes and when etc. This assumption is to merely understand. In real world though, this may be too stringent to hold true etc.
If no, what is wrong with this scenario? Is it that if 2 different processes open the same file, then the changes made by 1st process are not flushed into persistent storage for others to view until the process terminates? or something else?
Any real world example from Windows and Linux should also be useful.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用文件是一种共享内存。不是在 RAM 中分配公共内存缓冲区,而是使用公共文件。
为了成功管理通信,需要针对文件中不同范围的某种锁定机制。这可以是文件系统提供的范围锁定(至少在 Windows 上可用)或全局操作系统互斥锁。
使用磁盘存储进行进程间通信的一种现实场景是集群中使用的仲裁磁盘。它是一种公共磁盘资源,所有群集节点都可以通过 SAN 进行访问,用于存储群集的配置。
Using a file is a kind of shared memory. Instead of allocating a common memory buffer in RAM, a common file is used.
To successfully manage the communication some kind of locking mechanism for different ranges in the file is needed. This could either be locking of ranges provided by the file system (available at least on Windows) or global operating system mutexes.
One real-world scenario where disk storage is used for inter-process-communication is the quorom disk used in clusters. It is a common disk resource, accessible over a SAN by all cluster nodes, that stores the cluster's configuration.
posix 系统调用 mmap 将文件映射到虚拟内存。如果映射在两个进程之间共享,则在一个进程中写入该区域将影响其他进程。现在回答你的问题,是的,从底层文件读取或写入的进程并不总是看到与映射它的进程相同的数据,因为文件的段被复制到 RAM 中并定期刷新到磁盘。尽管我相信您可以通过 msync 系统调用强制同步。请阅读 mmap()。它还有许多其他内存共享选项。
The posix system call mmap does mappings of files to virtual memory. If the mapping is shared between two processes, writes to that area in one process will affect other processes. Now coming to you question, yes a process reading from or writing to the underlying file will not always see the same data that the process that has mapped it, since the segment of the file is copied into RAM and periodically flushed to disk. Although I believe you can force synchronization with the msync system call. Do read up on mmap(). It has a host of other memory sharing options.