第6章インフラストラクチャーの抽象化層:オペレーティングシステム
Virtual Memory仮想メモリ
The key component of the operating system that ensures process isolation by guaranteeing that each process gets its own view of the memory is virtual memory. A running program (process) has a seemingly infinite view of memory and can access any region without worrying about other programs that might also be running on the computer. The OS seamlessly translates each process memory request into a separate region of the physical hardware memory through address translation. When the system needs to find a physical address in the memory that matches the virtual address, address translation occurs. The running process only deals with virtual addresses and never sees the physical address. Virtual memory is mapped to physical memory in units called “pages.”
英語のヒント
各プロセスに固有のメモリの見え方を保証し、プロセスの分離を確保する、OSの重要な構成要素が仮想メモリである。実行中のプログラム、つまりプロセスには、メモリが一見無限にあるように見える。同じコンピューター上でほかのプログラムも動いているかもしれないことを気にせず、どの領域にもアクセスできる。OSは、アドレス変換によって、各プロセスのメモリ要求を、物理ハードウェアのメモリ内の別々の領域へ、意識させることなく変換する。仮想アドレスに対応するメモリ内の物理アドレスを探す必要があるときに、アドレス変換が行われる。実行中のプロセスが扱うのは仮想アドレスだけで、物理アドレスを目にすることはない。仮想メモリは、「ページ」という単位で物理メモリに対応付けられる。
There is a time cost associated with performing virtual-to-physical memory address translation, however, and this can add up given that most programs need access to the memory to store data. To speed up address translation, the CPU has dedicated hardware for caching (storing) recent address translations called a translation lookaside buffer (TLB). A TLB is a memory cache that stores the virtual memory recent transaction to physical memory. TLBs help the CPU avoid making multiple round trips to main memory just to resolve a single virtual memory access by only requiring one round trip (Figure 6.31).
英語のヒント
ただし、仮想メモリアドレスを物理メモリアドレスへ変換するには時間がかかる。ほとんどのプログラムは、データを格納するためにメモリアクセスを必要とするので、この時間は積み重なる。アドレス変換を速めるため、CPUは、最近のアドレス変換をキャッシュ、つまり保存する専用ハードウェアである、トランスレーション・ルックアサイド・バッファー(TLB)を持つ。TLBは、仮想メモリから物理メモリへの最近の変換を保存するメモリキャッシュである。TLBにより、CPUは、一回の仮想メモリアクセスを解決するためだけに主記憶へ何度も往復することを避け、一往復だけで済ませられる(図6.31)。
A TLB contains page table entries that have been most recently used. Given a virtual address, the processor examines the TLB table. If a page table entry is present, it’s a “hit.” This means the frame number is retrieved, and the real address is formed. If a page table entry is not found in the TLB, then it’s a “miss.” In this case, the page number is used as an index while processing the page table. TLB checks if the page is already in the memory; if it’s not, then a page fault is issued and the TLB is updated to include the new page entry.
英語のヒント
TLBには、最も最近使われたページテーブル項目が入っている。仮想アドレスが与えられると、プロセッサはTLBの表を調べる。ページテーブル項目があれば「ヒット」である。この場合、フレーム番号を取り出して、実アドレスを作る。TLBにページテーブル項目が見つからなければ「ミス」である。その場合、ページテーブルを処理するときに、ページ番号を索引として使う。TLBは、ページがすでにメモリ内にあるかを調べる。なければ、ページフォールトを発生させ、新しいページ項目を含むようにTLBを更新する。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 6.31 Translation lookaside buffers (TLBs) speed up address translation by using an approach that involves detecting “hits” (which means a page table entry is present in the TLB) and “misses” (a page table entry is not found in the TLB). (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.31 トランスレーション・ルックアサイド・バッファー(TLB)は、「ヒット」(ページテーブル項目がTLBにあること)と「ミス」(ページテーブル項目がTLBに見つからないこと)を検出する方法により、アドレス変換を高速化する。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
LINK TO LEARNING学習リンク
What is the fundamental concept that makes it possible to implement virtual memory? Check out an explanation of how to implement virtual memory (https://openstax.org/r/76virtmem) to investigate this question.
英語のヒント
仮想メモリを実現できるようにする基本的な概念は何だろうか。この問いを調べるために、仮想メモリの実装方法の解説(https://openstax.org/r/76virtmem)を見よう。
Demand Paging
デマンドページング
The storage mechanism that uses a page form in retrieving a process from secondary or virtual memory to main memory is called paging. Virtual memory presented a seemingly infinite amount of memory to the running process, but what happens when the operating system runs out of free physical memory? Modern operating systems also have a backup when DRAM runs out, which means virtual memory can be mapped to disk to meet demand. The storage mechanism in which pages should only allocate in the memory if it is required from the execution process is called demand paging. Figure 6.32 shows a CPU that is demanding pages from the virtual memory to the main memory (i.e., swap in) and releasing pages from the main memory to the virtual memory (i.e., swap out). The working set size (WSS) refers to the total amount of memory a process requires during a specific period of activity, measured as the set of pages or data blocks the process accesses. WSS is measured by tracking the unique pages a process references over a fixed interval of time. This provides an estimate of the process’s active memory footprint and helps in memory management decisions like paging and swapping to optimize performance and resource allocation.
英語のヒント
二次記憶や仮想メモリから主記憶へプロセスを取り出す際、ページという形式を使う記憶の仕組みを、ページングという。仮想メモリは、実行中のプロセスに一見無限のメモリを見せるが、OSが空き物理メモリを使い果たしたらどうなるだろうか。現代のOSには、DRAMが足りなくなったときの予備の手段もある。つまり、需要を満たすために、仮想メモリをディスクへ対応付けられる。実行プロセスが必要とする場合にだけ、ページをメモリへ割り当てる記憶の仕組みを、デマンドページングという。図6.32は、CPUが仮想メモリから主記憶へページを要求すること(スワップイン)と、主記憶から仮想メモリへページを解放すること(スワップアウト)を示す。ワーキングセットサイズ(WSS)は、プロセスが一定の活動期間中に必要とするメモリの総量を指し、アクセスするページやデータブロックの集合として測る。WSSは、一定の時間間隔内にプロセスが参照する、重複を除いたページを追跡して測定する。これにより、プロセスが実際に使っているメモリの規模を推定でき、性能やリソース割り当てを最適化するための、ページングやスワッピングといったメモリ管理上の判断に役立つ。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 6.32 In demand paging, a CPU retrieves pages from the virtual memory to the main memory and releases pages from the main memory to the virtual memory. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.32 デマンドページングでは、CPUは仮想メモリから主記憶へページを取り出し、主記憶から仮想メモリへページを解放する。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
When the CPU demands a page and this page is not present in the main memory, we call this situation a page fault. A page fault occurs when a process references a page that is in the backing store. To handle a page fault, the CPU transfers control from the program to the OS to demand the requested page to the main memory. OS finds a free page frame in memory, loads the page from the backing store to the main memory, and resumes execution of the thread. The CPU has special hardware to assist in resuming execution after a page fault.
英語のヒント
CPUがページを要求したのに、そのページが主記憶にない状況を、ページフォールトという。ページフォールトは、プロセスが補助記憶領域にあるページを参照すると起こる。これに対処するため、CPUはプログラムからOSへ制御を移し、要求されたページを主記憶へ読み込むよう求める。OSはメモリ内の空きページフレームを探し、補助記憶領域から主記憶へページを読み込み、スレッドの実行を再開する。CPUには、ページフォールト後の実行再開を助ける特別なハードウェアがある。
Given that access to the disk is much slower than DRAM, operating systems are often designed to predictively swap in-use pages into DRAM and out-of-use pages to disk. The process of bringing pages into memory (i.e., demand paging) is called page fetching. Most modern OSs use page fetching by starting the process with no pages loaded and do not load a page into memory until it is referenced. If a requested page is stored on the disk, prefetching, which is the act of trying to predict when pages will be needed and loading them ahead of time to avoid page faults, is performed.
英語のヒント
ディスクへのアクセスはDRAMよりはるかに遅いため、OSは多くの場合、予測に基づき、使用中のページをDRAMへ、使われていないページをディスクへ移すよう設計されている。ページをメモリへ持ってくる処理、つまりデマンドページングを、ページフェッチという。現代のOSの大半は、ページを一つも読み込まずにプロセスを開始し、参照されるまでページをメモリへ読み込まない方法で、ページフェッチを行う。要求したページがディスクにある場合は、プリフェッチを行う。これは、ページが必要になる時点を予測し、ページフォールトを避けるため、先に読み込んでおこうとする処理である。
If all memory is in use, it is necessary to throw out one page each time there is a page fault. This process is called page replacement. In page replacement, one page in the (full) DRAM is swapped to disk while the requested page is brought into DRAM. However, if too many processes need access to a lot of memory back and forth between DRAM and disk, this causes problems. For example, each page fault causes one of the active pages to be moved to disk, so another page fault soon occurs, and this leads to thrashing. Thrashing is when a computer’s operating system becomes overwhelmed by the number of processes requesting memory. This situation leads to a cycle where the system spends more time moving data between the physical memory and disk storage (paging or swapping) than executing actual processes. It’s like a busy restaurant where the staff spends more time rearranging tables than serving food. The main cause of thrashing is often that too many programs are running at the same time. These activities exceed the available memory, causing the system to constantly try to make space for new requests by moving data to and from the disk.
英語のヒント
すべてのメモリが使用中なら、ページフォールトのたびに、一つのページを追い出す必要がある。この処理をページ置換という。ページ置換では、満杯のDRAM内の一つのページをディスクへ移すと同時に、要求されたページをDRAMへ持ってくる。しかし、あまりに多くのプロセスが大量のメモリを必要とし、DRAMとディスクの間をデータが行き来すると、問題が起きる。たとえば、ページフォールトのたびに、使用中のページの一つがディスクへ移されると、すぐに別のページフォールトが起こり、スラッシングにつながる。スラッシングは、メモリを要求するプロセスの多さに、コンピューターのOSが対応しきれなくなることである。この状況では、実際にプロセスを実行するよりも、物理メモリとディスク記憶領域の間でデータを動かすこと、つまりページングやスワッピングに多くの時間を費やす循環に陥る。料理を出すよりテーブルの配置換えに時間を取られる、忙しいレストランのようなものだ。スラッシングの主な原因は、同時に動かしているプログラムが多すぎることが多い。その活動が利用可能なメモリ量を超えるため、システムは、新しい要求のための空きを作ろうと、ディスクとの間で絶えずデータを動かす。
Strategies to prevent thrashing include limiting the number of simultaneously running programs to avoid memory overcommitment, optimizing how memory is allocated to processes, and possibly increasing the system’s physical memory. By managing memory more efficiently and ensuring that the system is not overloaded with too many tasks, the system can not only avoid a slowdown, but significantly improve its performance.
英語のヒント
スラッシングを防ぐ方法には、同時に実行するプログラム数を制限してメモリの過剰な割り当てを避けること、プロセスへのメモリ割り当て方法を最適化すること、必要に応じて物理メモリを増やすことがある。メモリをより効率よく管理し、タスクが多すぎてシステムに過大な負荷がかからないようにすれば、速度低下を防げるだけでなく、性能を大幅に高められる。
In extreme cases of thrashing, the OS can spend all its time fetching and replacing pages and will not get much work done. This is one reason why our devices can slow to a halt when they run out of memory and each thread must wait on requested pages.
英語のヒント
スラッシングが極端になると、OSは時間のすべてをページの取得と置換に費やし、ほとんど仕事を進められなくなる。機器のメモリが足りなくなり、各スレッドが要求したページを待たなければならないと、動作が遅くなって停止状態に至る理由の一つがこれである。