Can malloc return the same address in two different processes?

Suppose I have two processes a and b on Linux. and in both processes I use malloc() to allocate memory,

Is there any chance that malloc() return the same starting address in two processes? If not, then who will take care of this. If so, then both processes can access the same data at this address.

+4
source share
3 answers

Is there any chance that malloc () will return the same starting address in two processes.

Yes, but that’s not a problem.

What you do not understand is that operating systems first process your physical space for you - programs, etc. see only virtual addresses. There is only one virtual address space, however, the operating system (now let it be 32-bit) shares this. On Windows, the upper half (0xA0000000 +) refers to kernel processes and the lower half to user processes. This is called a 2 GB / 2 GB split. On Linux, the partition is 3 GB / 1 GB - see this article :

Kernel memory is first defined with PAGE_OFFSET, which on x86 is 0XC0000000 or 3 gigabytes. (The 3gig / 1gig separation is defined here.) Each virtual address above PAGE_OFFSET is the kernel, any address below PAGE_OFFSET is the user's address.

Now, when the process is switched (unlike the context switch), all pages belonging to the current process are not displayed from virtual memory (not necessarily swapping them), and all pages belonging to the future -run process are copied to (disclaimer : this may not be entirely true, you can mark pages dirty, etc. and copy to access, theoretically).

The reason for the gap is that, for performance reasons, the upper half of the virtual memory space may appear in the kernel of the operating system.

So, although malloc can return the same value in two given processes, it does not matter, because:

  • physically, they do not match the address.
  • processes do not exchange virtual memory anywhere.

For 64-bit systems, since we currently use only 48 of these bits, there is a gap between the bottom of the user mode and the kernel mode, which is not addressed (for now).

+11
source

Yes, malloc() can return the same pointer value in separate processes if the processes run in separate address spaces, which is achieved through virtual memory. But in this case, they will not have access to the same physical location of the memory, and the data at the address does not have to be the same.

+2
source

A process is a collection of threads plus address space. This address space is called virtual, because each byte of it is not necessarily supported by physical memory. The virtual address space segments will ultimately be supported by physical memory if the application in this process will use this memory efficiently.

So, malloc() can return an identical address for two processes, but this is not a problem, since these malloced memories will be supported by different segments of physical memory.

In addition, the implementation of malloc() is moslty not reentrant, so calling malloc() on different threads sharing the same address space will hopefully not return the same virtual address.

+1
source

All Articles