Search code examples
memory-managementprocessoperating-systemexecutable

Why code segment is common for different instances of same program


I wanted to know why code segment is common for different instances of same program.

For example: consider program P1.exe running, if another copy of P1.exe is running, code segment will be common for both running instances. Why is it so?


Solution

  • If the code segment in question is loaded from a DLL, it might be the operating system being clever and re-using the already loaded library. This is one of the core points of using dynamically loaded library code, it allows the code to be shared across multiple processes.

    Not sure if Windows is clever enough to do this with the code sections of regular EXE files, but it would make sense if possible.

    It could also be virtual memory fooling you; two processes can look like they have the same thing on the same address, but that address is virtual, so they really are just showing mappings of physical memory.