<p>I have process A and process B. Process A opens a file, calls mmap and write to it, process B do the same but reads the same mapped region when process A has finished writing.</p>
<p>Using mmap, process B is suppossed to read the file from memory instead of disk assuming process A has not called munmap.</p>
<p>If I would like to deploy process A and process B to diferent containers in the same pod in Kubernetes, is memory mapped IO supposed to work the same way as the initial example? Should container B (process B) read the file from memory as in my regular Linux desktop?</p>
<p>Let's assume both containers are in the same pod and are reading/writing the file from the same persistent volume. Do I need to consider a specific type of volume to achieve mmap IO?</p>
<p>In case you are courious I am using Apache Arrow and pyarrow to read and write those files and achieve zero-copy reads.</p>

<p>A Kubernetes pod is a group of containers that are deployed together on the same host. (<a href="https://coreos.com/kubernetes/docs/latest/pods.html#:%7E:text=A%20Kubernetes%20pod%20is%20a,and%20accurately%20understand%20the%20concept." rel="noreferrer">reference</a>). So this question is really about what happens for multiple containers running on the same host.</p>
<p>Containers are isolated on a host using a number of different technologies.  There are two that <em>might</em> be relevant here.  <strong>Neither</strong> prevent two processes from different containers sharing the same memory when they mmap a file.</p>
<p>The two things to consider are how the file systems are isolated and how memory is ring fenced (limited).</p>
<h1>How the file systems are isolated</h1>
<p>The trick used is to create a <a href="https://man7.org/linux/man-pages/man7/mount_namespaces.7.html" rel="noreferrer">mount namespace</a> so that any new mount points are not seen by other processes.  Then file systems are mounted into a directory tree and finally the process calls <a href="https://en.wikipedia.org/wiki/Chroot" rel="noreferrer">chroot</a> to set <code>/</code> as the root of that directory tree.</p>
<p>No part of this affects the way processes mmap files.  This is just a clever trick on how file names / file paths work for the two different processes.</p>
<p>Even if, as part of that setup, the same file system was mounted from scratch by the two different processes the result would be the same as a <a href="https://unix.stackexchange.com/q/198590/20140">bind mount</a>.  That means the same file system exists under two paths but it is *<em>the same</em> file system, not a copy.</p>
<p>Any attempt to mmap files in this situation would be identical to two processes in the same namespace.</p>
<h1>How are memory limits applied?</h1>
<p>This is done through <a href="https://en.wikipedia.org/wiki/Cgroups" rel="noreferrer">cgroups</a>.  cgroups don't really isolate anything, they just put limits on what a single process can do.</p>
<p>But there is a natuarl question to ask, if two processes have different memory limits through cgroups can they share the same shared memory?  <a href="https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt" rel="noreferrer">Yes they can!</a></p>
<blockquote>
<p>Note: file and shmem may be shared among other cgroups. In that case,
mapped_file is accounted only when the memory cgroup is owner of page
cache.</p>
</blockquote>
<p>The reference is a little obscure, but describes how memory limits are applied to such situations.</p>
<h1>Conclusion</h1>
<p>Two processes both memory mapping the same file from the same file system as different containers on the same host will behave almost exactly the same as if the two processes were in the same container.</p>
