Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual Memory?

I am very much confused with these questions.

  1. On a 32 bit processor, every process has 4 GB virtual memory. But, if evey process has 4gb space than it will be every huge amount if 100 process is running - this is greater than swap area. Can someone please explain this; I am very confused.

  2. How does the operating system allocate the memory to a process? Suppose a process has a = malloc(2). Who will allocate this memory to the process? Will the OS give these 2 Bytes memory to the process.
    (We access the a[2] it generate the segmentation error).

  3. Where do the different parts of the process remains ( Code , Data , Stack, Heap ) in Main Memory or in secondary memory.

Please give me some good link so that I can also understand the virtual memory and its whole mechanism as the links I've found are not fully explaining the virtual memory.

like image 240
RATHI Avatar asked Jul 24 '12 07:07

RATHI


2 Answers

  1. Who cares whether virtual memory is greater or less than swap area? What difference does that make? (If you, say, map a 2GB file read-only, that uses 2GB of virtual memory, but no swap space and only trivial amounts of physical memory is needed.)

  2. The OS simply extends the process' virtual memory space. It's just changing an accounting entry. Physical memory is not needed until an attempt is made to modify the contents of the address space. (Actually, the process will likely do this itself, only asking the OS to extend its virtual memory space when it needs larger chunks.)

  3. They remain in physical memory (assuming they faulted in to begin with) until the operating system elects to move them elsewhere or discard them. If they are moved elsewhere or discarded, they are paged back in or recreated when they are accessed through page faults. (The OS manages physical memory as a precious resource, granting it as it thinks best.)

By the way, on most 32-bit OSes, the OS itself takes 1GB or 2GB of that virtual memory space, leaving only 2GB or 3GB truly usable by the process. On 64-bit OSes, the OS doesn't take any of that space, so the full 4GB is available to 32-bit processes.

like image 131
David Schwartz Avatar answered Nov 17 '22 07:11

David Schwartz


1) Each process has 4gb of virtual memory space, but it need not be allocated all at once. The operating system specifies to the MMU what parts of physical memory are mapped to its virtual space, and what parts are not mapped at all. Accesses to the parts that are not mapped will cause the processor to fault and the operating system usually generates a segfault. There is also a marker for "not present" which tells the processor that the area of memory is not in physical memory space but is in the swap space, so the processor faults and the operating system swaps the page back into physical memory, then resumes the process where it left off. To describe a processes page table, you only need a few bytes of memory, so 100 processes would not use that much memory until they actually request it.

2) There are many memory allocation algorithms. Usually the operating system only allocates large blocks of memory at a time, and so calls to malloc() only sometimes result in a call to the operating system, most of the time however it is the C standard library implementation details that handle the micromanagement. There is no guarantee that an access out of bounds of an array will produce a seg fault, as it could be part of a different array that was malloc'ed earlier, or part of free space that the standard library is keeping track of for future allocations and therefore will not segfault. There are debugging tools like valgrind that will detect such errors, however.

3) The details as to where each segment is located is operating system dependent, but for code that is general and portable, there is no need to know.

For more information on all of these topics, consult the osdev wiki, specifically the part on paging and memory allocation.

like image 22
Dougvj Avatar answered Nov 17 '22 09:11

Dougvj