如何做一个程序在内存中运行,内存的方式是通过操作系统来处理 [英] how does a program runs in memory and the way memory is handled by Operating system

查看:294
本文介绍了如何做一个程序在内存中运行,内存的方式是通过操作系统来处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不内存管理明确当一个进程在执行
在运行时

下面是一个图表

我不与图像在以下明确的:


  • 1)什么是这一形象是指?
  • 堆栈
  • 2)什么是它所指的文件映射内存映射段?

  • 3)什么是堆有一个过程做。在堆仅仅在一个进程处理或是堆的东西由操作系统内核维护,然后存储空间由malloc(使用堆)永远的用户空间应用程序调用这样当分配?

文中提到
<一href=\"http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/\">http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/


  

虚拟地址空间,这在32位模式下始终是一个4GB的块
  内存地址。这些虚拟地址映射到物理
  由页表内存,



  • 4),这是否意味着在同一时间只有一个程序在内存占用的RAM整个4 GB运行?

在同一篇文章还提到


  

Linux的加入随机化栈,内存映射段和堆
  补偿他们的起始地址。不幸的是,32位地址
  空间pretty紧,留下一点空间随机化和
  阻碍了其有效性。



  • 5),它是指一个进程内随机堆栈抑或是指一些东西,计算所有进程的空间后留下的?


解决方案

  

1)什么是这一形象是指堆栈?


借助是分配局部变量和函数调用框架(其中包括像功能参数,在返回后,函数调用等)。


  

2)什么是它所指的文件映射内存映射段?


持有链接库内存映射段。这也就是 MMAP 呼叫分配。在一般情况下,一个存储器映射文件是简单地通过一个文件备份的存储区。


  

3)什么堆有一个过程做。在堆仅仅在一个进程处理或是堆的东西通过操作系统内核,然后存储器空间被由malloc分配(使用堆)当过一个用户空间应用程序调用该保持


是特定的过程,并且由程序本身管理的,但它必须请求从OS存储器开始与(并视需要)。你是正确的,这通常是其中的malloc 呼叫分配。然而,大多数的malloc 实现使用 MMAP 的请求的内存块,所以之间确实少了区别堆和内存映射段。说真的,堆的可能的被认为是内存映射段的一部分。


  

4)这是否意味着,在同一时间只有一个程序在内存占用整个4 GB的RAM?运行


没有,这意味着中的寻址的内存可供程序限制为4 GB的RAM,是什么在任何特定时间实际上是包含在存储器依赖于OS如何分配的物理内存量,这超出了这个问题的范围。


  

5),它是指一个进程内随机堆栈抑或是指一些东西,计算所有进程的空间后留下的?


我从来没有见过任何建议4GB空间阻碍由操作系统使用的内存分配策略的有效性。此外,由于@Jason笔记,堆的位置,内存映射段和堆是随机的,以prevent predictable安全漏洞,或者至少给他们带来极大难度比如果每个处理操作系统管理有在完全相同的虚拟存储位置中可执行的每一个部。具体而言,操作系统被随机化的虚拟地址的堆栈,存储器映射的区域,和堆。关于这一点,一切的过程中看到的是一个虚拟地址,然后映射到物理地址在内存中,具体情况取决于特定页面的位置。有关虚拟和物理地址之间的映射的更多信息,可以发现 rel=\"nofollow\">。

在此分页维基百科文章是学习操作系统如何管理内存之间的一个很好的起点进程,并且是在读开回答问题4和5总之良好的资源,存储器中的页面的进程分配,并且这些页无论是在主存储器存在,或已被换出到磁盘。当一个存储器地址是通过一个过程的要求,这将在页面从磁盘到主存储器,如果需要移动,替换另一页。有使用各种页面替换策略,我是指你的文章,详细了解各的优点和缺点。

I am not clear with memory management when a process is in execution during run time

Here is a diagram

I am not clear with the following in the image:

  • 1) What is the stack which this image is referring to?
  • 2) What is memory mapping segment which is referring to file mappings?
  • 3) What does the heap have to do with a process. Is the heap only handled in a process or is the heap something maintained by the operating system kernel and then memory space is allocated by malloc (using the heap) when ever a user space application invokes this?

The article mentions http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/

virtual address space, which in 32-bit mode is always a 4GB block of memory addresses. These virtual addresses are mapped to physical memory by page tables,

  • 4) Does this mean that at a time only one program runs in memory occupying entire 4 GB of RAM?

The same article also mentions

Linux randomizes the stack, memory mapping segment, and heap by adding offsets to their starting addresses. Unfortunately the 32-bit address space is pretty tight, leaving little room for randomization and hampering its effectiveness.

  • 5) Is it referring to randomizing the stack within a process or is it referring to something which is left after counting the space of all the processes?

解决方案

1) What is the stack which this image is referring to?

The stack is for allocating local variables and function call frames (which include things like function parameters, where to return after the function has called, etc.).

2) What is memory mapping segment which is referring to file mappings?

Memory mapping segment holds linked libraries. It also is where mmap calls are allocated. In general, a memory mapped file is simply a region of memory backed by a file.

3) What does the heap have to do with a process. Is the heap only handled in a process or is the heap something maintained by the operating system kernel and then memory space is allocated by malloc (using the heap) when ever a user space application invokes this?

The heap is process specific, and is managed by the process itself, however it must request memory from the OS to begin with (and as needed). You are correct, this is typically where malloc calls are allocated. However, most malloc implementations make use of mmap to request chunks of memory, so there is really less of a distinction between heap and the memory mapping segment. Really, the heap could be considered part of the memory mapped segment.

4) Does this mean that at a time only one program runs in memory occupying entire 4 GB of RAM?

No, that means the amount of addressable memory available to the program is limited to 4 GB of RAM, what is actually contained in memory at any given time is dependent on how the OS allocated physical memory, and is beyond the scope of this question.

5) Is it referring to randomizing the stack within a process or is it referring to something which is left after counting the space of all the processes?

I've never seen anything that suggests 4gb of space "hampers" the effectiveness of memory allocation strategies used by the OS. Additionally, as @Jason notes, the locations of the stack, memory mapped segment, and heap are randomized "to prevent predictable security exploits, or at least make them a lot harder than if every process the OS managed had each portion of the executable in the exact same virtual memory location." To be specific, the OS is randomizing the virtual addresses for the stack, memory mapped region, and heap. On that note, everything the process sees is a virtual address, which is then mapped to a physical address in memory, depending on where the specific page is located. More information about the mapping between virtual and physical addresses can be found here.

This wikipedia article on paging is a good starting point for learning how the OS manages memory between processes, and is a good resource to read up on for answering questions 4 and 5. In short, memory is allocated in pages to processes, and these pages either exist in main memory, or have been "paged out" to the disk. When a memory address is requested by a process, it will move the page from the disk to main memory, replacing another page if needed. There are various page replacement strategies that are used and I refer you to the article to learn more about the advantages and disadvantages of each.

这篇关于如何做一个程序在内存中运行,内存的方式是通过操作系统来处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆