7

This question has been confusing me for days:

Assume that I have two processes (p_write and p_read) which run on the same machine.

  • Process p_write is for writing/updating the mmap file.

  • Process p_read is for consuming the mmap file, or in other words, reading from the mmap file.

My assumption here is that p_write needs to first allocate a memory space (off-heap) for the mmap file (the space is mapped to the file automatically using the Java MappedByteBuffer API).

My question here is how does p_read read from the mmap file? My assumption now is that p_read also needs to allocate another same size off-heap space for the mmap file to be mapped to, but this seems incorrect as the amount of memory will need to be doubled in this scenario.

If p_read does not need to allocate a separate memory space for the mmap file to be mapped to, how does p_read know the right memory address that the file was mapped to by p_write?

UPDATE 1

I found a better question to ask, or you can consider this as a follow-up question: If FileChannel.map() is called twice, will the same file be mapped twice into two different memory spaces?

// Scenario A: In single process

try (FileChannel fc = FileChannel.open(filePath, openOptions)) {
   // First call
   fc.map(MapMode.READ_ONLY, 0, SIZE_CONSTANT);
   // Second call
   fc.map(MapMode.READ_ONLY, 0, SIZE_CONSTANT);
}

And

// Scenario B: In two processes

// in first process
try (FileChannel fc = FileChannel.open(filePath, openOptions)) {
   // First call in first process
   fc.map(MapMode.READ_WRITE, 0, SIZE_CONSTANT);
}
...
// in second process
try (FileChannel fc = FileChannel.open(filePath, openOptions)) {
   // Second call in second process
   fc.map(MapMode.READ_ONLY, 0, SIZE_CONSTANT);
}

Maybe it does not really matter for the two scenarios, if they are all mapped into one same memory space?

Lubor
  • 989
  • 3
  • 10
  • 33

0 Answers0