Window's Virtual Memory verses Linux's Real Memory

Any other programming language that is not covered in the Web Scripting category.

Window's Virtual Memory verses Linux's Real Memory

Postby bockman on Mon Dec 05, 2005 10:27 pm

A friend and I were talking about how Linux (and really the rest of the computing world) differ in the way they handle memory. He said Windows uses virtual memory, meaning every program is given (seemingly, at least to the programs point of view) the same memory address space. The following program will always produce the same output on Windows, but only sometimes the same on Linux:
Code: Select all
int main()
{
    int a;
    printf("0x%.8X", &a);
    return 0;
}

Now, I know Linux uses some form of virtual memory (hence swap), but I realize the difference in the two operating systems when running the same program. What's the real story?
bockman
Newbie
Newbie
 
Posts: 17
Joined: Mon Nov 28, 2005 8:42 pm

Postby bockman on Mon Dec 05, 2005 10:28 pm

edited.
bockman
Newbie
Newbie
 
Posts: 17
Joined: Mon Nov 28, 2005 8:42 pm

Postby mm_freak on Fri Dec 16, 2005 7:36 am

Talking about Intel architectures, Linux, Windows and most other operating systems running in protected mode use virtual memory. This doesn't have to do with swap, though swapping is part of it.

Virtual memory means that programs cannot reference memory in an absolute way. They have no knowledge of the underlying memory structure, so in fact, you could run them from a tape device as the random access memory (it would be painfully slow and loud, but it would work). Instead of an absolute partition of the real physical memory they get what is called an own address space. In other words: they intentionally have their own, virtual memory.

The advantage is that the operating system can move RAM portions freely in memory and also save (swap) them elsewhere for later retrieval, while not changing the virtual address of those memory portions. It also prevents inter-process memory fiddling by design, because other processes are beyond the own address space.

Both in Windows and Linux there is something called an offset from the NULL address. So most Windows programs' address space will actually start at something like 0x400000. This causes programs dereferencing the NULL address to cause a memory protection fault. In Linux there is no default offset, so the operating system chooses one, which never is NULL, so NULL dereferences cause programs to crash by protection fault (called segmentation fault in Linux), too. All this is intentional, because NULL pointer dereferences are always indicating program bugs.
I'm x^10 + x^8 + x^5 + x^4 + x^3 + 1, so don't mess with me!
mm_freak
Moderator
Moderator
 
Posts: 635
Joined: Sun Sep 11, 2005 12:29 pm
Location: Baden-Wuerttemberg (Germany)


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron