RATHI
RATHI

Reputation: 5299

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.

Upvotes: 7

Views: 2581

Answers (4)

Dougvj
Dougvj

Reputation: 6585

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.

Upvotes: 3

SJuan76
SJuan76

Reputation: 24895

A grave missunderstanding from you is differencing between virtual memory and memory. From a process POV, there is no difference, the process only accesses to memory and it is the OS which is in charge of swapping portions of data between physical (RAM) and virtual memory.

1) That the space of address of a process can reach up to 4GB does not mean that each process has 4GB allocated. The OS assigns memory to them as needed.

2) The OS gives memory (*1) in blocks. When you do a malloc, the malloc function that manages the program memory internally get the needed space inside the process memory and return the pointer (maybe in the process they request additional memory from the OS, but it is not required).

3) As started at the beginning, this is an OS issue. Each OS can decide which parts they do virtualize and which parts they don't, there are complex strategies involved to reduce the number of swaps.

*1 Note that I talk about memory, not VM. The application does not know which parts of its memory are virtual or physical, it is transparent to them.

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182883

  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.

Upvotes: 3

Mirko
Mirko

Reputation: 1552

First: 32bit means 32bit. there no more bits to address more memory space. Multiprocessor-Systems are not new invention. With 32 bit you can only address 4gigs space. there some workarounds like PAE http://en.wikipedia.org/wiki/Physical_Address_Extension.

Second and third.. I'm not really sure how it works today. But take a look at http://en.wikipedia.org/wiki/Virtual_memory

Upvotes: 0

Related Questions