Modern OS's, including Linux, have "anonymous" and non anonymous memory. Non anonymous memory is "mapped" from files and brought into ram by demand paging. Anonymous memory is created in ram and might be paged out to the swap space.
Non anonymous memory is typically shareable, meaning any pages brought into memory by demand paging the file can be shared among any processes mapping the same file.
In terms of the original question, most code and constants will be mapped and shareable. Most writeable data in most processes will by anonymous.
Quote:
Originally Posted by Lsatenstein
If program X was comprised of 100k data, 100k constant, and 300k code segments, what would be in memory if, while program X was executing, a second program X was loaded by another user(process id)?
|
In 4Kb chunks, whichever parts of that 100k of constants have been referenced by one of the two processes would be in ram once (shared if both processes have accessed it). The same is mostly true of the 300k of code. But some 4kb pages of some code need load time fixups, which I think aren't shared even when identical, and if the code is in .so files the fixups typically aren't identical across processes sharing the code.
The 100k of data is created as "demand zero" (meaning it doesn't really exist in ram) or in some cases "copy on write" mappings when the program starts. Each 4Kb page of "demand zero" or "copy on write" memory that is written to is allocated as a real page of ram the first time it is written to.
Quote:
Originally Posted by rtmistler
My impression is that any given process owns all its memory and does not share that memory for re-use or duplication purposes such as you describe where the memory model re-uses constants as a method to efficiently manage memory.
|
Each process has its own
mapping, which acts like it (the mapping tables themselves) are not shared. But even parts of the mapping tables can be shared when the OS can make shared mapping tables act as if they are not shared. Similarly, "demand zero" and "copy on write" mappings are shared in a way that lets the OS make it appear to the process that they are not shared (including converting to actually not shared when necessary to preserve the illusion that it never had been shared.
But there are also parts of the address space that are explicitly shared across processes.
Quote:
my opinion is that the Operating System does not do similar things to what you're speaking about.
|
Apologies if I'm landing on the wrong side of the tradeoff between avoiding misinforming the OP and avoiding being nasty to others who answer, but ...
These are much more matters of fact than opinion. I might have some of the subtle details incorrect in what I said earlier, but you have the basic facts much more fundamentally wrong. What Linux does is similar to (though more complicated than) what the OP described.
In modern systems, the amount of anonymous data has grown more than the amount of code and constants, and typical amounts of physical ram has grown even more than that. So all that complicated code and constant sharing is becoming a minor efficiency feature, rather than a fundamental part of what makes a Linux system usable. But it is all still in there.