views:

306

answers:

4

Hi,

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

For Eg: 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 ?.

Answer will be highly appreciated.

Thanks in advance.

+4  A: 

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.

unwind
Certainly Windows is clever enough. DLLs can only be shared among multiple processes if the DLL was able to be loaded at its desired *base address*, though. If it had to be loaded someplace else, the it will not be reused by other processes. That's why it's a good idea to *rebase* your DLLs so that they all have unique base addresses configured. Lots of information available by searching for *dll rebase*.
Rob Kennedy
Ah, I see you meant whether it's clever enough to do with *EXE* files. My comment above was written thinking you meant whether *Windows* was clever enough, as opposed to, say, Linux or Solaris.
Rob Kennedy
+1  A: 

The code segment is (supposed to be) static (does not change) so there is no reason not to use it for several instances.

Gamecat
+2  A: 

Code is typically read-only, so it would be wasteful to make multiple copies of it.

Also, Windows (at least, I can't speak for other OS's at this level) uses the paging infrastructure to page code in and out direct from the executable file, as if it were a paging file. Since you are dealing with the same executable, it is paging from the same location to the same location.

Self-modifying code is effectively no longer supported by modern operating systems. Generating new code is possible (by setting the correct flags when allocating memory) but this is separate from the original code segment.

Zooba
It's quite a feat to go from "no longer supported" to "possible" in adjacent sentences. Self-modifying code is absolutely supported by modern operating systems.
Rob Kennedy
Modifying the code segment is made so difficult by OS and hardware protection that it is no longer worthwhile. Creating code on the fly into a newly allocated block of memory (marked 'executable') is a completely different idea.
Zooba
A: 

EDIT: oh, maybe I misinterpreted the question. I thought he was asking why both processes have the same value in the code segment register.

Just to start at a basic level, Segmentation is just a way to implement memory isolation and partitioning. Paging is another way to achieve this. For the most part, anything you can achieve via segmentation, you can be achieve via paging. As such, most modern operating systems on the x86 forego using segmentation at all, instead relying completely on paging facilities.

Because of this, all processes will usually be running under the trivial segment of (Base = 0, Limit = 4GB, Privilege level = 3), which means the code/data segment registers play no real part in determining the physical address, and are just used to set the privilege level of the process. All processes will usually be run at the same privilege, so they should all have the same value in the segment register.

Falaina