Virtual memory?

I am very confused by these questions.

  • On a 32-bit processor, each process has 4 GB of virtual memory. But if the evey process has 4gb space, then it will be a huge amount; if 100 processes are running, this is more than the swap area. Can someone explain this; I am very confused.

  • How does the operating system allocate memory for a process? Suppose a process has a = malloc(2) . Who will allocate this memory for the process? Will the OS provide these 2 bytes of memory for the process.
    (We gain access to a [2], which generates a segmentation error).

  • Where are the different parts of the process (code, data, stack, heap) in main memory or in secondary memory.

Please give me a good link so that I can also understand virtual memory and its entire mechanism, since the links found do not fully explain virtual memory.

+6
source share
4 answers
  • Who cares if there is more or less virtual memory than the swap area? What's the difference? (If you, say, map a 2 GB read-only file that uses 2 GB of virtual memory, but there is no swap space and only a trivial amount of physical memory is required.)

  • The OS simply extends the virtual memory space of the process. It just changes the account. Physical memory is not needed until an attempt is made to change the contents of the address space. (In fact, the process is likely to do it itself, only asking the OS to expand the virtual memory space when it needs large chunks.)

  • They remain in physical memory (provided that they take effect from the very beginning) until the operating system decides to move them to another location or drops them. If they are moved elsewhere or discarded, they are returned or recreated when they are accessed through page errors. (The OS manages physical memory as a valuable resource, providing it as it thinks best.)

By the way, in most 32-bit operating systems, the OS itself takes up 1 GB or 2 GB of virtual memory, leaving only 2 GB or 3 GB that can actually be used in this process. On 64-bit operating systems, the OS does not occupy any of these spaces, so the full 4 GB is available for 32-bit processes.
+3
source

1) Each process has 4 GB of virtual memory space, but it does not need to be allocated immediately. The operating system tells the MMU which parts of the physical memory are mapped to virtual space and which parts are not displayed at all. Access to components that are not displayed will cause a processor error, and the operating system usually generates segfault. There is also a no token that tells the processor that the memory area is not in physical memory but is in the swap area, so the processor is faulty and the operating system changes the page back to physical memory, then resumes the process when it stops. To describe a table of process pages, you only need a few bytes of memory, so 100 processes will not use this large memory until they request it.

2) There are many memory allocation algorithms. Typically, the operating system allocates only large blocks of memory at a time, and therefore calls to malloc () sometimes cause the operating system to be called, but most of the time these are implementation details of the standard C library that handle micromanagement. There is no guarantee that access outside the array will cause seg to fail, as it may be part of another array that was previously malloc'ed, or part of the free space that the standard library monitors for the future and therefore will not be segfault. However, there are debugging tools like valgrind that will detect such errors.

3) Information about where each segment is located depends on the operating system, but there is no need to know for general and portable code.

For more information on all of these topics, refer to the osdev wiki, in particular, the parts on paging and memory allocation .

+2
source

First: 32 bits means 32 bits. no more bits to access more memory. Multiprocessor systems are not a new invention. With 32 bits you can only use 4gigs space. there are some workarounds such as PAE http://en.wikipedia.org/wiki/Physical_Address_Extension .

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

0
source

A serious misunderstanding from you is the difference between virtual memory and memory. There is no difference from the POV process, the process only accesses the memory, and it is the OS that is responsible for the exchange of pieces of data between physical (RAM) and virtual memory.

1) that the process address space can reach 4 GB, does not mean that each process has 4 GB. The OS assigns them memory as needed.

2) The OS gives memory (* 1) in blocks. When you execute malloc, the malloc function, which manages the program memory, internally obtains the necessary space inside the process memory and returns a pointer (perhaps in the process they request additional memory from the OS, but this is not required).

3) As it began at the beginning, this is an OS problem. Each OS can decide which parts they virtualize and which parts they don’t have, there are complex strategies to reduce the number of swaps.

* 1 Note that I'm talking about memory, not about VM. The application does not know which parts of its memory are virtual or physical; they are transparent to them.

0
source

Source: https://habr.com/ru/post/921175/


All Articles