13

実行時にプロセスが実行されているときのメモリ管理がよくわかりません

ここに図がありますここに画像の説明を入力

画像の次の点が明確ではありません。

  • 1) この画像が参照しているスタックは何ですか?
  • 2) ファイル マッピングを参照しているメモリ マッピング セグメントとは何ですか?
  • 3) ヒープはプロセスと何の関係がありますか。ヒープはプロセスでのみ処理されますか、それともヒープはオペレーティング システム カーネルによって維持されるものであり、ユーザー空間アプリケーションがこれを呼び出すたびに、(ヒープを使用して) malloc によってメモリ空間が割り当てられますか?

この記事では、 http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/について言及しています。

仮想アドレス空間。32 ビット モードでは常にメモリ アドレスの 4GB ブロックです。これらの仮想アドレスは、ページ テーブルによって物理メモリにマップされます。

  • 4) これは、一度に 1 つのプログラムだけが 4 GB の RAM 全体を占有するメモリで実行されることを意味しますか?

同記事にも言及

Linux は、開始アドレスにオフセットを追加することにより、スタック、メモリ マッピング セグメント、およびヒープをランダム化します。残念ながら、32 ビットのアドレス空間は非常に狭いため、ランダム化の余地がほとんどなく、その効果が妨げられています。

  • 5)プロセス内のスタックをランダム化することを指していますか、それともすべてのプロセスのスペースを数えた後に残っているものを指していますか?
4

3 に答える 3

11

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.

于 2013-01-13T05:58:03.787 に答える
1

Part 1. The Stack ...

A function can call a function, which might call another function. Any variables allocated end up on the stack through each iteration. And de-allocated as each function exits, hence "stack". You might consider Wikipedia for this stuff ... http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29

于 2013-01-13T05:57:38.133 に答える
1

Linux は、開始アドレスにオフセットを追加することにより、スタック、メモリ マッピング セグメント、およびヒープをランダム化します。残念ながら、32 ビットのアドレス空間は非常に狭いため、ランダム化の余地がほとんどなく、その効果が妨げられています。

これは、32 ビットと 64 ビットでランダム化する機能を比較する際に、この記事で行われている一般化のようなものだと思います。32 ビットの 3 GB のアドレス指定可能なメモリは、「移動」するためのスペースがまだかなりあります...64 ビット OS で利用できるほどのスペースではなく、image- などの特定のアプリケーションがあります。エディターなどは非常にメモリを集中的に使用し、利用可能な 3GB のアドレス可能なメモリ全体を簡単に使い切ってしまう可能性があります。「アドレス可能な」メモリと言っていることに注意してください...これはプラットフォームに依存し、システムで利用可能な物理メモリの量ではありません。

于 2013-01-13T06:08:38.193 に答える