7

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.

Nathan S.
  • 5,244
  • 3
  • 45
  • 55
RATHI
  • 5,129
  • 8
  • 39
  • 48
  • Since nobody noted this before, it should be pointed out that if you allocate a 2-byte array, a[2] won't be part of that array, only a[0] and a[1] will be. – Nathan S. Jan 01 '15 at 06:58
  • Will malloc give segmentation each time.? as malloc will allocate the space from heap and then heap size is normally very big so why we are not able to access the a[2]? it should give some random value. segmentation means that we are not able to access that. correct me if i am wrong – RATHI Jan 05 '15 at 16:12
  • It really depends on your machine and compiler. But, the bottom line is that if you haven't allocated the memory for your program to use, you shouldn't access or change it. – Nathan S. Jan 06 '15 at 00:10

4 Answers4

3
  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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • so as per comment#2, malloc() wont reserve memory in RAM, it just adds entry. when we access that allocated memory only RAM reserved??? – Jeyaram Jul 24 '12 at 12:17
  • @rjayavrp: When you access the memory, RAM is allocated. It's not reserved as the operating system will take it back if it has a more important use for it. – David Schwartz Jul 24 '12 at 18:36
  • malloc is implemented in libc and it calls syscall sbrk(). If sbrk requires another page to entertain the request only then OS is involved otherwise the malloc is completed in user mode ... AM I right here ? – theadnangondal Jan 03 '15 at 05:58
  • @theadnangondal I didn't think modern `malloc` implementations used `sbrk` anymore. But in any event, yes, if the implementation can satisfy the request with mappings it already has, it will complete the request in user mode. – David Schwartz Jan 05 '15 at 03:49
  • @David Schwartz, The last paragraph of your answer: "On 64-bit OSes, the OS doesn't take any of that space, so the full 4GB is available to 32-bit processes." doesn't make sense to me. If so where is the OS API then? – fante Sep 25 '15 at 14:05
  • @fante What do you mean by "OS API" exactly? – David Schwartz Sep 25 '15 at 17:30
  • @David Schwartz: 'OS API' or OS interface for User code. I guess the OS code (kernel code) must be mapped somehow (with protection enabled) in User space to be accesed!!!. If don't, how would you make a system call in your User code to create a mutex for example? – fante Oct 09 '15 at 15:52
  • @fante You push the parameters on the stack and then you trigger a software interrupt. – David Schwartz Oct 09 '15 at 16:33
  • @DavidSchwartz: Yes, but that is nothing more than an indirect call (via IVT) to a kernel routine which must be located at some 'xxxx_xxxxh' address. Right? This means that the kernel (OS) must be reachable from every User code. – fante Oct 09 '15 at 18:47
  • @fante Right, but that doesn't mean it has to occupy any of the user's space. There's no way I can explain your misconception fully in just the space of this comment, but as a simple way to see how you could be wrong, consider that the operation that invokes the kernel could also change the memory mapping. – David Schwartz Oct 09 '15 at 21:24
  • @DavidSchwartz: It could help if you provide some links to this topic. I would like to learn. – fante Oct 09 '15 at 22:38
  • @DavidSchwartz: Thinking about it: why Linux or Win 32-bit OS's don't use this trick of changing the MAS (Memory Address Space) on system calls? I always wondered why kernel code takes possesion of considerable MAS in these 2 OS's. On every system call they could just do the trick of changing the MAS context (via paging) and ¡voila! get a new 4GB of MAS!. – fante Oct 09 '15 at 22:53
  • @fante Because on a 32-bit OS, the cost of switching addresses spaces would be enormous every time information had to move between kernel and user spaces. – David Schwartz Oct 10 '15 at 05:10
3

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.

Dougvj
  • 6,409
  • 2
  • 23
  • 18
0

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

Mirko
  • 1,512
  • 1
  • 12
  • 19
0

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.

SJuan76
  • 24,532
  • 6
  • 47
  • 87