简体   繁体   English

进程地址空间和虚拟内存

[英]Process address space and virtual memory

I'm aa total noob in this field, so please bear with my question and answer it :)我是这个领域的菜鸟,所以请耐心回答我的问题:)

I was reading about process address space, virtual memory and paging.我正在阅读有关进程地址空间、虚拟内存和分页的信息。 I understood the mechanism of pages being swapped in and out of RAM.我了解页面在 RAM 中换入和换出的机制。 It is documented that every process is given a virtual address space of 4GB, out of which a portion is for user space (specific to each process - 1 or 2 GB) and the rest is kernel space (which is common across all processes).Since the vritual address space for each process is stored in secondary storage, does it mean that every time I launch a process, it reserves 4 GB in my hard disk ?据记载,每个进程都有一个 4GB 的虚拟地址空间,其中一部分用于用户空间(特定于每个进程 - 1 或 2 GB),其余部分是内核空间(所有进程通用)。由于每个进程的虚拟地址空间都存储在二级存储中,是否意味着每次启动进程时,它都会在我的硬盘中保留 4 GB 空间? I don`t see my hard disk space being reduced by 4GB each time I start a process... or is it something like the virtual space is not reserved as a block but rather starts small and can grow upto 4GB.我没有看到每次启动一个进程时我的硬盘空间都减少了 4GB……或者是虚拟空间不是作为一个块保留的,而是从小开始的,可以增长到 4GB。 Please shed some light, and also do post some links on the topic if you know any.. anything that would help me understand this complex topic.请说明一些问题,如果您知道任何可以帮助我理解这个复杂主题的内容,也请发布一些有关该主题的链接。

Virtual address memory management doesn't use secondary storage.虚拟地址内存管理不使用辅助存储。 It just divides your program and main memory into some partition size.它只是将您的程序和主内存划分为某个分区大小。 Main memory partitions are called frames and program partitions are called pages.主内存分区称为帧,程序分区称为页。 Each partition in main memory and frame are of the same size.主存和帧中的每个分区大小相同。

whenever a program is used, the kernel checks if all pages of this programs can be fit into main memory.每当使用一个程序时,内核都会检查该程序的所有页面是否都可以放入主内存中。 if so, it will keep all pages in main memory.如果是这样,它会将所有页面保留在主内存中。 other wise it will keep only pages that are necessary at first.否则,它只会保留最初需要的页面。 when other pages are required later it removes one of the frames from main memory and then transfers the requested page to where the frame was removed.当稍后需要其他页面时,它会从主内存中删除其中一个帧,然后将请求的页面传输到删除该帧的位置。

For mapping of address from pages to frames the kernel uses a page map table, which tells exactly in which frame in main memory this particular page is located.为了将地址从页映射到帧,内核使用页映射表,该表准确地说明此特定页位于主存储器中的哪个帧。

Check the below references: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/virtual.html https://www.bottomupcs.com/virtual_addresses.xhtml检查以下参考资料: https : //www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/virtual.html https://www.bottomupcs.com/virtual_addresses.xhtml

It is offtopic here and one could elaborate hundreds of pages text about (see the cs books on this topic).这里是题外话,可以详细说明数百页的文字(请参阅有关此主题的 cs 书籍)。

But in short you are wrong: virtual memory is NOT stored in secondary storage.但简而言之,您错了:虚拟内存存储在二级存储中。 It can be there, but usually it is not.它可以在那里,但通常不是。 The OS is handling it, depending on its information it keeps the data in normal memory (eg when then process is active), or it is on hdd (eg when the memory is short and the process is inactive).操作系统正在处理它,根据它的信息,它将数据保存在正常内存中(例如,当进程处于活动状态时),或者它在硬盘上(例如,当内存不足且进程处于非活动状态时)。

The virtualisation means, that for the process it is transparent, it does not know where the data is stored, it is always accessed via the same pointer.虚拟化意味着,对于进程它是透明的,它不知道数据存储在哪里,它总是通过相同的指针访问。 The system can change the underlying storage depending on the circumstances (it is then swapping data in or out).系统可以根据情况更改底层存储(然后将数据换入或换出)。

Virtual address space is not "stored" anywhere.虚拟地址空间不会“存储”在任何地方。 Only pages of memory allocated for the process may be stored in memory/on disk.只有为进程分配的内存页可以存储在内存/磁盘上。

The allocation of memory depends of operating system.内存的分配取决于操作系统。 Ie in Windows Allocation and managment can be done by program using corresponding memory functions , like VirtualAlloc to map memory to address space and VirtualProtect function to specify what attributes given memory block allocated in your program's address space should have.即在 Windows 中分配和管理可以由程序使用相应的内存函数来完成,例如 VirtualAlloc 将内存映射到地址空间, VirtualProtect函数指定给定在程序地址空间中分配的内存块应该具有的属性。

Well, that's a good question for a total noob in this field.What actually happens is that the process that is executing is made to think that it has all the memory,while it only has a few memory that will expand as it grows.This memory is called virtual memory.嗯,对于这个领域的新手来说,这是一个很好的问题。实际发生的是,正在执行的进程被认为拥有所有内存,而它只有少数内存会随着它的增长而扩展。这个内存称为虚拟内存。

I hope this answer is helpful.我希望这个答案有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM